@@ -53,6 +53,8 @@ class Runner
5353 */
5454 public function runPHPCS ()
5555 {
56+ $ this ->register_out_of_memory_shutdown_message ('phpcs ' );
57+
5658 try {
5759 Util \Timing::startTiming ();
5860 Runner::checkRequirements ();
@@ -153,6 +155,8 @@ public function runPHPCS()
153155 */
154156 public function runPHPCBF ()
155157 {
158+ $ this ->register_out_of_memory_shutdown_message ('phpcbf ' );
159+
156160 if (defined ('PHP_CODESNIFFER_CBF ' ) === false ) {
157161 define ('PHP_CODESNIFFER_CBF ' , true );
158162 }
@@ -891,4 +895,42 @@ public function printProgress(File $file, $numFiles, $numProcessed)
891895 }//end printProgress()
892896
893897
898+ /**
899+ * Registers a PHP shutdown function to provide a more informative out of memory error.
900+ *
901+ * @param string $command The command which was used to initiate the PHPCS run.
902+ *
903+ * @return void
904+ */
905+ private function register_out_of_memory_shutdown_message ($ command )
906+ {
907+ // Allocate all needed memory beforehand as much as possible.
908+ $ errorMsg = PHP_EOL .'The PHP_CodeSniffer "%1$s" command ran out of memory. ' .PHP_EOL ;
909+ $ errorMsg .= 'Either raise the "memory_limit" of PHP in the php.ini file or raise the memory limit at runtime ' .PHP_EOL ;
910+ $ errorMsg .= 'using `%1$s -d memory_limit=512M` (replace 512M with the desired memory limit). ' .PHP_EOL ;
911+ $ errorMsg = sprintf ($ errorMsg , $ command );
912+ $ memoryError = 'Allowed memory size of ' ;
913+ $ errorArray = [
914+ 'type ' => 42 ,
915+ 'message ' => 'Some random dummy string to take up memory and take up some more memory and some more ' ,
916+ 'file ' => 'Another random string, which would be a filename this time. Should be relatively long to allow for deeply nested files ' ,
917+ 'line ' => 31427 ,
918+ ];
919+
920+ register_shutdown_function (
921+ static function () use (
922+ $ errorMsg ,
923+ $ memoryError ,
924+ $ errorArray
925+ ) {
926+ $ errorArray = error_get_last ();
927+ if (is_array ($ errorArray ) === true && strpos ($ errorArray ['message ' ], $ memoryError ) !== false ) {
928+ echo $ errorMsg ;
929+ }
930+ }
931+ );
932+
933+ }//end register_out_of_memory_shutdown_message()
934+
935+
894936}//end class
0 commit comments