comparison vendor/squizlabs/php_codesniffer/src/Runner.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
228 228
229 }//end runPHPCBF() 229 }//end runPHPCBF()
230 230
231 231
232 /** 232 /**
233 * Exits if the minimum requirements of PHP_CodSniffer are not met. 233 * Exits if the minimum requirements of PHP_CodeSniffer are not met.
234 * 234 *
235 * @return array 235 * @return array
236 * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
236 */ 237 */
237 public function checkRequirements() 238 public function checkRequirements()
238 { 239 {
239 // Check the PHP version. 240 // Check the PHP version.
240 if (PHP_VERSION_ID < 50400) { 241 if (PHP_VERSION_ID < 50400) {
241 $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; 242 $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL;
242 throw new DeepExitException($error, 3); 243 throw new DeepExitException($error, 3);
243 } 244 }
244 245
245 if (extension_loaded('tokenizer') === false) { 246 $requiredExtensions = [
246 $error = 'ERROR: PHP_CodeSniffer requires the tokenizer extension to be enabled.'.PHP_EOL; 247 'tokenizer',
248 'xmlwriter',
249 'SimpleXML',
250 ];
251 $missingExtensions = [];
252
253 foreach ($requiredExtensions as $extension) {
254 if (extension_loaded($extension) === false) {
255 $missingExtensions[] = $extension;
256 }
257 }
258
259 if (empty($missingExtensions) === false) {
260 $last = array_pop($requiredExtensions);
261 $required = implode(', ', $requiredExtensions);
262 $required .= ' and '.$last;
263
264 if (count($missingExtensions) === 1) {
265 $missing = $missingExtensions[0];
266 } else {
267 $last = array_pop($missingExtensions);
268 $missing = implode(', ', $missingExtensions);
269 $missing .= ' and '.$last;
270 }
271
272 $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL;
273 $error = sprintf($error, $required, $missing);
247 throw new DeepExitException($error, 3); 274 throw new DeepExitException($error, 3);
248 } 275 }
249 276
250 }//end checkRequirements() 277 }//end checkRequirements()
251 278
252 279
253 /** 280 /**
254 * Init the rulesets and other high-level settings. 281 * Init the rulesets and other high-level settings.
255 * 282 *
256 * @return void 283 * @return void
284 * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
257 */ 285 */
258 public function init() 286 public function init()
259 { 287 {
260 if (defined('PHP_CODESNIFFER_CBF') === false) { 288 if (defined('PHP_CODESNIFFER_CBF') === false) {
261 define('PHP_CODESNIFFER_CBF', false); 289 define('PHP_CODESNIFFER_CBF', false);
262 } 290 }
263 291
264 // Ensure this option is enabled or else line endings will not always 292 // Ensure this option is enabled or else line endings will not always
265 // be detected properly for files created on a Mac with the /r line ending. 293 // be detected properly for files created on a Mac with the /r line ending.
266 ini_set('auto_detect_line_endings', true); 294 ini_set('auto_detect_line_endings', true);
295
296 // Disable the PCRE JIT as this caused issues with parallel running.
297 ini_set('pcre.jit', false);
267 298
268 // Check that the standards are valid. 299 // Check that the standards are valid.
269 foreach ($this->config->standards as $standard) { 300 foreach ($this->config->standards as $standard) {
270 if (Util\Standards::isInstalledStandard($standard) === false) { 301 if (Util\Standards::isInstalledStandard($standard) === false) {
271 // They didn't select a valid coding standard, so help them 302 // They didn't select a valid coding standard, so help them
278 throw new DeepExitException($error, 3); 309 throw new DeepExitException($error, 3);
279 } 310 }
280 } 311 }
281 312
282 // Saves passing the Config object into other objects that only need 313 // Saves passing the Config object into other objects that only need
283 // the verbostity flag for deubg output. 314 // the verbosity flag for debug output.
284 if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { 315 if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {
285 define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); 316 define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity);
286 } 317 }
287 318
288 // Create this class so it is autoloaded and sets up a bunch 319 // Create this class so it is autoloaded and sets up a bunch
310 341
311 /** 342 /**
312 * Performs the run. 343 * Performs the run.
313 * 344 *
314 * @return int The number of errors and warnings found. 345 * @return int The number of errors and warnings found.
346 * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
347 * @throws \PHP_CodeSniffer\Exceptions\RuntimeException
315 */ 348 */
316 private function run() 349 private function run()
317 { 350 {
318 // The class that manages all reporters for the run. 351 // The class that manages all reporters for the run.
319 $this->reporter = new Reporter($this->config); 352 $this->reporter = new Reporter($this->config);
504 file_put_contents($childOutFilename, $output); 537 file_put_contents($childOutFilename, $output);
505 exit($pid); 538 exit($pid);
506 }//end if 539 }//end if
507 }//end for 540 }//end for
508 541
509 $this->processChildProcs($childProcs); 542 $success = $this->processChildProcs($childProcs);
543 if ($success === false) {
544 throw new RuntimeException('One or more child processes failed to run');
545 }
510 }//end if 546 }//end if
511 547
512 restore_error_handler(); 548 restore_error_handler();
513 549
514 if (PHP_CODESNIFFER_VERBOSITY === 0 550 if (PHP_CODESNIFFER_VERBOSITY === 0
556 * @param string $message The error message. 592 * @param string $message The error message.
557 * @param string $file The path of the file that raised the error. 593 * @param string $file The path of the file that raised the error.
558 * @param int $line The line number the error was raised at. 594 * @param int $line The line number the error was raised at.
559 * 595 *
560 * @return void 596 * @return void
597 * @throws \PHP_CodeSniffer\Exceptions\RuntimeException
561 */ 598 */
562 public function handleErrors($code, $message, $file, $line) 599 public function handleErrors($code, $message, $file, $line)
563 { 600 {
564 if ((error_reporting() & $code) === 0) { 601 if ((error_reporting() & $code) === 0) {
565 // This type of error is being muted. 602 // This type of error is being muted.
575 * Processes a single file, including checking and fixing. 612 * Processes a single file, including checking and fixing.
576 * 613 *
577 * @param \PHP_CodeSniffer\Files\File $file The file to be processed. 614 * @param \PHP_CodeSniffer\Files\File $file The file to be processed.
578 * 615 *
579 * @return void 616 * @return void
617 * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
580 */ 618 */
581 public function processFile($file) 619 public function processFile($file)
582 { 620 {
583 if (PHP_CODESNIFFER_VERBOSITY > 0) { 621 if (PHP_CODESNIFFER_VERBOSITY > 0) {
584 $startTime = microtime(true); 622 $startTime = microtime(true);
675 private function processChildProcs($childProcs) 713 private function processChildProcs($childProcs)
676 { 714 {
677 $numProcessed = 0; 715 $numProcessed = 0;
678 $totalBatches = count($childProcs); 716 $totalBatches = count($childProcs);
679 717
718 $success = true;
719
680 while (count($childProcs) > 0) { 720 while (count($childProcs) > 0) {
681 foreach ($childProcs as $key => $procData) { 721 foreach ($childProcs as $key => $procData) {
682 $res = pcntl_waitpid($procData['pid'], $status, WNOHANG); 722 $res = pcntl_waitpid($procData['pid'], $status, WNOHANG);
683 if ($res === $procData['pid']) { 723 if ($res === $procData['pid']) {
684 if (file_exists($procData['out']) === true) { 724 if (file_exists($procData['out']) === true) {
685 include $procData['out']; 725 include $procData['out'];
686 if (isset($childOutput) === true) { 726
687 $this->reporter->totalFiles += $childOutput['totalFiles']; 727 unlink($procData['out']);
688 $this->reporter->totalErrors += $childOutput['totalErrors']; 728 unset($childProcs[$key]);
689 $this->reporter->totalWarnings += $childOutput['totalWarnings']; 729
690 $this->reporter->totalFixable += $childOutput['totalFixable']; 730 $numProcessed++;
691 $this->reporter->totalFixed += $childOutput['totalFixed']; 731
732 if (isset($childOutput) === false) {
733 // The child process died, so the run has failed.
734 $file = new DummyFile(null, $this->ruleset, $this->config);
735 $file->setErrorCounts(1, 0, 0, 0);
736 $this->printProgress($file, $totalBatches, $numProcessed);
737 $success = false;
738 continue;
692 } 739 }
740
741 $this->reporter->totalFiles += $childOutput['totalFiles'];
742 $this->reporter->totalErrors += $childOutput['totalErrors'];
743 $this->reporter->totalWarnings += $childOutput['totalWarnings'];
744 $this->reporter->totalFixable += $childOutput['totalFixable'];
745 $this->reporter->totalFixed += $childOutput['totalFixed'];
693 746
694 if (isset($debugOutput) === true) { 747 if (isset($debugOutput) === true) {
695 echo $debugOutput; 748 echo $debugOutput;
696 } 749 }
697 750
698 if (isset($childCache) === true) { 751 if (isset($childCache) === true) {
699 foreach ($childCache as $path => $cache) { 752 foreach ($childCache as $path => $cache) {
700 Cache::set($path, $cache); 753 Cache::set($path, $cache);
701 } 754 }
702 } 755 }
703
704 unlink($procData['out']);
705 unset($childProcs[$key]);
706
707 $numProcessed++;
708 756
709 // Fake a processed file so we can print progress output for the batch. 757 // Fake a processed file so we can print progress output for the batch.
710 $file = new DummyFile(null, $this->ruleset, $this->config); 758 $file = new DummyFile(null, $this->ruleset, $this->config);
711 $file->setErrorCounts( 759 $file->setErrorCounts(
712 $childOutput['totalErrors'], 760 $childOutput['totalErrors'],
718 }//end if 766 }//end if
719 }//end if 767 }//end if
720 }//end foreach 768 }//end foreach
721 }//end while 769 }//end while
722 770
771 return $success;
772
723 }//end processChildProcs() 773 }//end processChildProcs()
724 774
725 775
726 /** 776 /**
727 * Print progress information for a single processed file. 777 * Print progress information for a single processed file.
728 * 778 *
729 * @param File $file The file that was processed. 779 * @param \PHP_CodeSniffer\Files\File $file The file that was processed.
730 * @param int $numFiles The total number of files to process. 780 * @param int $numFiles The total number of files to process.
731 * @param int $numProcessed The number of files that have been processed, 781 * @param int $numProcessed The number of files that have been processed,
732 * including this one. 782 * including this one.
733 * 783 *
734 * @return void 784 * @return void
735 */ 785 */
736 public function printProgress($file, $numFiles, $numProcessed) 786 public function printProgress(File $file, $numFiles, $numProcessed)
737 { 787 {
738 if (PHP_CODESNIFFER_VERBOSITY > 0 788 if (PHP_CODESNIFFER_VERBOSITY > 0
739 || $this->config->showProgress === false 789 || $this->config->showProgress === false
740 ) { 790 ) {
741 return; 791 return;