-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Runner.php
969 lines (808 loc) · 33.8 KB
/
Runner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
<?php
/**
* Responsible for running PHPCS and PHPCBF.
*
* After creating an object of this class, you probably just want to
* call runPHPCS() or runPHPCBF().
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer;
use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Files\FileList;
use PHP_CodeSniffer\Util\Cache;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Standards;
class Runner
{
/**
* The config data for the run.
*
* @var \PHP_CodeSniffer\Config
*/
public $config = null;
/**
* The ruleset used for the run.
*
* @var \PHP_CodeSniffer\Ruleset
*/
public $ruleset = null;
/**
* The reporter used for generating reports after the run.
*
* @var \PHP_CodeSniffer\Reporter
*/
public $reporter = null;
/**
* Run the PHPCS script.
*
* @return array
*/
public function runPHPCS()
{
$this->registerOutOfMemoryShutdownMessage('phpcs');
try {
Util\Timing::startTiming();
Runner::checkRequirements();
if (defined('PHP_CODESNIFFER_CBF') === false) {
define('PHP_CODESNIFFER_CBF', false);
}
// Creating the Config object populates it with all required settings
// based on the CLI arguments provided to the script and any config
// values the user has set.
$this->config = new Config();
// Init the run and load the rulesets to set additional config vars.
$this->init();
// Print a list of sniffs in each of the supplied standards.
// We fudge the config here so that each standard is explained in isolation.
if ($this->config->explain === true) {
$standards = $this->config->standards;
foreach ($standards as $standard) {
$this->config->standards = [$standard];
$ruleset = new Ruleset($this->config);
$ruleset->explain();
}
return 0;
}
// Generate documentation for each of the supplied standards.
if ($this->config->generator !== null) {
$standards = $this->config->standards;
foreach ($standards as $standard) {
$this->config->standards = [$standard];
$ruleset = new Ruleset($this->config);
$class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator;
$generator = new $class($ruleset);
$generator->generate();
}
return 0;
}
// Other report formats don't really make sense in interactive mode
// so we hard-code the full report here and when outputting.
// We also ensure parallel processing is off because we need to do one file at a time.
if ($this->config->interactive === true) {
$this->config->reports = ['full' => null];
$this->config->parallel = 1;
$this->config->showProgress = false;
}
// Disable caching if we are processing STDIN as we can't be 100%
// sure where the file came from or if it will change in the future.
if ($this->config->stdin === true) {
$this->config->cache = false;
}
$numErrors = $this->run();
// Print all the reports for this run.
$toScreen = $this->reporter->printReports();
// Only print timer output if no reports were
// printed to the screen so we don't put additional output
// in something like an XML report. If we are printing to screen,
// the report types would have already worked out who should
// print the timer info.
if ($this->config->interactive === false
&& ($toScreen === false
|| (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true))
) {
Util\Timing::printRunTime();
}
} catch (DeepExitException $e) {
echo $e->getMessage();
return $e->getCode();
}//end try
if ($numErrors === 0) {
// No errors found.
return 0;
} else if ($this->reporter->totalFixable === 0) {
// Errors found, but none of them can be fixed by PHPCBF.
return 1;
} else {
// Errors found, and some can be fixed by PHPCBF.
return 2;
}
}//end runPHPCS()
/**
* Run the PHPCBF script.
*
* @return array
*/
public function runPHPCBF()
{
$this->registerOutOfMemoryShutdownMessage('phpcbf');
if (defined('PHP_CODESNIFFER_CBF') === false) {
define('PHP_CODESNIFFER_CBF', true);
}
try {
Util\Timing::startTiming();
Runner::checkRequirements();
// Creating the Config object populates it with all required settings
// based on the CLI arguments provided to the script and any config
// values the user has set.
$this->config = new Config();
// When processing STDIN, we can't output anything to the screen
// or it will end up mixed in with the file output.
if ($this->config->stdin === true) {
$this->config->verbosity = 0;
}
// Init the run and load the rulesets to set additional config vars.
$this->init();
// When processing STDIN, we only process one file at a time and
// we don't process all the way through, so we can't use the parallel
// running system.
if ($this->config->stdin === true) {
$this->config->parallel = 1;
}
// Override some of the command line settings that might break the fixes.
$this->config->generator = null;
$this->config->explain = false;
$this->config->interactive = false;
$this->config->cache = false;
$this->config->showSources = false;
$this->config->recordErrors = false;
$this->config->reportFile = null;
$this->config->reports = ['cbf' => null];
// If a standard tries to set command line arguments itself, some
// may be blocked because PHPCBF is running, so stop the script
// dying if any are found.
$this->config->dieOnUnknownArg = false;
$this->run();
$this->reporter->printReports();
echo PHP_EOL;
Util\Timing::printRunTime();
} catch (DeepExitException $e) {
echo $e->getMessage();
return $e->getCode();
}//end try
if ($this->reporter->totalFixed === 0) {
// Nothing was fixed by PHPCBF.
if ($this->reporter->totalFixable === 0) {
// Nothing found that could be fixed.
return 0;
} else {
// Something failed to fix.
return 2;
}
}
if ($this->reporter->totalFixable === 0) {
// PHPCBF fixed all fixable errors.
return 1;
}
// PHPCBF fixed some fixable errors, but others failed to fix.
return 2;
}//end runPHPCBF()
/**
* Exits if the minimum requirements of PHP_CodeSniffer are not met.
*
* @return void
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the requirements are not met.
*/
public function checkRequirements()
{
// Check the PHP version.
if (PHP_VERSION_ID < 50400) {
$error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL;
throw new DeepExitException($error, 3);
}
$requiredExtensions = [
'tokenizer',
'xmlwriter',
'SimpleXML',
];
$missingExtensions = [];
foreach ($requiredExtensions as $extension) {
if (extension_loaded($extension) === false) {
$missingExtensions[] = $extension;
}
}
if (empty($missingExtensions) === false) {
$last = array_pop($requiredExtensions);
$required = implode(', ', $requiredExtensions);
$required .= ' and '.$last;
if (count($missingExtensions) === 1) {
$missing = $missingExtensions[0];
} else {
$last = array_pop($missingExtensions);
$missing = implode(', ', $missingExtensions);
$missing .= ' and '.$last;
}
$error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL;
$error = sprintf($error, $required, $missing);
throw new DeepExitException($error, 3);
}
}//end checkRequirements()
/**
* Init the rulesets and other high-level settings.
*
* @return void
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If a referenced standard is not installed.
*/
public function init()
{
if (defined('PHP_CODESNIFFER_CBF') === false) {
define('PHP_CODESNIFFER_CBF', false);
}
// Ensure this option is enabled or else line endings will not always
// be detected properly for files created on a Mac with the /r line ending.
@ini_set('auto_detect_line_endings', true);
// Disable the PCRE JIT as this caused issues with parallel running.
ini_set('pcre.jit', false);
// Check that the standards are valid.
foreach ($this->config->standards as $standard) {
if (Util\Standards::isInstalledStandard($standard) === false) {
// They didn't select a valid coding standard, so help them
// out by letting them know which standards are installed.
$error = 'ERROR: the "'.$standard.'" coding standard is not installed. ';
ob_start();
Util\Standards::printInstalledStandards();
$error .= ob_get_contents();
ob_end_clean();
throw new DeepExitException($error, 3);
}
}
// Saves passing the Config object into other objects that only need
// the verbosity flag for debug output.
if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {
define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity);
}
// Create this class so it is autoloaded and sets up a bunch
// of PHP_CodeSniffer-specific token type constants.
$tokens = new Util\Tokens();
// Allow autoloading of custom files inside installed standards.
$installedStandards = Standards::getInstalledStandardDetails();
foreach ($installedStandards as $name => $details) {
Autoload::addSearchPath($details['path'], $details['namespace']);
}
// The ruleset contains all the information about how the files
// should be checked and/or fixed.
try {
$this->ruleset = new Ruleset($this->config);
} catch (RuntimeException $e) {
$error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL;
$error .= $this->config->printShortUsage(true);
throw new DeepExitException($error, 3);
}
}//end init()
/**
* Performs the run.
*
* @return int The number of errors and warnings found.
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
*/
private function run()
{
// The class that manages all reporters for the run.
$this->reporter = new Reporter($this->config);
// Include bootstrap files.
foreach ($this->config->bootstrap as $bootstrap) {
include $bootstrap;
}
if ($this->config->stdin === true) {
$fileContents = $this->config->stdinContent;
if ($fileContents === null) {
$handle = fopen('php://stdin', 'r');
stream_set_blocking($handle, true);
$fileContents = stream_get_contents($handle);
fclose($handle);
}
$todo = new FileList($this->config, $this->ruleset);
$dummy = new DummyFile($fileContents, $this->ruleset, $this->config);
$todo->addFile($dummy->path, $dummy);
} else {
if (empty($this->config->files) === true) {
$error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL;
$error .= $this->config->printShortUsage(true);
throw new DeepExitException($error, 3);
}
if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo 'Creating file list... ';
}
$todo = new FileList($this->config, $this->ruleset);
if (PHP_CODESNIFFER_VERBOSITY > 0) {
$numFiles = count($todo);
echo "DONE ($numFiles files in queue)".PHP_EOL;
}
if ($this->config->cache === true) {
if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo 'Loading cache... ';
}
Cache::load($this->ruleset, $this->config);
if (PHP_CODESNIFFER_VERBOSITY > 0) {
$size = Cache::getSize();
echo "DONE ($size files in cache)".PHP_EOL;
}
}
}//end if
// Turn all sniff errors into exceptions.
set_error_handler([$this, 'handleErrors']);
// If verbosity is too high, turn off parallelism so the
// debug output is clean.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$this->config->parallel = 1;
}
// If the PCNTL extension isn't installed, we can't fork.
if (function_exists('pcntl_fork') === false) {
$this->config->parallel = 1;
}
$lastDir = '';
$numFiles = count($todo);
if ($this->config->parallel === 1) {
// Running normally.
$numProcessed = 0;
foreach ($todo as $path => $file) {
if ($file->ignored === false) {
$currDir = dirname($path);
if ($lastDir !== $currDir) {
if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL;
}
$lastDir = $currDir;
}
$this->processFile($file);
} else if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo 'Skipping '.basename($file->path).PHP_EOL;
}
$numProcessed++;
$this->printProgress($file, $numFiles, $numProcessed);
}
} else {
// Batching and forking.
$childProcs = [];
$numPerBatch = ceil($numFiles / $this->config->parallel);
for ($batch = 0; $batch < $this->config->parallel; $batch++) {
$startAt = ($batch * $numPerBatch);
if ($startAt >= $numFiles) {
break;
}
$endAt = ($startAt + $numPerBatch);
if ($endAt > $numFiles) {
$endAt = $numFiles;
}
$childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child');
$pid = pcntl_fork();
if ($pid === -1) {
throw new RuntimeException('Failed to create child process');
} else if ($pid !== 0) {
$childProcs[$pid] = $childOutFilename;
} else {
// Move forward to the start of the batch.
$todo->rewind();
for ($i = 0; $i < $startAt; $i++) {
$todo->next();
}
// Reset the reporter to make sure only figures from this
// file batch are recorded.
$this->reporter->totalFiles = 0;
$this->reporter->totalErrors = 0;
$this->reporter->totalWarnings = 0;
$this->reporter->totalFixable = 0;
$this->reporter->totalFixed = 0;
// Process the files.
$pathsProcessed = [];
ob_start();
for ($i = $startAt; $i < $endAt; $i++) {
$path = $todo->key();
$file = $todo->current();
if ($file->ignored === true) {
$todo->next();
continue;
}
$currDir = dirname($path);
if ($lastDir !== $currDir) {
if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL;
}
$lastDir = $currDir;
}
$this->processFile($file);
$pathsProcessed[] = $path;
$todo->next();
}//end for
$debugOutput = ob_get_contents();
ob_end_clean();
// Write information about the run to the filesystem
// so it can be picked up by the main process.
$childOutput = [
'totalFiles' => $this->reporter->totalFiles,
'totalErrors' => $this->reporter->totalErrors,
'totalWarnings' => $this->reporter->totalWarnings,
'totalFixable' => $this->reporter->totalFixable,
'totalFixed' => $this->reporter->totalFixed,
];
$output = '<'.'?php'."\n".' $childOutput = ';
$output .= var_export($childOutput, true);
$output .= ";\n\$debugOutput = ";
$output .= var_export($debugOutput, true);
if ($this->config->cache === true) {
$childCache = [];
foreach ($pathsProcessed as $path) {
$childCache[$path] = Cache::get($path);
}
$output .= ";\n\$childCache = ";
$output .= var_export($childCache, true);
}
$output .= ";\n?".'>';
file_put_contents($childOutFilename, $output);
exit();
}//end if
}//end for
$success = $this->processChildProcs($childProcs);
if ($success === false) {
throw new RuntimeException('One or more child processes failed to run');
}
}//end if
restore_error_handler();
if (PHP_CODESNIFFER_VERBOSITY === 0
&& $this->config->interactive === false
&& $this->config->showProgress === true
) {
echo PHP_EOL.PHP_EOL;
}
if ($this->config->cache === true) {
Cache::save();
}
$ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit');
$ignoreErrors = Config::getConfigData('ignore_errors_on_exit');
$return = ($this->reporter->totalErrors + $this->reporter->totalWarnings);
if ($ignoreErrors !== null) {
$ignoreErrors = (bool) $ignoreErrors;
if ($ignoreErrors === true) {
$return -= $this->reporter->totalErrors;
}
}
if ($ignoreWarnings !== null) {
$ignoreWarnings = (bool) $ignoreWarnings;
if ($ignoreWarnings === true) {
$return -= $this->reporter->totalWarnings;
}
}
return $return;
}//end run()
/**
* Converts all PHP errors into exceptions.
*
* This method forces a sniff to stop processing if it is not
* able to handle a specific piece of code, instead of continuing
* and potentially getting into a loop.
*
* @param int $code The level of error raised.
* @param string $message The error message.
* @param string $file The path of the file that raised the error.
* @param int $line The line number the error was raised at.
*
* @return void
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
*/
public function handleErrors($code, $message, $file, $line)
{
if ((error_reporting() & $code) === 0) {
// This type of error is being muted.
return true;
}
throw new RuntimeException("$message in $file on line $line");
}//end handleErrors()
/**
* Processes a single file, including checking and fixing.
*
* @param \PHP_CodeSniffer\Files\File $file The file to be processed.
*
* @return void
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
*/
public function processFile($file)
{
if (PHP_CODESNIFFER_VERBOSITY > 0) {
$startTime = microtime(true);
echo 'Processing '.basename($file->path).' ';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
}
}
try {
$file->process();
if (PHP_CODESNIFFER_VERBOSITY > 0) {
$timeTaken = ((microtime(true) - $startTime) * 1000);
if ($timeTaken < 1000) {
$timeTaken = round($timeTaken);
echo "DONE in {$timeTaken}ms";
} else {
$timeTaken = round(($timeTaken / 1000), 2);
echo "DONE in $timeTaken secs";
}
if (PHP_CODESNIFFER_CBF === true) {
$errors = $file->getFixableCount();
echo " ($errors fixable violations)".PHP_EOL;
} else {
$errors = $file->getErrorCount();
$warnings = $file->getWarningCount();
echo " ($errors errors, $warnings warnings)".PHP_EOL;
}
}
} catch (\Exception $e) {
$error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage();
// Determine which sniff caused the error.
$sniffStack = null;
$nextStack = null;
foreach ($e->getTrace() as $step) {
if (isset($step['file']) === false) {
continue;
}
if (empty($sniffStack) === false) {
$nextStack = $step;
break;
}
if (substr($step['file'], -9) === 'Sniff.php') {
$sniffStack = $step;
continue;
}
}
if (empty($sniffStack) === false) {
if (empty($nextStack) === false
&& isset($nextStack['class']) === true
&& substr($nextStack['class'], -5) === 'Sniff'
) {
$sniffCode = Common::getSniffCode($nextStack['class']);
} else {
$sniffCode = substr(strrchr(str_replace('\\', '/', $sniffStack['file']), '/'), 1);
}
$error .= sprintf(PHP_EOL.'The error originated in the %s sniff on line %s.', $sniffCode, $sniffStack['line']);
}
$file->addErrorOnLine($error, 1, 'Internal.Exception');
}//end try
$this->reporter->cacheFileReport($file, $this->config);
if ($this->config->interactive === true) {
/*
Running interactively.
Print the error report for the current file and then wait for user input.
*/
// Get current violations and then clear the list to make sure
// we only print violations for a single file each time.
$numErrors = null;
while ($numErrors !== 0) {
$numErrors = ($file->getErrorCount() + $file->getWarningCount());
if ($numErrors === 0) {
continue;
}
$this->reporter->printReport('full');
echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
$input = fgets(STDIN);
$input = trim($input);
switch ($input) {
case 's':
break(2);
case 'q':
throw new DeepExitException('', 0);
default:
// Repopulate the sniffs because some of them save their state
// and only clear it when the file changes, but we are rechecking
// the same file.
$file->ruleset->populateTokenListeners();
$file->reloadContent();
$file->process();
$this->reporter->cacheFileReport($file, $this->config);
break;
}
}//end while
}//end if
// Clean up the file to save (a lot of) memory.
$file->cleanUp();
}//end processFile()
/**
* Waits for child processes to complete and cleans up after them.
*
* The reporting information returned by each child process is merged
* into the main reporter class.
*
* @param array $childProcs An array of child processes to wait for.
*
* @return bool
*/
private function processChildProcs($childProcs)
{
$numProcessed = 0;
$totalBatches = count($childProcs);
$success = true;
while (count($childProcs) > 0) {
$pid = pcntl_waitpid(0, $status);
if ($pid <= 0) {
continue;
}
$childProcessStatus = pcntl_wexitstatus($status);
if ($childProcessStatus !== 0) {
$success = false;
}
$out = $childProcs[$pid];
unset($childProcs[$pid]);
if (file_exists($out) === false) {
continue;
}
include $out;
unlink($out);
$numProcessed++;
if (isset($childOutput) === false) {
// The child process died, so the run has failed.
$file = new DummyFile('', $this->ruleset, $this->config);
$file->setErrorCounts(1, 0, 0, 0);
$this->printProgress($file, $totalBatches, $numProcessed);
$success = false;
continue;
}
$this->reporter->totalFiles += $childOutput['totalFiles'];
$this->reporter->totalErrors += $childOutput['totalErrors'];
$this->reporter->totalWarnings += $childOutput['totalWarnings'];
$this->reporter->totalFixable += $childOutput['totalFixable'];
$this->reporter->totalFixed += $childOutput['totalFixed'];
if (isset($debugOutput) === true) {
echo $debugOutput;
}
if (isset($childCache) === true) {
foreach ($childCache as $path => $cache) {
Cache::set($path, $cache);
}
}
// Fake a processed file so we can print progress output for the batch.
$file = new DummyFile('', $this->ruleset, $this->config);
$file->setErrorCounts(
$childOutput['totalErrors'],
$childOutput['totalWarnings'],
$childOutput['totalFixable'],
$childOutput['totalFixed']
);
$this->printProgress($file, $totalBatches, $numProcessed);
}//end while
return $success;
}//end processChildProcs()
/**
* Print progress information for a single processed file.
*
* @param \PHP_CodeSniffer\Files\File $file The file that was processed.
* @param int $numFiles The total number of files to process.
* @param int $numProcessed The number of files that have been processed,
* including this one.
*
* @return void
*/
public function printProgress(File $file, $numFiles, $numProcessed)
{
if (PHP_CODESNIFFER_VERBOSITY > 0
|| $this->config->showProgress === false
) {
return;
}
// Show progress information.
if ($file->ignored === true) {
echo 'S';
} else {
$errors = $file->getErrorCount();
$warnings = $file->getWarningCount();
$fixable = $file->getFixableCount();
$fixed = $file->getFixedCount();
if (PHP_CODESNIFFER_CBF === true) {
// Files with fixed errors or warnings are F (green).
// Files with unfixable errors or warnings are E (red).
// Files with no errors or warnings are . (black).
if ($fixable > 0) {
if ($this->config->colors === true) {
echo "\033[31m";
}
echo 'E';
if ($this->config->colors === true) {
echo "\033[0m";
}
} else if ($fixed > 0) {
if ($this->config->colors === true) {
echo "\033[32m";
}
echo 'F';
if ($this->config->colors === true) {
echo "\033[0m";
}
} else {
echo '.';
}//end if
} else {
// Files with errors are E (red).
// Files with fixable errors are E (green).
// Files with warnings are W (yellow).
// Files with fixable warnings are W (green).
// Files with no errors or warnings are . (black).
if ($errors > 0) {
if ($this->config->colors === true) {
if ($fixable > 0) {
echo "\033[32m";
} else {
echo "\033[31m";
}
}
echo 'E';
if ($this->config->colors === true) {
echo "\033[0m";
}
} else if ($warnings > 0) {
if ($this->config->colors === true) {
if ($fixable > 0) {
echo "\033[32m";
} else {
echo "\033[33m";
}
}
echo 'W';
if ($this->config->colors === true) {
echo "\033[0m";
}
} else {
echo '.';
}//end if
}//end if
}//end if
$numPerLine = 60;
if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) {
return;
}
$percent = round(($numProcessed / $numFiles) * 100);
$padding = (strlen($numFiles) - strlen($numProcessed));
if ($numProcessed === $numFiles
&& $numFiles > $numPerLine
&& ($numProcessed % $numPerLine) !== 0
) {
$padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine)));
}
echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL;
}//end printProgress()
/**
* Registers a PHP shutdown function to provide a more informative out of memory error.
*
* @param string $command The command which was used to initiate the PHPCS run.
*
* @return void
*/
private function registerOutOfMemoryShutdownMessage($command)
{
// Allocate all needed memory beforehand as much as possible.
$errorMsg = PHP_EOL.'The PHP_CodeSniffer "%1$s" command ran out of memory.'.PHP_EOL;
$errorMsg .= 'Either raise the "memory_limit" of PHP in the php.ini file or raise the memory limit at runtime'.PHP_EOL;
$errorMsg .= 'using `%1$s -d memory_limit=512M` (replace 512M with the desired memory limit).'.PHP_EOL;
$errorMsg = sprintf($errorMsg, $command);
$memoryError = 'Allowed memory size of';
$errorArray = [
'type' => 42,
'message' => 'Some random dummy string to take up memory and take up some more memory and some more',
'file' => 'Another random string, which would be a filename this time. Should be relatively long to allow for deeply nested files',
'line' => 31427,
];
register_shutdown_function(
static function () use (
$errorMsg,
$memoryError,
$errorArray
) {
$errorArray = error_get_last();
if (is_array($errorArray) === true && strpos($errorArray['message'], $memoryError) !== false) {
echo $errorMsg;
}
}
);
}//end registerOutOfMemoryShutdownMessage()
}//end class