@@ -50,7 +50,7 @@ |
||
| 50 | 50 | do { |
| 51 | 51 | $modified[$path] = true; |
| 52 | 52 | $path = dirname($path); |
| 53 | - } while ($path !== $basedir); |
|
| 53 | + }while ($path !== $basedir); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | return $modified; |
@@ -15,52 +15,52 @@ |
||
| 15 | 15 | { |
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Get a list of blacklisted file paths. |
|
| 20 | - * |
|
| 21 | - * @return array |
|
| 22 | - */ |
|
| 23 | - protected function getBlacklist() |
|
| 24 | - { |
|
| 25 | - return []; |
|
| 26 | - |
|
| 27 | - }//end getBlacklist() |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Get a list of whitelisted file paths. |
|
| 32 | - * |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - protected function getWhitelist() |
|
| 36 | - { |
|
| 37 | - $modified = []; |
|
| 38 | - |
|
| 39 | - $cmd = 'git ls-files -o -m --exclude-standard -- '.escapeshellarg($this->basedir); |
|
| 40 | - $output = []; |
|
| 41 | - exec($cmd, $output); |
|
| 42 | - |
|
| 43 | - $basedir = $this->basedir; |
|
| 44 | - if (is_dir($basedir) === false) { |
|
| 45 | - $basedir = dirname($basedir); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - foreach ($output as $path) { |
|
| 49 | - $path = Util\Common::realpath($path); |
|
| 50 | - |
|
| 51 | - if ($path === false) { |
|
| 52 | - continue; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - do { |
|
| 56 | - $modified[$path] = true; |
|
| 57 | - $path = dirname($path); |
|
| 58 | - } while ($path !== $basedir); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return $modified; |
|
| 62 | - |
|
| 63 | - }//end getWhitelist() |
|
| 18 | + /** |
|
| 19 | + * Get a list of blacklisted file paths. |
|
| 20 | + * |
|
| 21 | + * @return array |
|
| 22 | + */ |
|
| 23 | + protected function getBlacklist() |
|
| 24 | + { |
|
| 25 | + return []; |
|
| 26 | + |
|
| 27 | + }//end getBlacklist() |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Get a list of whitelisted file paths. |
|
| 32 | + * |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + protected function getWhitelist() |
|
| 36 | + { |
|
| 37 | + $modified = []; |
|
| 38 | + |
|
| 39 | + $cmd = 'git ls-files -o -m --exclude-standard -- '.escapeshellarg($this->basedir); |
|
| 40 | + $output = []; |
|
| 41 | + exec($cmd, $output); |
|
| 42 | + |
|
| 43 | + $basedir = $this->basedir; |
|
| 44 | + if (is_dir($basedir) === false) { |
|
| 45 | + $basedir = dirname($basedir); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + foreach ($output as $path) { |
|
| 49 | + $path = Util\Common::realpath($path); |
|
| 50 | + |
|
| 51 | + if ($path === false) { |
|
| 52 | + continue; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + do { |
|
| 56 | + $modified[$path] = true; |
|
| 57 | + $path = dirname($path); |
|
| 58 | + } while ($path !== $basedir); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return $modified; |
|
| 62 | + |
|
| 63 | + }//end getWhitelist() |
|
| 64 | 64 | |
| 65 | 65 | |
| 66 | 66 | }//end class |
@@ -16,93 +16,93 @@ |
||
| 16 | 16 | abstract class ExactMatch extends Filter |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of files to exclude. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - private $blacklist = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A list of files to include. |
|
| 28 | - * |
|
| 29 | - * If the whitelist is empty, only files in the blacklist will be excluded. |
|
| 30 | - * |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $whitelist = null; |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Check whether the current element of the iterator is acceptable. |
|
| 38 | - * |
|
| 39 | - * If a file is both blacklisted and whitelisted, it will be deemed unacceptable. |
|
| 40 | - * |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - public function accept() |
|
| 44 | - { |
|
| 45 | - if (parent::accept() === false) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - if ($this->blacklist === null) { |
|
| 50 | - $this->blacklist = $this->getblacklist(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - if ($this->whitelist === null) { |
|
| 54 | - $this->whitelist = $this->getwhitelist(); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $filePath = Util\Common::realpath($this->current()); |
|
| 58 | - |
|
| 59 | - // If file is both blacklisted and whitelisted, the blacklist takes precedence. |
|
| 60 | - if (isset($this->blacklist[$filePath]) === true) { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - if (empty($this->whitelist) === true && empty($this->blacklist) === false) { |
|
| 65 | - // We are only checking a blacklist, so everything else should be whitelisted. |
|
| 66 | - return true; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return isset($this->whitelist[$filePath]); |
|
| 70 | - |
|
| 71 | - }//end accept() |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Returns an iterator for the current entry. |
|
| 76 | - * |
|
| 77 | - * Ensures that the blacklist and whitelist are preserved so they don't have |
|
| 78 | - * to be generated each time. |
|
| 79 | - * |
|
| 80 | - * @return \RecursiveIterator |
|
| 81 | - */ |
|
| 82 | - public function getChildren() |
|
| 83 | - { |
|
| 84 | - $children = parent::getChildren(); |
|
| 85 | - $children->blacklist = $this->blacklist; |
|
| 86 | - $children->whitelist = $this->whitelist; |
|
| 87 | - return $children; |
|
| 88 | - |
|
| 89 | - }//end getChildren() |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Get a list of blacklisted file paths. |
|
| 94 | - * |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - abstract protected function getBlacklist(); |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get a list of whitelisted file paths. |
|
| 102 | - * |
|
| 103 | - * @return array |
|
| 104 | - */ |
|
| 105 | - abstract protected function getWhitelist(); |
|
| 19 | + /** |
|
| 20 | + * A list of files to exclude. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + private $blacklist = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A list of files to include. |
|
| 28 | + * |
|
| 29 | + * If the whitelist is empty, only files in the blacklist will be excluded. |
|
| 30 | + * |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $whitelist = null; |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Check whether the current element of the iterator is acceptable. |
|
| 38 | + * |
|
| 39 | + * If a file is both blacklisted and whitelisted, it will be deemed unacceptable. |
|
| 40 | + * |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + public function accept() |
|
| 44 | + { |
|
| 45 | + if (parent::accept() === false) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + if ($this->blacklist === null) { |
|
| 50 | + $this->blacklist = $this->getblacklist(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + if ($this->whitelist === null) { |
|
| 54 | + $this->whitelist = $this->getwhitelist(); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $filePath = Util\Common::realpath($this->current()); |
|
| 58 | + |
|
| 59 | + // If file is both blacklisted and whitelisted, the blacklist takes precedence. |
|
| 60 | + if (isset($this->blacklist[$filePath]) === true) { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + if (empty($this->whitelist) === true && empty($this->blacklist) === false) { |
|
| 65 | + // We are only checking a blacklist, so everything else should be whitelisted. |
|
| 66 | + return true; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return isset($this->whitelist[$filePath]); |
|
| 70 | + |
|
| 71 | + }//end accept() |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Returns an iterator for the current entry. |
|
| 76 | + * |
|
| 77 | + * Ensures that the blacklist and whitelist are preserved so they don't have |
|
| 78 | + * to be generated each time. |
|
| 79 | + * |
|
| 80 | + * @return \RecursiveIterator |
|
| 81 | + */ |
|
| 82 | + public function getChildren() |
|
| 83 | + { |
|
| 84 | + $children = parent::getChildren(); |
|
| 85 | + $children->blacklist = $this->blacklist; |
|
| 86 | + $children->whitelist = $this->whitelist; |
|
| 87 | + return $children; |
|
| 88 | + |
|
| 89 | + }//end getChildren() |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Get a list of blacklisted file paths. |
|
| 94 | + * |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + abstract protected function getBlacklist(); |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get a list of whitelisted file paths. |
|
| 102 | + * |
|
| 103 | + * @return array |
|
| 104 | + */ |
|
| 105 | + abstract protected function getWhitelist(); |
|
| 106 | 106 | |
| 107 | 107 | |
| 108 | 108 | }//end class |
@@ -632,19 +632,19 @@ |
||
| 632 | 632 | $input = trim($input); |
| 633 | 633 | |
| 634 | 634 | switch ($input) { |
| 635 | - case 's': |
|
| 636 | - break(2); |
|
| 637 | - case 'q': |
|
| 638 | - throw new DeepExitException('', 0); |
|
| 639 | - default: |
|
| 640 | - // Repopulate the sniffs because some of them save their state |
|
| 641 | - // and only clear it when the file changes, but we are rechecking |
|
| 642 | - // the same file. |
|
| 643 | - $file->ruleset->populateTokenListeners(); |
|
| 644 | - $file->reloadContent(); |
|
| 645 | - $file->process(); |
|
| 646 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 647 | - break; |
|
| 635 | + case 's': |
|
| 636 | + break(2); |
|
| 637 | + case 'q': |
|
| 638 | + throw new DeepExitException('', 0); |
|
| 639 | + default: |
|
| 640 | + // Repopulate the sniffs because some of them save their state |
|
| 641 | + // and only clear it when the file changes, but we are rechecking |
|
| 642 | + // the same file. |
|
| 643 | + $file->ruleset->populateTokenListeners(); |
|
| 644 | + $file->reloadContent(); |
|
| 645 | + $file->process(); |
|
| 646 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 647 | + break; |
|
| 648 | 648 | } |
| 649 | 649 | }//end while |
| 650 | 650 | }//end if |
@@ -248,7 +248,7 @@ |
||
| 248 | 248 | 'xmlwriter', |
| 249 | 249 | 'SimpleXML', |
| 250 | 250 | ]; |
| 251 | - $missingExtensions = []; |
|
| 251 | + $missingExtensions = []; |
|
| 252 | 252 | |
| 253 | 253 | foreach ($requiredExtensions as $extension) { |
| 254 | 254 | if (extension_loaded($extension) === false) { |
@@ -24,866 +24,866 @@ |
||
| 24 | 24 | class Runner |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * The config data for the run. |
|
| 29 | - * |
|
| 30 | - * @var \PHP_CodeSniffer\Config |
|
| 31 | - */ |
|
| 32 | - public $config = null; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The ruleset used for the run. |
|
| 36 | - * |
|
| 37 | - * @var \PHP_CodeSniffer\Ruleset |
|
| 38 | - */ |
|
| 39 | - public $ruleset = null; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The reporter used for generating reports after the run. |
|
| 43 | - * |
|
| 44 | - * @var \PHP_CodeSniffer\Reporter |
|
| 45 | - */ |
|
| 46 | - public $reporter = null; |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Run the PHPCS script. |
|
| 51 | - * |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function runPHPCS() |
|
| 55 | - { |
|
| 56 | - try { |
|
| 57 | - Util\Timing::startTiming(); |
|
| 58 | - Runner::checkRequirements(); |
|
| 59 | - |
|
| 60 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 61 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Creating the Config object populates it with all required settings |
|
| 65 | - // based on the CLI arguments provided to the script and any config |
|
| 66 | - // values the user has set. |
|
| 67 | - $this->config = new Config(); |
|
| 68 | - |
|
| 69 | - // Init the run and load the rulesets to set additional config vars. |
|
| 70 | - $this->init(); |
|
| 71 | - |
|
| 72 | - // Print a list of sniffs in each of the supplied standards. |
|
| 73 | - // We fudge the config here so that each standard is explained in isolation. |
|
| 74 | - if ($this->config->explain === true) { |
|
| 75 | - $standards = $this->config->standards; |
|
| 76 | - foreach ($standards as $standard) { |
|
| 77 | - $this->config->standards = [$standard]; |
|
| 78 | - $ruleset = new Ruleset($this->config); |
|
| 79 | - $ruleset->explain(); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return 0; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // Generate documentation for each of the supplied standards. |
|
| 86 | - if ($this->config->generator !== null) { |
|
| 87 | - $standards = $this->config->standards; |
|
| 88 | - foreach ($standards as $standard) { |
|
| 89 | - $this->config->standards = [$standard]; |
|
| 90 | - $ruleset = new Ruleset($this->config); |
|
| 91 | - $class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator; |
|
| 92 | - $generator = new $class($ruleset); |
|
| 93 | - $generator->generate(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return 0; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Other report formats don't really make sense in interactive mode |
|
| 100 | - // so we hard-code the full report here and when outputting. |
|
| 101 | - // We also ensure parallel processing is off because we need to do one file at a time. |
|
| 102 | - if ($this->config->interactive === true) { |
|
| 103 | - $this->config->reports = ['full' => null]; |
|
| 104 | - $this->config->parallel = 1; |
|
| 105 | - $this->config->showProgress = false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Disable caching if we are processing STDIN as we can't be 100% |
|
| 109 | - // sure where the file came from or if it will change in the future. |
|
| 110 | - if ($this->config->stdin === true) { |
|
| 111 | - $this->config->cache = false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $numErrors = $this->run(); |
|
| 115 | - |
|
| 116 | - // Print all the reports for this run. |
|
| 117 | - $toScreen = $this->reporter->printReports(); |
|
| 118 | - |
|
| 119 | - // Only print timer output if no reports were |
|
| 120 | - // printed to the screen so we don't put additional output |
|
| 121 | - // in something like an XML report. If we are printing to screen, |
|
| 122 | - // the report types would have already worked out who should |
|
| 123 | - // print the timer info. |
|
| 124 | - if ($this->config->interactive === false |
|
| 125 | - && ($toScreen === false |
|
| 126 | - || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true)) |
|
| 127 | - ) { |
|
| 128 | - Util\Timing::printRunTime(); |
|
| 129 | - } |
|
| 130 | - } catch (DeepExitException $e) { |
|
| 131 | - echo $e->getMessage(); |
|
| 132 | - return $e->getCode(); |
|
| 133 | - }//end try |
|
| 134 | - |
|
| 135 | - if ($numErrors === 0) { |
|
| 136 | - // No errors found. |
|
| 137 | - return 0; |
|
| 138 | - } else if ($this->reporter->totalFixable === 0) { |
|
| 139 | - // Errors found, but none of them can be fixed by PHPCBF. |
|
| 140 | - return 1; |
|
| 141 | - } else { |
|
| 142 | - // Errors found, and some can be fixed by PHPCBF. |
|
| 143 | - return 2; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - }//end runPHPCS() |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Run the PHPCBF script. |
|
| 151 | - * |
|
| 152 | - * @return array |
|
| 153 | - */ |
|
| 154 | - public function runPHPCBF() |
|
| 155 | - { |
|
| 156 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 157 | - define('PHP_CODESNIFFER_CBF', true); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - try { |
|
| 161 | - Util\Timing::startTiming(); |
|
| 162 | - Runner::checkRequirements(); |
|
| 163 | - |
|
| 164 | - // Creating the Config object populates it with all required settings |
|
| 165 | - // based on the CLI arguments provided to the script and any config |
|
| 166 | - // values the user has set. |
|
| 167 | - $this->config = new Config(); |
|
| 168 | - |
|
| 169 | - // When processing STDIN, we can't output anything to the screen |
|
| 170 | - // or it will end up mixed in with the file output. |
|
| 171 | - if ($this->config->stdin === true) { |
|
| 172 | - $this->config->verbosity = 0; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - // Init the run and load the rulesets to set additional config vars. |
|
| 176 | - $this->init(); |
|
| 177 | - |
|
| 178 | - // When processing STDIN, we only process one file at a time and |
|
| 179 | - // we don't process all the way through, so we can't use the parallel |
|
| 180 | - // running system. |
|
| 181 | - if ($this->config->stdin === true) { |
|
| 182 | - $this->config->parallel = 1; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Override some of the command line settings that might break the fixes. |
|
| 186 | - $this->config->generator = null; |
|
| 187 | - $this->config->explain = false; |
|
| 188 | - $this->config->interactive = false; |
|
| 189 | - $this->config->cache = false; |
|
| 190 | - $this->config->showSources = false; |
|
| 191 | - $this->config->recordErrors = false; |
|
| 192 | - $this->config->reportFile = null; |
|
| 193 | - $this->config->reports = ['cbf' => null]; |
|
| 194 | - |
|
| 195 | - // If a standard tries to set command line arguments itself, some |
|
| 196 | - // may be blocked because PHPCBF is running, so stop the script |
|
| 197 | - // dying if any are found. |
|
| 198 | - $this->config->dieOnUnknownArg = false; |
|
| 199 | - |
|
| 200 | - $this->run(); |
|
| 201 | - $this->reporter->printReports(); |
|
| 202 | - |
|
| 203 | - echo PHP_EOL; |
|
| 204 | - Util\Timing::printRunTime(); |
|
| 205 | - } catch (DeepExitException $e) { |
|
| 206 | - echo $e->getMessage(); |
|
| 207 | - return $e->getCode(); |
|
| 208 | - }//end try |
|
| 209 | - |
|
| 210 | - if ($this->reporter->totalFixed === 0) { |
|
| 211 | - // Nothing was fixed by PHPCBF. |
|
| 212 | - if ($this->reporter->totalFixable === 0) { |
|
| 213 | - // Nothing found that could be fixed. |
|
| 214 | - return 0; |
|
| 215 | - } else { |
|
| 216 | - // Something failed to fix. |
|
| 217 | - return 2; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if ($this->reporter->totalFixable === 0) { |
|
| 222 | - // PHPCBF fixed all fixable errors. |
|
| 223 | - return 1; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // PHPCBF fixed some fixable errors, but others failed to fix. |
|
| 227 | - return 2; |
|
| 228 | - |
|
| 229 | - }//end runPHPCBF() |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Exits if the minimum requirements of PHP_CodeSniffer are not met. |
|
| 234 | - * |
|
| 235 | - * @return void |
|
| 236 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the requirements are not met. |
|
| 237 | - */ |
|
| 238 | - public function checkRequirements() |
|
| 239 | - { |
|
| 240 | - // Check the PHP version. |
|
| 241 | - if (PHP_VERSION_ID < 50400) { |
|
| 242 | - $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
|
| 243 | - throw new DeepExitException($error, 3); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - $requiredExtensions = [ |
|
| 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); |
|
| 274 | - throw new DeepExitException($error, 3); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - }//end checkRequirements() |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Init the rulesets and other high-level settings. |
|
| 282 | - * |
|
| 283 | - * @return void |
|
| 284 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If a referenced standard is not installed. |
|
| 285 | - */ |
|
| 286 | - public function init() |
|
| 287 | - { |
|
| 288 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 289 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - // Ensure this option is enabled or else line endings will not always |
|
| 293 | - // be detected properly for files created on a Mac with the /r line ending. |
|
| 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); |
|
| 298 | - |
|
| 299 | - // Check that the standards are valid. |
|
| 300 | - foreach ($this->config->standards as $standard) { |
|
| 301 | - if (Util\Standards::isInstalledStandard($standard) === false) { |
|
| 302 | - // They didn't select a valid coding standard, so help them |
|
| 303 | - // out by letting them know which standards are installed. |
|
| 304 | - $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; |
|
| 305 | - ob_start(); |
|
| 306 | - Util\Standards::printInstalledStandards(); |
|
| 307 | - $error .= ob_get_contents(); |
|
| 308 | - ob_end_clean(); |
|
| 309 | - throw new DeepExitException($error, 3); |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - // Saves passing the Config object into other objects that only need |
|
| 314 | - // the verbosity flag for debug output. |
|
| 315 | - if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
| 316 | - define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // Create this class so it is autoloaded and sets up a bunch |
|
| 320 | - // of PHP_CodeSniffer-specific token type constants. |
|
| 321 | - $tokens = new Util\Tokens(); |
|
| 322 | - |
|
| 323 | - // Allow autoloading of custom files inside installed standards. |
|
| 324 | - $installedStandards = Standards::getInstalledStandardDetails(); |
|
| 325 | - foreach ($installedStandards as $name => $details) { |
|
| 326 | - Autoload::addSearchPath($details['path'], $details['namespace']); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - // The ruleset contains all the information about how the files |
|
| 330 | - // should be checked and/or fixed. |
|
| 331 | - try { |
|
| 332 | - $this->ruleset = new Ruleset($this->config); |
|
| 333 | - } catch (RuntimeException $e) { |
|
| 334 | - $error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL; |
|
| 335 | - $error .= $this->config->printShortUsage(true); |
|
| 336 | - throw new DeepExitException($error, 3); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - }//end init() |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Performs the run. |
|
| 344 | - * |
|
| 345 | - * @return int The number of errors and warnings found. |
|
| 346 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 347 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 348 | - */ |
|
| 349 | - private function run() |
|
| 350 | - { |
|
| 351 | - // The class that manages all reporters for the run. |
|
| 352 | - $this->reporter = new Reporter($this->config); |
|
| 353 | - |
|
| 354 | - // Include bootstrap files. |
|
| 355 | - foreach ($this->config->bootstrap as $bootstrap) { |
|
| 356 | - include $bootstrap; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - if ($this->config->stdin === true) { |
|
| 360 | - $fileContents = $this->config->stdinContent; |
|
| 361 | - if ($fileContents === null) { |
|
| 362 | - $handle = fopen('php://stdin', 'r'); |
|
| 363 | - stream_set_blocking($handle, true); |
|
| 364 | - $fileContents = stream_get_contents($handle); |
|
| 365 | - fclose($handle); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 369 | - $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
|
| 370 | - $todo->addFile($dummy->path, $dummy); |
|
| 371 | - } else { |
|
| 372 | - if (empty($this->config->files) === true) { |
|
| 373 | - $error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
|
| 374 | - $error .= $this->config->printShortUsage(true); |
|
| 375 | - throw new DeepExitException($error, 3); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 379 | - echo 'Creating file list... '; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 383 | - |
|
| 384 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 385 | - $numFiles = count($todo); |
|
| 386 | - echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if ($this->config->cache === true) { |
|
| 390 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 391 | - echo 'Loading cache... '; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - Cache::load($this->ruleset, $this->config); |
|
| 395 | - |
|
| 396 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 397 | - $size = Cache::getSize(); |
|
| 398 | - echo "DONE ($size files in cache)".PHP_EOL; |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - }//end if |
|
| 402 | - |
|
| 403 | - // Turn all sniff errors into exceptions. |
|
| 404 | - set_error_handler([$this, 'handleErrors']); |
|
| 405 | - |
|
| 406 | - // If verbosity is too high, turn off parallelism so the |
|
| 407 | - // debug output is clean. |
|
| 408 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 409 | - $this->config->parallel = 1; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // If the PCNTL extension isn't installed, we can't fork. |
|
| 413 | - if (function_exists('pcntl_fork') === false) { |
|
| 414 | - $this->config->parallel = 1; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - $lastDir = ''; |
|
| 418 | - $numFiles = count($todo); |
|
| 419 | - |
|
| 420 | - if ($this->config->parallel === 1) { |
|
| 421 | - // Running normally. |
|
| 422 | - $numProcessed = 0; |
|
| 423 | - foreach ($todo as $path => $file) { |
|
| 424 | - if ($file->ignored === false) { |
|
| 425 | - $currDir = dirname($path); |
|
| 426 | - if ($lastDir !== $currDir) { |
|
| 427 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 428 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - $lastDir = $currDir; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $this->processFile($file); |
|
| 435 | - } else if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 436 | - echo 'Skipping '.basename($file->path).PHP_EOL; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - $numProcessed++; |
|
| 440 | - $this->printProgress($file, $numFiles, $numProcessed); |
|
| 441 | - } |
|
| 442 | - } else { |
|
| 443 | - // Batching and forking. |
|
| 444 | - $childProcs = []; |
|
| 445 | - $numPerBatch = ceil($numFiles / $this->config->parallel); |
|
| 446 | - |
|
| 447 | - for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
|
| 448 | - $startAt = ($batch * $numPerBatch); |
|
| 449 | - if ($startAt >= $numFiles) { |
|
| 450 | - break; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - $endAt = ($startAt + $numPerBatch); |
|
| 454 | - if ($endAt > $numFiles) { |
|
| 455 | - $endAt = $numFiles; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
|
| 459 | - $pid = pcntl_fork(); |
|
| 460 | - if ($pid === -1) { |
|
| 461 | - throw new RuntimeException('Failed to create child process'); |
|
| 462 | - } else if ($pid !== 0) { |
|
| 463 | - $childProcs[$pid] = $childOutFilename; |
|
| 464 | - } else { |
|
| 465 | - // Move forward to the start of the batch. |
|
| 466 | - $todo->rewind(); |
|
| 467 | - for ($i = 0; $i < $startAt; $i++) { |
|
| 468 | - $todo->next(); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - // Reset the reporter to make sure only figures from this |
|
| 472 | - // file batch are recorded. |
|
| 473 | - $this->reporter->totalFiles = 0; |
|
| 474 | - $this->reporter->totalErrors = 0; |
|
| 475 | - $this->reporter->totalWarnings = 0; |
|
| 476 | - $this->reporter->totalFixable = 0; |
|
| 477 | - $this->reporter->totalFixed = 0; |
|
| 478 | - |
|
| 479 | - // Process the files. |
|
| 480 | - $pathsProcessed = []; |
|
| 481 | - ob_start(); |
|
| 482 | - for ($i = $startAt; $i < $endAt; $i++) { |
|
| 483 | - $path = $todo->key(); |
|
| 484 | - $file = $todo->current(); |
|
| 485 | - |
|
| 486 | - if ($file->ignored === true) { |
|
| 487 | - $todo->next(); |
|
| 488 | - continue; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - $currDir = dirname($path); |
|
| 492 | - if ($lastDir !== $currDir) { |
|
| 493 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 494 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $lastDir = $currDir; |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - $this->processFile($file); |
|
| 501 | - |
|
| 502 | - $pathsProcessed[] = $path; |
|
| 503 | - $todo->next(); |
|
| 504 | - }//end for |
|
| 505 | - |
|
| 506 | - $debugOutput = ob_get_contents(); |
|
| 507 | - ob_end_clean(); |
|
| 508 | - |
|
| 509 | - // Write information about the run to the filesystem |
|
| 510 | - // so it can be picked up by the main process. |
|
| 511 | - $childOutput = [ |
|
| 512 | - 'totalFiles' => $this->reporter->totalFiles, |
|
| 513 | - 'totalErrors' => $this->reporter->totalErrors, |
|
| 514 | - 'totalWarnings' => $this->reporter->totalWarnings, |
|
| 515 | - 'totalFixable' => $this->reporter->totalFixable, |
|
| 516 | - 'totalFixed' => $this->reporter->totalFixed, |
|
| 517 | - ]; |
|
| 518 | - |
|
| 519 | - $output = '<'.'?php'."\n".' $childOutput = '; |
|
| 520 | - $output .= var_export($childOutput, true); |
|
| 521 | - $output .= ";\n\$debugOutput = "; |
|
| 522 | - $output .= var_export($debugOutput, true); |
|
| 523 | - |
|
| 524 | - if ($this->config->cache === true) { |
|
| 525 | - $childCache = []; |
|
| 526 | - foreach ($pathsProcessed as $path) { |
|
| 527 | - $childCache[$path] = Cache::get($path); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - $output .= ";\n\$childCache = "; |
|
| 531 | - $output .= var_export($childCache, true); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - $output .= ";\n?".'>'; |
|
| 535 | - file_put_contents($childOutFilename, $output); |
|
| 536 | - exit(); |
|
| 537 | - }//end if |
|
| 538 | - }//end for |
|
| 539 | - |
|
| 540 | - $success = $this->processChildProcs($childProcs); |
|
| 541 | - if ($success === false) { |
|
| 542 | - throw new RuntimeException('One or more child processes failed to run'); |
|
| 543 | - } |
|
| 544 | - }//end if |
|
| 545 | - |
|
| 546 | - restore_error_handler(); |
|
| 547 | - |
|
| 548 | - if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 549 | - && $this->config->interactive === false |
|
| 550 | - && $this->config->showProgress === true |
|
| 551 | - ) { |
|
| 552 | - echo PHP_EOL.PHP_EOL; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - if ($this->config->cache === true) { |
|
| 556 | - Cache::save(); |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
|
| 560 | - $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
|
| 561 | - |
|
| 562 | - $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
|
| 563 | - if ($ignoreErrors !== null) { |
|
| 564 | - $ignoreErrors = (bool) $ignoreErrors; |
|
| 565 | - if ($ignoreErrors === true) { |
|
| 566 | - $return -= $this->reporter->totalErrors; |
|
| 567 | - } |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - if ($ignoreWarnings !== null) { |
|
| 571 | - $ignoreWarnings = (bool) $ignoreWarnings; |
|
| 572 | - if ($ignoreWarnings === true) { |
|
| 573 | - $return -= $this->reporter->totalWarnings; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - return $return; |
|
| 578 | - |
|
| 579 | - }//end run() |
|
| 580 | - |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * Converts all PHP errors into exceptions. |
|
| 584 | - * |
|
| 585 | - * This method forces a sniff to stop processing if it is not |
|
| 586 | - * able to handle a specific piece of code, instead of continuing |
|
| 587 | - * and potentially getting into a loop. |
|
| 588 | - * |
|
| 589 | - * @param int $code The level of error raised. |
|
| 590 | - * @param string $message The error message. |
|
| 591 | - * @param string $file The path of the file that raised the error. |
|
| 592 | - * @param int $line The line number the error was raised at. |
|
| 593 | - * |
|
| 594 | - * @return void |
|
| 595 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 596 | - */ |
|
| 597 | - public function handleErrors($code, $message, $file, $line) |
|
| 598 | - { |
|
| 599 | - if ((error_reporting() & $code) === 0) { |
|
| 600 | - // This type of error is being muted. |
|
| 601 | - return true; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - throw new RuntimeException("$message in $file on line $line"); |
|
| 605 | - |
|
| 606 | - }//end handleErrors() |
|
| 607 | - |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Processes a single file, including checking and fixing. |
|
| 611 | - * |
|
| 612 | - * @param \PHP_CodeSniffer\Files\File $file The file to be processed. |
|
| 613 | - * |
|
| 614 | - * @return void |
|
| 615 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 616 | - */ |
|
| 617 | - public function processFile($file) |
|
| 618 | - { |
|
| 619 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 620 | - $startTime = microtime(true); |
|
| 621 | - echo 'Processing '.basename($file->path).' '; |
|
| 622 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 623 | - echo PHP_EOL; |
|
| 624 | - } |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - try { |
|
| 628 | - $file->process(); |
|
| 629 | - |
|
| 630 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 631 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 632 | - if ($timeTaken < 1000) { |
|
| 633 | - $timeTaken = round($timeTaken); |
|
| 634 | - echo "DONE in {$timeTaken}ms"; |
|
| 635 | - } else { |
|
| 636 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
| 637 | - echo "DONE in $timeTaken secs"; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 641 | - $errors = $file->getFixableCount(); |
|
| 642 | - echo " ($errors fixable violations)".PHP_EOL; |
|
| 643 | - } else { |
|
| 644 | - $errors = $file->getErrorCount(); |
|
| 645 | - $warnings = $file->getWarningCount(); |
|
| 646 | - echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
| 647 | - } |
|
| 648 | - } |
|
| 649 | - } catch (\Exception $e) { |
|
| 650 | - $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
|
| 651 | - $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
|
| 652 | - }//end try |
|
| 653 | - |
|
| 654 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 655 | - |
|
| 656 | - if ($this->config->interactive === true) { |
|
| 657 | - /* |
|
| 27 | + /** |
|
| 28 | + * The config data for the run. |
|
| 29 | + * |
|
| 30 | + * @var \PHP_CodeSniffer\Config |
|
| 31 | + */ |
|
| 32 | + public $config = null; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The ruleset used for the run. |
|
| 36 | + * |
|
| 37 | + * @var \PHP_CodeSniffer\Ruleset |
|
| 38 | + */ |
|
| 39 | + public $ruleset = null; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The reporter used for generating reports after the run. |
|
| 43 | + * |
|
| 44 | + * @var \PHP_CodeSniffer\Reporter |
|
| 45 | + */ |
|
| 46 | + public $reporter = null; |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Run the PHPCS script. |
|
| 51 | + * |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function runPHPCS() |
|
| 55 | + { |
|
| 56 | + try { |
|
| 57 | + Util\Timing::startTiming(); |
|
| 58 | + Runner::checkRequirements(); |
|
| 59 | + |
|
| 60 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 61 | + define('PHP_CODESNIFFER_CBF', false); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Creating the Config object populates it with all required settings |
|
| 65 | + // based on the CLI arguments provided to the script and any config |
|
| 66 | + // values the user has set. |
|
| 67 | + $this->config = new Config(); |
|
| 68 | + |
|
| 69 | + // Init the run and load the rulesets to set additional config vars. |
|
| 70 | + $this->init(); |
|
| 71 | + |
|
| 72 | + // Print a list of sniffs in each of the supplied standards. |
|
| 73 | + // We fudge the config here so that each standard is explained in isolation. |
|
| 74 | + if ($this->config->explain === true) { |
|
| 75 | + $standards = $this->config->standards; |
|
| 76 | + foreach ($standards as $standard) { |
|
| 77 | + $this->config->standards = [$standard]; |
|
| 78 | + $ruleset = new Ruleset($this->config); |
|
| 79 | + $ruleset->explain(); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return 0; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // Generate documentation for each of the supplied standards. |
|
| 86 | + if ($this->config->generator !== null) { |
|
| 87 | + $standards = $this->config->standards; |
|
| 88 | + foreach ($standards as $standard) { |
|
| 89 | + $this->config->standards = [$standard]; |
|
| 90 | + $ruleset = new Ruleset($this->config); |
|
| 91 | + $class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator; |
|
| 92 | + $generator = new $class($ruleset); |
|
| 93 | + $generator->generate(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return 0; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Other report formats don't really make sense in interactive mode |
|
| 100 | + // so we hard-code the full report here and when outputting. |
|
| 101 | + // We also ensure parallel processing is off because we need to do one file at a time. |
|
| 102 | + if ($this->config->interactive === true) { |
|
| 103 | + $this->config->reports = ['full' => null]; |
|
| 104 | + $this->config->parallel = 1; |
|
| 105 | + $this->config->showProgress = false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Disable caching if we are processing STDIN as we can't be 100% |
|
| 109 | + // sure where the file came from or if it will change in the future. |
|
| 110 | + if ($this->config->stdin === true) { |
|
| 111 | + $this->config->cache = false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $numErrors = $this->run(); |
|
| 115 | + |
|
| 116 | + // Print all the reports for this run. |
|
| 117 | + $toScreen = $this->reporter->printReports(); |
|
| 118 | + |
|
| 119 | + // Only print timer output if no reports were |
|
| 120 | + // printed to the screen so we don't put additional output |
|
| 121 | + // in something like an XML report. If we are printing to screen, |
|
| 122 | + // the report types would have already worked out who should |
|
| 123 | + // print the timer info. |
|
| 124 | + if ($this->config->interactive === false |
|
| 125 | + && ($toScreen === false |
|
| 126 | + || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true)) |
|
| 127 | + ) { |
|
| 128 | + Util\Timing::printRunTime(); |
|
| 129 | + } |
|
| 130 | + } catch (DeepExitException $e) { |
|
| 131 | + echo $e->getMessage(); |
|
| 132 | + return $e->getCode(); |
|
| 133 | + }//end try |
|
| 134 | + |
|
| 135 | + if ($numErrors === 0) { |
|
| 136 | + // No errors found. |
|
| 137 | + return 0; |
|
| 138 | + } else if ($this->reporter->totalFixable === 0) { |
|
| 139 | + // Errors found, but none of them can be fixed by PHPCBF. |
|
| 140 | + return 1; |
|
| 141 | + } else { |
|
| 142 | + // Errors found, and some can be fixed by PHPCBF. |
|
| 143 | + return 2; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + }//end runPHPCS() |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Run the PHPCBF script. |
|
| 151 | + * |
|
| 152 | + * @return array |
|
| 153 | + */ |
|
| 154 | + public function runPHPCBF() |
|
| 155 | + { |
|
| 156 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 157 | + define('PHP_CODESNIFFER_CBF', true); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + try { |
|
| 161 | + Util\Timing::startTiming(); |
|
| 162 | + Runner::checkRequirements(); |
|
| 163 | + |
|
| 164 | + // Creating the Config object populates it with all required settings |
|
| 165 | + // based on the CLI arguments provided to the script and any config |
|
| 166 | + // values the user has set. |
|
| 167 | + $this->config = new Config(); |
|
| 168 | + |
|
| 169 | + // When processing STDIN, we can't output anything to the screen |
|
| 170 | + // or it will end up mixed in with the file output. |
|
| 171 | + if ($this->config->stdin === true) { |
|
| 172 | + $this->config->verbosity = 0; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + // Init the run and load the rulesets to set additional config vars. |
|
| 176 | + $this->init(); |
|
| 177 | + |
|
| 178 | + // When processing STDIN, we only process one file at a time and |
|
| 179 | + // we don't process all the way through, so we can't use the parallel |
|
| 180 | + // running system. |
|
| 181 | + if ($this->config->stdin === true) { |
|
| 182 | + $this->config->parallel = 1; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Override some of the command line settings that might break the fixes. |
|
| 186 | + $this->config->generator = null; |
|
| 187 | + $this->config->explain = false; |
|
| 188 | + $this->config->interactive = false; |
|
| 189 | + $this->config->cache = false; |
|
| 190 | + $this->config->showSources = false; |
|
| 191 | + $this->config->recordErrors = false; |
|
| 192 | + $this->config->reportFile = null; |
|
| 193 | + $this->config->reports = ['cbf' => null]; |
|
| 194 | + |
|
| 195 | + // If a standard tries to set command line arguments itself, some |
|
| 196 | + // may be blocked because PHPCBF is running, so stop the script |
|
| 197 | + // dying if any are found. |
|
| 198 | + $this->config->dieOnUnknownArg = false; |
|
| 199 | + |
|
| 200 | + $this->run(); |
|
| 201 | + $this->reporter->printReports(); |
|
| 202 | + |
|
| 203 | + echo PHP_EOL; |
|
| 204 | + Util\Timing::printRunTime(); |
|
| 205 | + } catch (DeepExitException $e) { |
|
| 206 | + echo $e->getMessage(); |
|
| 207 | + return $e->getCode(); |
|
| 208 | + }//end try |
|
| 209 | + |
|
| 210 | + if ($this->reporter->totalFixed === 0) { |
|
| 211 | + // Nothing was fixed by PHPCBF. |
|
| 212 | + if ($this->reporter->totalFixable === 0) { |
|
| 213 | + // Nothing found that could be fixed. |
|
| 214 | + return 0; |
|
| 215 | + } else { |
|
| 216 | + // Something failed to fix. |
|
| 217 | + return 2; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if ($this->reporter->totalFixable === 0) { |
|
| 222 | + // PHPCBF fixed all fixable errors. |
|
| 223 | + return 1; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // PHPCBF fixed some fixable errors, but others failed to fix. |
|
| 227 | + return 2; |
|
| 228 | + |
|
| 229 | + }//end runPHPCBF() |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Exits if the minimum requirements of PHP_CodeSniffer are not met. |
|
| 234 | + * |
|
| 235 | + * @return void |
|
| 236 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the requirements are not met. |
|
| 237 | + */ |
|
| 238 | + public function checkRequirements() |
|
| 239 | + { |
|
| 240 | + // Check the PHP version. |
|
| 241 | + if (PHP_VERSION_ID < 50400) { |
|
| 242 | + $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
|
| 243 | + throw new DeepExitException($error, 3); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $requiredExtensions = [ |
|
| 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); |
|
| 274 | + throw new DeepExitException($error, 3); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + }//end checkRequirements() |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Init the rulesets and other high-level settings. |
|
| 282 | + * |
|
| 283 | + * @return void |
|
| 284 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If a referenced standard is not installed. |
|
| 285 | + */ |
|
| 286 | + public function init() |
|
| 287 | + { |
|
| 288 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 289 | + define('PHP_CODESNIFFER_CBF', false); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + // Ensure this option is enabled or else line endings will not always |
|
| 293 | + // be detected properly for files created on a Mac with the /r line ending. |
|
| 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); |
|
| 298 | + |
|
| 299 | + // Check that the standards are valid. |
|
| 300 | + foreach ($this->config->standards as $standard) { |
|
| 301 | + if (Util\Standards::isInstalledStandard($standard) === false) { |
|
| 302 | + // They didn't select a valid coding standard, so help them |
|
| 303 | + // out by letting them know which standards are installed. |
|
| 304 | + $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; |
|
| 305 | + ob_start(); |
|
| 306 | + Util\Standards::printInstalledStandards(); |
|
| 307 | + $error .= ob_get_contents(); |
|
| 308 | + ob_end_clean(); |
|
| 309 | + throw new DeepExitException($error, 3); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + // Saves passing the Config object into other objects that only need |
|
| 314 | + // the verbosity flag for debug output. |
|
| 315 | + if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
| 316 | + define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // Create this class so it is autoloaded and sets up a bunch |
|
| 320 | + // of PHP_CodeSniffer-specific token type constants. |
|
| 321 | + $tokens = new Util\Tokens(); |
|
| 322 | + |
|
| 323 | + // Allow autoloading of custom files inside installed standards. |
|
| 324 | + $installedStandards = Standards::getInstalledStandardDetails(); |
|
| 325 | + foreach ($installedStandards as $name => $details) { |
|
| 326 | + Autoload::addSearchPath($details['path'], $details['namespace']); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + // The ruleset contains all the information about how the files |
|
| 330 | + // should be checked and/or fixed. |
|
| 331 | + try { |
|
| 332 | + $this->ruleset = new Ruleset($this->config); |
|
| 333 | + } catch (RuntimeException $e) { |
|
| 334 | + $error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL; |
|
| 335 | + $error .= $this->config->printShortUsage(true); |
|
| 336 | + throw new DeepExitException($error, 3); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + }//end init() |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Performs the run. |
|
| 344 | + * |
|
| 345 | + * @return int The number of errors and warnings found. |
|
| 346 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 347 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 348 | + */ |
|
| 349 | + private function run() |
|
| 350 | + { |
|
| 351 | + // The class that manages all reporters for the run. |
|
| 352 | + $this->reporter = new Reporter($this->config); |
|
| 353 | + |
|
| 354 | + // Include bootstrap files. |
|
| 355 | + foreach ($this->config->bootstrap as $bootstrap) { |
|
| 356 | + include $bootstrap; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + if ($this->config->stdin === true) { |
|
| 360 | + $fileContents = $this->config->stdinContent; |
|
| 361 | + if ($fileContents === null) { |
|
| 362 | + $handle = fopen('php://stdin', 'r'); |
|
| 363 | + stream_set_blocking($handle, true); |
|
| 364 | + $fileContents = stream_get_contents($handle); |
|
| 365 | + fclose($handle); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + $todo = new FileList($this->config, $this->ruleset); |
|
| 369 | + $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
|
| 370 | + $todo->addFile($dummy->path, $dummy); |
|
| 371 | + } else { |
|
| 372 | + if (empty($this->config->files) === true) { |
|
| 373 | + $error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
|
| 374 | + $error .= $this->config->printShortUsage(true); |
|
| 375 | + throw new DeepExitException($error, 3); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 379 | + echo 'Creating file list... '; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + $todo = new FileList($this->config, $this->ruleset); |
|
| 383 | + |
|
| 384 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 385 | + $numFiles = count($todo); |
|
| 386 | + echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if ($this->config->cache === true) { |
|
| 390 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 391 | + echo 'Loading cache... '; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + Cache::load($this->ruleset, $this->config); |
|
| 395 | + |
|
| 396 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 397 | + $size = Cache::getSize(); |
|
| 398 | + echo "DONE ($size files in cache)".PHP_EOL; |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + }//end if |
|
| 402 | + |
|
| 403 | + // Turn all sniff errors into exceptions. |
|
| 404 | + set_error_handler([$this, 'handleErrors']); |
|
| 405 | + |
|
| 406 | + // If verbosity is too high, turn off parallelism so the |
|
| 407 | + // debug output is clean. |
|
| 408 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 409 | + $this->config->parallel = 1; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // If the PCNTL extension isn't installed, we can't fork. |
|
| 413 | + if (function_exists('pcntl_fork') === false) { |
|
| 414 | + $this->config->parallel = 1; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + $lastDir = ''; |
|
| 418 | + $numFiles = count($todo); |
|
| 419 | + |
|
| 420 | + if ($this->config->parallel === 1) { |
|
| 421 | + // Running normally. |
|
| 422 | + $numProcessed = 0; |
|
| 423 | + foreach ($todo as $path => $file) { |
|
| 424 | + if ($file->ignored === false) { |
|
| 425 | + $currDir = dirname($path); |
|
| 426 | + if ($lastDir !== $currDir) { |
|
| 427 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 428 | + echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + $lastDir = $currDir; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $this->processFile($file); |
|
| 435 | + } else if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 436 | + echo 'Skipping '.basename($file->path).PHP_EOL; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + $numProcessed++; |
|
| 440 | + $this->printProgress($file, $numFiles, $numProcessed); |
|
| 441 | + } |
|
| 442 | + } else { |
|
| 443 | + // Batching and forking. |
|
| 444 | + $childProcs = []; |
|
| 445 | + $numPerBatch = ceil($numFiles / $this->config->parallel); |
|
| 446 | + |
|
| 447 | + for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
|
| 448 | + $startAt = ($batch * $numPerBatch); |
|
| 449 | + if ($startAt >= $numFiles) { |
|
| 450 | + break; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + $endAt = ($startAt + $numPerBatch); |
|
| 454 | + if ($endAt > $numFiles) { |
|
| 455 | + $endAt = $numFiles; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
|
| 459 | + $pid = pcntl_fork(); |
|
| 460 | + if ($pid === -1) { |
|
| 461 | + throw new RuntimeException('Failed to create child process'); |
|
| 462 | + } else if ($pid !== 0) { |
|
| 463 | + $childProcs[$pid] = $childOutFilename; |
|
| 464 | + } else { |
|
| 465 | + // Move forward to the start of the batch. |
|
| 466 | + $todo->rewind(); |
|
| 467 | + for ($i = 0; $i < $startAt; $i++) { |
|
| 468 | + $todo->next(); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + // Reset the reporter to make sure only figures from this |
|
| 472 | + // file batch are recorded. |
|
| 473 | + $this->reporter->totalFiles = 0; |
|
| 474 | + $this->reporter->totalErrors = 0; |
|
| 475 | + $this->reporter->totalWarnings = 0; |
|
| 476 | + $this->reporter->totalFixable = 0; |
|
| 477 | + $this->reporter->totalFixed = 0; |
|
| 478 | + |
|
| 479 | + // Process the files. |
|
| 480 | + $pathsProcessed = []; |
|
| 481 | + ob_start(); |
|
| 482 | + for ($i = $startAt; $i < $endAt; $i++) { |
|
| 483 | + $path = $todo->key(); |
|
| 484 | + $file = $todo->current(); |
|
| 485 | + |
|
| 486 | + if ($file->ignored === true) { |
|
| 487 | + $todo->next(); |
|
| 488 | + continue; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + $currDir = dirname($path); |
|
| 492 | + if ($lastDir !== $currDir) { |
|
| 493 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 494 | + echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $lastDir = $currDir; |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + $this->processFile($file); |
|
| 501 | + |
|
| 502 | + $pathsProcessed[] = $path; |
|
| 503 | + $todo->next(); |
|
| 504 | + }//end for |
|
| 505 | + |
|
| 506 | + $debugOutput = ob_get_contents(); |
|
| 507 | + ob_end_clean(); |
|
| 508 | + |
|
| 509 | + // Write information about the run to the filesystem |
|
| 510 | + // so it can be picked up by the main process. |
|
| 511 | + $childOutput = [ |
|
| 512 | + 'totalFiles' => $this->reporter->totalFiles, |
|
| 513 | + 'totalErrors' => $this->reporter->totalErrors, |
|
| 514 | + 'totalWarnings' => $this->reporter->totalWarnings, |
|
| 515 | + 'totalFixable' => $this->reporter->totalFixable, |
|
| 516 | + 'totalFixed' => $this->reporter->totalFixed, |
|
| 517 | + ]; |
|
| 518 | + |
|
| 519 | + $output = '<'.'?php'."\n".' $childOutput = '; |
|
| 520 | + $output .= var_export($childOutput, true); |
|
| 521 | + $output .= ";\n\$debugOutput = "; |
|
| 522 | + $output .= var_export($debugOutput, true); |
|
| 523 | + |
|
| 524 | + if ($this->config->cache === true) { |
|
| 525 | + $childCache = []; |
|
| 526 | + foreach ($pathsProcessed as $path) { |
|
| 527 | + $childCache[$path] = Cache::get($path); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + $output .= ";\n\$childCache = "; |
|
| 531 | + $output .= var_export($childCache, true); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + $output .= ";\n?".'>'; |
|
| 535 | + file_put_contents($childOutFilename, $output); |
|
| 536 | + exit(); |
|
| 537 | + }//end if |
|
| 538 | + }//end for |
|
| 539 | + |
|
| 540 | + $success = $this->processChildProcs($childProcs); |
|
| 541 | + if ($success === false) { |
|
| 542 | + throw new RuntimeException('One or more child processes failed to run'); |
|
| 543 | + } |
|
| 544 | + }//end if |
|
| 545 | + |
|
| 546 | + restore_error_handler(); |
|
| 547 | + |
|
| 548 | + if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 549 | + && $this->config->interactive === false |
|
| 550 | + && $this->config->showProgress === true |
|
| 551 | + ) { |
|
| 552 | + echo PHP_EOL.PHP_EOL; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + if ($this->config->cache === true) { |
|
| 556 | + Cache::save(); |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
|
| 560 | + $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
|
| 561 | + |
|
| 562 | + $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
|
| 563 | + if ($ignoreErrors !== null) { |
|
| 564 | + $ignoreErrors = (bool) $ignoreErrors; |
|
| 565 | + if ($ignoreErrors === true) { |
|
| 566 | + $return -= $this->reporter->totalErrors; |
|
| 567 | + } |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + if ($ignoreWarnings !== null) { |
|
| 571 | + $ignoreWarnings = (bool) $ignoreWarnings; |
|
| 572 | + if ($ignoreWarnings === true) { |
|
| 573 | + $return -= $this->reporter->totalWarnings; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + return $return; |
|
| 578 | + |
|
| 579 | + }//end run() |
|
| 580 | + |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * Converts all PHP errors into exceptions. |
|
| 584 | + * |
|
| 585 | + * This method forces a sniff to stop processing if it is not |
|
| 586 | + * able to handle a specific piece of code, instead of continuing |
|
| 587 | + * and potentially getting into a loop. |
|
| 588 | + * |
|
| 589 | + * @param int $code The level of error raised. |
|
| 590 | + * @param string $message The error message. |
|
| 591 | + * @param string $file The path of the file that raised the error. |
|
| 592 | + * @param int $line The line number the error was raised at. |
|
| 593 | + * |
|
| 594 | + * @return void |
|
| 595 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 596 | + */ |
|
| 597 | + public function handleErrors($code, $message, $file, $line) |
|
| 598 | + { |
|
| 599 | + if ((error_reporting() & $code) === 0) { |
|
| 600 | + // This type of error is being muted. |
|
| 601 | + return true; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + throw new RuntimeException("$message in $file on line $line"); |
|
| 605 | + |
|
| 606 | + }//end handleErrors() |
|
| 607 | + |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Processes a single file, including checking and fixing. |
|
| 611 | + * |
|
| 612 | + * @param \PHP_CodeSniffer\Files\File $file The file to be processed. |
|
| 613 | + * |
|
| 614 | + * @return void |
|
| 615 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 616 | + */ |
|
| 617 | + public function processFile($file) |
|
| 618 | + { |
|
| 619 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 620 | + $startTime = microtime(true); |
|
| 621 | + echo 'Processing '.basename($file->path).' '; |
|
| 622 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 623 | + echo PHP_EOL; |
|
| 624 | + } |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + try { |
|
| 628 | + $file->process(); |
|
| 629 | + |
|
| 630 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 631 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 632 | + if ($timeTaken < 1000) { |
|
| 633 | + $timeTaken = round($timeTaken); |
|
| 634 | + echo "DONE in {$timeTaken}ms"; |
|
| 635 | + } else { |
|
| 636 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
| 637 | + echo "DONE in $timeTaken secs"; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + if (PHP_CODESNIFFER_CBF === true) { |
|
| 641 | + $errors = $file->getFixableCount(); |
|
| 642 | + echo " ($errors fixable violations)".PHP_EOL; |
|
| 643 | + } else { |
|
| 644 | + $errors = $file->getErrorCount(); |
|
| 645 | + $warnings = $file->getWarningCount(); |
|
| 646 | + echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
| 647 | + } |
|
| 648 | + } |
|
| 649 | + } catch (\Exception $e) { |
|
| 650 | + $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
|
| 651 | + $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
|
| 652 | + }//end try |
|
| 653 | + |
|
| 654 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 655 | + |
|
| 656 | + if ($this->config->interactive === true) { |
|
| 657 | + /* |
|
| 658 | 658 | Running interactively. |
| 659 | 659 | Print the error report for the current file and then wait for user input. |
| 660 | 660 | */ |
| 661 | 661 | |
| 662 | - // Get current violations and then clear the list to make sure |
|
| 663 | - // we only print violations for a single file each time. |
|
| 664 | - $numErrors = null; |
|
| 665 | - while ($numErrors !== 0) { |
|
| 666 | - $numErrors = ($file->getErrorCount() + $file->getWarningCount()); |
|
| 667 | - if ($numErrors === 0) { |
|
| 668 | - continue; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - $this->reporter->printReport('full'); |
|
| 672 | - |
|
| 673 | - echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
| 674 | - $input = fgets(STDIN); |
|
| 675 | - $input = trim($input); |
|
| 676 | - |
|
| 677 | - switch ($input) { |
|
| 678 | - case 's': |
|
| 679 | - break(2); |
|
| 680 | - case 'q': |
|
| 681 | - throw new DeepExitException('', 0); |
|
| 682 | - default: |
|
| 683 | - // Repopulate the sniffs because some of them save their state |
|
| 684 | - // and only clear it when the file changes, but we are rechecking |
|
| 685 | - // the same file. |
|
| 686 | - $file->ruleset->populateTokenListeners(); |
|
| 687 | - $file->reloadContent(); |
|
| 688 | - $file->process(); |
|
| 689 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 690 | - break; |
|
| 691 | - } |
|
| 692 | - }//end while |
|
| 693 | - }//end if |
|
| 694 | - |
|
| 695 | - // Clean up the file to save (a lot of) memory. |
|
| 696 | - $file->cleanUp(); |
|
| 697 | - |
|
| 698 | - }//end processFile() |
|
| 699 | - |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Waits for child processes to complete and cleans up after them. |
|
| 703 | - * |
|
| 704 | - * The reporting information returned by each child process is merged |
|
| 705 | - * into the main reporter class. |
|
| 706 | - * |
|
| 707 | - * @param array $childProcs An array of child processes to wait for. |
|
| 708 | - * |
|
| 709 | - * @return bool |
|
| 710 | - */ |
|
| 711 | - private function processChildProcs($childProcs) |
|
| 712 | - { |
|
| 713 | - $numProcessed = 0; |
|
| 714 | - $totalBatches = count($childProcs); |
|
| 715 | - |
|
| 716 | - $success = true; |
|
| 717 | - |
|
| 718 | - while (count($childProcs) > 0) { |
|
| 719 | - $pid = pcntl_waitpid(0, $status); |
|
| 720 | - if ($pid <= 0) { |
|
| 721 | - continue; |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - $out = $childProcs[$pid]; |
|
| 725 | - unset($childProcs[$pid]); |
|
| 726 | - if (file_exists($out) === false) { |
|
| 727 | - continue; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - include $out; |
|
| 731 | - unlink($out); |
|
| 732 | - |
|
| 733 | - $numProcessed++; |
|
| 734 | - |
|
| 735 | - if (isset($childOutput) === false) { |
|
| 736 | - // The child process died, so the run has failed. |
|
| 737 | - $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 738 | - $file->setErrorCounts(1, 0, 0, 0); |
|
| 739 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 740 | - $success = false; |
|
| 741 | - continue; |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - $this->reporter->totalFiles += $childOutput['totalFiles']; |
|
| 745 | - $this->reporter->totalErrors += $childOutput['totalErrors']; |
|
| 746 | - $this->reporter->totalWarnings += $childOutput['totalWarnings']; |
|
| 747 | - $this->reporter->totalFixable += $childOutput['totalFixable']; |
|
| 748 | - $this->reporter->totalFixed += $childOutput['totalFixed']; |
|
| 749 | - |
|
| 750 | - if (isset($debugOutput) === true) { |
|
| 751 | - echo $debugOutput; |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - if (isset($childCache) === true) { |
|
| 755 | - foreach ($childCache as $path => $cache) { |
|
| 756 | - Cache::set($path, $cache); |
|
| 757 | - } |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - // Fake a processed file so we can print progress output for the batch. |
|
| 761 | - $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 762 | - $file->setErrorCounts( |
|
| 763 | - $childOutput['totalErrors'], |
|
| 764 | - $childOutput['totalWarnings'], |
|
| 765 | - $childOutput['totalFixable'], |
|
| 766 | - $childOutput['totalFixed'] |
|
| 767 | - ); |
|
| 768 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 769 | - }//end while |
|
| 770 | - |
|
| 771 | - return $success; |
|
| 772 | - |
|
| 773 | - }//end processChildProcs() |
|
| 774 | - |
|
| 775 | - |
|
| 776 | - /** |
|
| 777 | - * Print progress information for a single processed file. |
|
| 778 | - * |
|
| 779 | - * @param \PHP_CodeSniffer\Files\File $file The file that was processed. |
|
| 780 | - * @param int $numFiles The total number of files to process. |
|
| 781 | - * @param int $numProcessed The number of files that have been processed, |
|
| 782 | - * including this one. |
|
| 783 | - * |
|
| 784 | - * @return void |
|
| 785 | - */ |
|
| 786 | - public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 787 | - { |
|
| 788 | - if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 789 | - || $this->config->showProgress === false |
|
| 790 | - ) { |
|
| 791 | - return; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - // Show progress information. |
|
| 795 | - if ($file->ignored === true) { |
|
| 796 | - echo 'S'; |
|
| 797 | - } else { |
|
| 798 | - $errors = $file->getErrorCount(); |
|
| 799 | - $warnings = $file->getWarningCount(); |
|
| 800 | - $fixable = $file->getFixableCount(); |
|
| 801 | - $fixed = $file->getFixedCount(); |
|
| 802 | - |
|
| 803 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 804 | - // Files with fixed errors or warnings are F (green). |
|
| 805 | - // Files with unfixable errors or warnings are E (red). |
|
| 806 | - // Files with no errors or warnings are . (black). |
|
| 807 | - if ($fixable > 0) { |
|
| 808 | - if ($this->config->colors === true) { |
|
| 809 | - echo "\033[31m"; |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - echo 'E'; |
|
| 813 | - |
|
| 814 | - if ($this->config->colors === true) { |
|
| 815 | - echo "\033[0m"; |
|
| 816 | - } |
|
| 817 | - } else if ($fixed > 0) { |
|
| 818 | - if ($this->config->colors === true) { |
|
| 819 | - echo "\033[32m"; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - echo 'F'; |
|
| 823 | - |
|
| 824 | - if ($this->config->colors === true) { |
|
| 825 | - echo "\033[0m"; |
|
| 826 | - } |
|
| 827 | - } else { |
|
| 828 | - echo '.'; |
|
| 829 | - }//end if |
|
| 830 | - } else { |
|
| 831 | - // Files with errors are E (red). |
|
| 832 | - // Files with fixable errors are E (green). |
|
| 833 | - // Files with warnings are W (yellow). |
|
| 834 | - // Files with fixable warnings are W (green). |
|
| 835 | - // Files with no errors or warnings are . (black). |
|
| 836 | - if ($errors > 0) { |
|
| 837 | - if ($this->config->colors === true) { |
|
| 838 | - if ($fixable > 0) { |
|
| 839 | - echo "\033[32m"; |
|
| 840 | - } else { |
|
| 841 | - echo "\033[31m"; |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - echo 'E'; |
|
| 846 | - |
|
| 847 | - if ($this->config->colors === true) { |
|
| 848 | - echo "\033[0m"; |
|
| 849 | - } |
|
| 850 | - } else if ($warnings > 0) { |
|
| 851 | - if ($this->config->colors === true) { |
|
| 852 | - if ($fixable > 0) { |
|
| 853 | - echo "\033[32m"; |
|
| 854 | - } else { |
|
| 855 | - echo "\033[33m"; |
|
| 856 | - } |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - echo 'W'; |
|
| 860 | - |
|
| 861 | - if ($this->config->colors === true) { |
|
| 862 | - echo "\033[0m"; |
|
| 863 | - } |
|
| 864 | - } else { |
|
| 865 | - echo '.'; |
|
| 866 | - }//end if |
|
| 867 | - }//end if |
|
| 868 | - }//end if |
|
| 869 | - |
|
| 870 | - $numPerLine = 60; |
|
| 871 | - if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) { |
|
| 872 | - return; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - $percent = round(($numProcessed / $numFiles) * 100); |
|
| 876 | - $padding = (strlen($numFiles) - strlen($numProcessed)); |
|
| 877 | - if ($numProcessed === $numFiles |
|
| 878 | - && $numFiles > $numPerLine |
|
| 879 | - && ($numProcessed % $numPerLine) !== 0 |
|
| 880 | - ) { |
|
| 881 | - $padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine))); |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
| 885 | - |
|
| 886 | - }//end printProgress() |
|
| 662 | + // Get current violations and then clear the list to make sure |
|
| 663 | + // we only print violations for a single file each time. |
|
| 664 | + $numErrors = null; |
|
| 665 | + while ($numErrors !== 0) { |
|
| 666 | + $numErrors = ($file->getErrorCount() + $file->getWarningCount()); |
|
| 667 | + if ($numErrors === 0) { |
|
| 668 | + continue; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + $this->reporter->printReport('full'); |
|
| 672 | + |
|
| 673 | + echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
| 674 | + $input = fgets(STDIN); |
|
| 675 | + $input = trim($input); |
|
| 676 | + |
|
| 677 | + switch ($input) { |
|
| 678 | + case 's': |
|
| 679 | + break(2); |
|
| 680 | + case 'q': |
|
| 681 | + throw new DeepExitException('', 0); |
|
| 682 | + default: |
|
| 683 | + // Repopulate the sniffs because some of them save their state |
|
| 684 | + // and only clear it when the file changes, but we are rechecking |
|
| 685 | + // the same file. |
|
| 686 | + $file->ruleset->populateTokenListeners(); |
|
| 687 | + $file->reloadContent(); |
|
| 688 | + $file->process(); |
|
| 689 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 690 | + break; |
|
| 691 | + } |
|
| 692 | + }//end while |
|
| 693 | + }//end if |
|
| 694 | + |
|
| 695 | + // Clean up the file to save (a lot of) memory. |
|
| 696 | + $file->cleanUp(); |
|
| 697 | + |
|
| 698 | + }//end processFile() |
|
| 699 | + |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Waits for child processes to complete and cleans up after them. |
|
| 703 | + * |
|
| 704 | + * The reporting information returned by each child process is merged |
|
| 705 | + * into the main reporter class. |
|
| 706 | + * |
|
| 707 | + * @param array $childProcs An array of child processes to wait for. |
|
| 708 | + * |
|
| 709 | + * @return bool |
|
| 710 | + */ |
|
| 711 | + private function processChildProcs($childProcs) |
|
| 712 | + { |
|
| 713 | + $numProcessed = 0; |
|
| 714 | + $totalBatches = count($childProcs); |
|
| 715 | + |
|
| 716 | + $success = true; |
|
| 717 | + |
|
| 718 | + while (count($childProcs) > 0) { |
|
| 719 | + $pid = pcntl_waitpid(0, $status); |
|
| 720 | + if ($pid <= 0) { |
|
| 721 | + continue; |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + $out = $childProcs[$pid]; |
|
| 725 | + unset($childProcs[$pid]); |
|
| 726 | + if (file_exists($out) === false) { |
|
| 727 | + continue; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + include $out; |
|
| 731 | + unlink($out); |
|
| 732 | + |
|
| 733 | + $numProcessed++; |
|
| 734 | + |
|
| 735 | + if (isset($childOutput) === false) { |
|
| 736 | + // The child process died, so the run has failed. |
|
| 737 | + $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 738 | + $file->setErrorCounts(1, 0, 0, 0); |
|
| 739 | + $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 740 | + $success = false; |
|
| 741 | + continue; |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + $this->reporter->totalFiles += $childOutput['totalFiles']; |
|
| 745 | + $this->reporter->totalErrors += $childOutput['totalErrors']; |
|
| 746 | + $this->reporter->totalWarnings += $childOutput['totalWarnings']; |
|
| 747 | + $this->reporter->totalFixable += $childOutput['totalFixable']; |
|
| 748 | + $this->reporter->totalFixed += $childOutput['totalFixed']; |
|
| 749 | + |
|
| 750 | + if (isset($debugOutput) === true) { |
|
| 751 | + echo $debugOutput; |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + if (isset($childCache) === true) { |
|
| 755 | + foreach ($childCache as $path => $cache) { |
|
| 756 | + Cache::set($path, $cache); |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + // Fake a processed file so we can print progress output for the batch. |
|
| 761 | + $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 762 | + $file->setErrorCounts( |
|
| 763 | + $childOutput['totalErrors'], |
|
| 764 | + $childOutput['totalWarnings'], |
|
| 765 | + $childOutput['totalFixable'], |
|
| 766 | + $childOutput['totalFixed'] |
|
| 767 | + ); |
|
| 768 | + $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 769 | + }//end while |
|
| 770 | + |
|
| 771 | + return $success; |
|
| 772 | + |
|
| 773 | + }//end processChildProcs() |
|
| 774 | + |
|
| 775 | + |
|
| 776 | + /** |
|
| 777 | + * Print progress information for a single processed file. |
|
| 778 | + * |
|
| 779 | + * @param \PHP_CodeSniffer\Files\File $file The file that was processed. |
|
| 780 | + * @param int $numFiles The total number of files to process. |
|
| 781 | + * @param int $numProcessed The number of files that have been processed, |
|
| 782 | + * including this one. |
|
| 783 | + * |
|
| 784 | + * @return void |
|
| 785 | + */ |
|
| 786 | + public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 787 | + { |
|
| 788 | + if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 789 | + || $this->config->showProgress === false |
|
| 790 | + ) { |
|
| 791 | + return; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + // Show progress information. |
|
| 795 | + if ($file->ignored === true) { |
|
| 796 | + echo 'S'; |
|
| 797 | + } else { |
|
| 798 | + $errors = $file->getErrorCount(); |
|
| 799 | + $warnings = $file->getWarningCount(); |
|
| 800 | + $fixable = $file->getFixableCount(); |
|
| 801 | + $fixed = $file->getFixedCount(); |
|
| 802 | + |
|
| 803 | + if (PHP_CODESNIFFER_CBF === true) { |
|
| 804 | + // Files with fixed errors or warnings are F (green). |
|
| 805 | + // Files with unfixable errors or warnings are E (red). |
|
| 806 | + // Files with no errors or warnings are . (black). |
|
| 807 | + if ($fixable > 0) { |
|
| 808 | + if ($this->config->colors === true) { |
|
| 809 | + echo "\033[31m"; |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + echo 'E'; |
|
| 813 | + |
|
| 814 | + if ($this->config->colors === true) { |
|
| 815 | + echo "\033[0m"; |
|
| 816 | + } |
|
| 817 | + } else if ($fixed > 0) { |
|
| 818 | + if ($this->config->colors === true) { |
|
| 819 | + echo "\033[32m"; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + echo 'F'; |
|
| 823 | + |
|
| 824 | + if ($this->config->colors === true) { |
|
| 825 | + echo "\033[0m"; |
|
| 826 | + } |
|
| 827 | + } else { |
|
| 828 | + echo '.'; |
|
| 829 | + }//end if |
|
| 830 | + } else { |
|
| 831 | + // Files with errors are E (red). |
|
| 832 | + // Files with fixable errors are E (green). |
|
| 833 | + // Files with warnings are W (yellow). |
|
| 834 | + // Files with fixable warnings are W (green). |
|
| 835 | + // Files with no errors or warnings are . (black). |
|
| 836 | + if ($errors > 0) { |
|
| 837 | + if ($this->config->colors === true) { |
|
| 838 | + if ($fixable > 0) { |
|
| 839 | + echo "\033[32m"; |
|
| 840 | + } else { |
|
| 841 | + echo "\033[31m"; |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + echo 'E'; |
|
| 846 | + |
|
| 847 | + if ($this->config->colors === true) { |
|
| 848 | + echo "\033[0m"; |
|
| 849 | + } |
|
| 850 | + } else if ($warnings > 0) { |
|
| 851 | + if ($this->config->colors === true) { |
|
| 852 | + if ($fixable > 0) { |
|
| 853 | + echo "\033[32m"; |
|
| 854 | + } else { |
|
| 855 | + echo "\033[33m"; |
|
| 856 | + } |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + echo 'W'; |
|
| 860 | + |
|
| 861 | + if ($this->config->colors === true) { |
|
| 862 | + echo "\033[0m"; |
|
| 863 | + } |
|
| 864 | + } else { |
|
| 865 | + echo '.'; |
|
| 866 | + }//end if |
|
| 867 | + }//end if |
|
| 868 | + }//end if |
|
| 869 | + |
|
| 870 | + $numPerLine = 60; |
|
| 871 | + if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) { |
|
| 872 | + return; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + $percent = round(($numProcessed / $numFiles) * 100); |
|
| 876 | + $padding = (strlen($numFiles) - strlen($numProcessed)); |
|
| 877 | + if ($numProcessed === $numFiles |
|
| 878 | + && $numFiles > $numPerLine |
|
| 879 | + && ($numProcessed % $numPerLine) !== 0 |
|
| 880 | + ) { |
|
| 881 | + $padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine))); |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
| 885 | + |
|
| 886 | + }//end printProgress() |
|
| 887 | 887 | |
| 888 | 888 | |
| 889 | 889 | }//end class |
@@ -15,50 +15,50 @@ |
||
| 15 | 15 | { |
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Generate a partial report for a single processed file. |
|
| 20 | - * |
|
| 21 | - * Function should return TRUE if it printed or stored data about the file |
|
| 22 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 23 | - * its data should be counted in the grand totals. |
|
| 24 | - * |
|
| 25 | - * @param array $report Prepared report data. |
|
| 26 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 27 | - * @param bool $showSources Show sources? |
|
| 28 | - * @param int $width Maximum allowed line width. |
|
| 29 | - * |
|
| 30 | - * @return bool |
|
| 31 | - */ |
|
| 32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80); |
|
| 18 | + /** |
|
| 19 | + * Generate a partial report for a single processed file. |
|
| 20 | + * |
|
| 21 | + * Function should return TRUE if it printed or stored data about the file |
|
| 22 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 23 | + * its data should be counted in the grand totals. |
|
| 24 | + * |
|
| 25 | + * @param array $report Prepared report data. |
|
| 26 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 27 | + * @param bool $showSources Show sources? |
|
| 28 | + * @param int $width Maximum allowed line width. |
|
| 29 | + * |
|
| 30 | + * @return bool |
|
| 31 | + */ |
|
| 32 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Generate the actual report. |
|
| 37 | - * |
|
| 38 | - * @param string $cachedData Any partial report data that was returned from |
|
| 39 | - * generateFileReport during the run. |
|
| 40 | - * @param int $totalFiles Total number of files processed during the run. |
|
| 41 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 42 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 43 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
| 44 | - * @param bool $showSources Show sources? |
|
| 45 | - * @param int $width Maximum allowed line width. |
|
| 46 | - * @param bool $interactive Are we running in interactive mode? |
|
| 47 | - * @param bool $toScreen Is the report being printed to screen? |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function generate( |
|
| 52 | - $cachedData, |
|
| 53 | - $totalFiles, |
|
| 54 | - $totalErrors, |
|
| 55 | - $totalWarnings, |
|
| 56 | - $totalFixable, |
|
| 57 | - $showSources=false, |
|
| 58 | - $width=80, |
|
| 59 | - $interactive=false, |
|
| 60 | - $toScreen=true |
|
| 61 | - ); |
|
| 35 | + /** |
|
| 36 | + * Generate the actual report. |
|
| 37 | + * |
|
| 38 | + * @param string $cachedData Any partial report data that was returned from |
|
| 39 | + * generateFileReport during the run. |
|
| 40 | + * @param int $totalFiles Total number of files processed during the run. |
|
| 41 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 42 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 43 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
| 44 | + * @param bool $showSources Show sources? |
|
| 45 | + * @param int $width Maximum allowed line width. |
|
| 46 | + * @param bool $interactive Are we running in interactive mode? |
|
| 47 | + * @param bool $toScreen Is the report being printed to screen? |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function generate( |
|
| 52 | + $cachedData, |
|
| 53 | + $totalFiles, |
|
| 54 | + $totalErrors, |
|
| 55 | + $totalWarnings, |
|
| 56 | + $totalFixable, |
|
| 57 | + $showSources=false, |
|
| 58 | + $width=80, |
|
| 59 | + $interactive=false, |
|
| 60 | + $toScreen=true |
|
| 61 | + ); |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | 64 | }//end interface |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @return bool |
| 31 | 31 | */ |
| 32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80); |
|
| 32 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -54,10 +54,10 @@ discard block |
||
| 54 | 54 | $totalErrors, |
| 55 | 55 | $totalWarnings, |
| 56 | 56 | $totalFixable, |
| 57 | - $showSources=false, |
|
| 58 | - $width=80, |
|
| 59 | - $interactive=false, |
|
| 60 | - $toScreen=true |
|
| 57 | + $showSources = false, |
|
| 58 | + $width = 80, |
|
| 59 | + $interactive = false, |
|
| 60 | + $toScreen = true |
|
| 61 | 61 | ); |
| 62 | 62 | |
| 63 | 63 | |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @return bool |
| 33 | 33 | */ |
| 34 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 34 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
| 35 | 35 | { |
| 36 | 36 | $out = new \XMLWriter; |
| 37 | 37 | $out->openMemory(); |
@@ -103,10 +103,10 @@ discard block |
||
| 103 | 103 | $totalErrors, |
| 104 | 104 | $totalWarnings, |
| 105 | 105 | $totalFixable, |
| 106 | - $showSources=false, |
|
| 107 | - $width=80, |
|
| 108 | - $interactive=false, |
|
| 109 | - $toScreen=true |
|
| 106 | + $showSources = false, |
|
| 107 | + $width = 80, |
|
| 108 | + $interactive = false, |
|
| 109 | + $toScreen = true |
|
| 110 | 110 | ) { |
| 111 | 111 | // Figure out the total number of tests. |
| 112 | 112 | $tests = 0; |
@@ -17,115 +17,115 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Generate a partial report for a single processed file. |
|
| 22 | - * |
|
| 23 | - * Function should return TRUE if it printed or stored data about the file |
|
| 24 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 25 | - * its data should be counted in the grand totals. |
|
| 26 | - * |
|
| 27 | - * @param array $report Prepared report data. |
|
| 28 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 29 | - * @param bool $showSources Show sources? |
|
| 30 | - * @param int $width Maximum allowed line width. |
|
| 31 | - * |
|
| 32 | - * @return bool |
|
| 33 | - */ |
|
| 34 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 35 | - { |
|
| 36 | - $out = new \XMLWriter; |
|
| 37 | - $out->openMemory(); |
|
| 38 | - $out->setIndent(true); |
|
| 39 | - |
|
| 40 | - $out->startElement('testsuite'); |
|
| 41 | - $out->writeAttribute('name', $report['filename']); |
|
| 42 | - $out->writeAttribute('errors', 0); |
|
| 43 | - |
|
| 44 | - if (count($report['messages']) === 0) { |
|
| 45 | - $out->writeAttribute('tests', 1); |
|
| 46 | - $out->writeAttribute('failures', 0); |
|
| 47 | - |
|
| 48 | - $out->startElement('testcase'); |
|
| 49 | - $out->writeAttribute('name', $report['filename']); |
|
| 50 | - $out->endElement(); |
|
| 51 | - } else { |
|
| 52 | - $failures = ($report['errors'] + $report['warnings']); |
|
| 53 | - $out->writeAttribute('tests', $failures); |
|
| 54 | - $out->writeAttribute('failures', $failures); |
|
| 55 | - |
|
| 56 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
| 57 | - foreach ($lineErrors as $column => $colErrors) { |
|
| 58 | - foreach ($colErrors as $error) { |
|
| 59 | - $out->startElement('testcase'); |
|
| 60 | - $out->writeAttribute('name', $error['source'].' at '.$report['filename']." ($line:$column)"); |
|
| 61 | - |
|
| 62 | - $error['type'] = strtolower($error['type']); |
|
| 63 | - if ($phpcsFile->config->encoding !== 'utf-8') { |
|
| 64 | - $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $out->startElement('failure'); |
|
| 68 | - $out->writeAttribute('type', $error['type']); |
|
| 69 | - $out->writeAttribute('message', $error['message']); |
|
| 70 | - $out->endElement(); |
|
| 71 | - |
|
| 72 | - $out->endElement(); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - }//end if |
|
| 77 | - |
|
| 78 | - $out->endElement(); |
|
| 79 | - echo $out->flush(); |
|
| 80 | - return true; |
|
| 81 | - |
|
| 82 | - }//end generateFileReport() |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Prints all violations for processed files, in a proprietary XML format. |
|
| 87 | - * |
|
| 88 | - * @param string $cachedData Any partial report data that was returned from |
|
| 89 | - * generateFileReport during the run. |
|
| 90 | - * @param int $totalFiles Total number of files processed during the run. |
|
| 91 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 92 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 93 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
| 94 | - * @param bool $showSources Show sources? |
|
| 95 | - * @param int $width Maximum allowed line width. |
|
| 96 | - * @param bool $interactive Are we running in interactive mode? |
|
| 97 | - * @param bool $toScreen Is the report being printed to screen? |
|
| 98 | - * |
|
| 99 | - * @return void |
|
| 100 | - */ |
|
| 101 | - public function generate( |
|
| 102 | - $cachedData, |
|
| 103 | - $totalFiles, |
|
| 104 | - $totalErrors, |
|
| 105 | - $totalWarnings, |
|
| 106 | - $totalFixable, |
|
| 107 | - $showSources=false, |
|
| 108 | - $width=80, |
|
| 109 | - $interactive=false, |
|
| 110 | - $toScreen=true |
|
| 111 | - ) { |
|
| 112 | - // Figure out the total number of tests. |
|
| 113 | - $tests = 0; |
|
| 114 | - $matches = []; |
|
| 115 | - preg_match_all('/tests="([0-9]+)"/', $cachedData, $matches); |
|
| 116 | - if (isset($matches[1]) === true) { |
|
| 117 | - foreach ($matches[1] as $match) { |
|
| 118 | - $tests += $match; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $failures = ($totalErrors + $totalWarnings); |
|
| 123 | - echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
| 124 | - echo '<testsuites name="PHP_CodeSniffer '.Config::VERSION.'" errors="0" tests="'.$tests.'" failures="'.$failures.'">'.PHP_EOL; |
|
| 125 | - echo $cachedData; |
|
| 126 | - echo '</testsuites>'.PHP_EOL; |
|
| 127 | - |
|
| 128 | - }//end generate() |
|
| 20 | + /** |
|
| 21 | + * Generate a partial report for a single processed file. |
|
| 22 | + * |
|
| 23 | + * Function should return TRUE if it printed or stored data about the file |
|
| 24 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 25 | + * its data should be counted in the grand totals. |
|
| 26 | + * |
|
| 27 | + * @param array $report Prepared report data. |
|
| 28 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 29 | + * @param bool $showSources Show sources? |
|
| 30 | + * @param int $width Maximum allowed line width. |
|
| 31 | + * |
|
| 32 | + * @return bool |
|
| 33 | + */ |
|
| 34 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 35 | + { |
|
| 36 | + $out = new \XMLWriter; |
|
| 37 | + $out->openMemory(); |
|
| 38 | + $out->setIndent(true); |
|
| 39 | + |
|
| 40 | + $out->startElement('testsuite'); |
|
| 41 | + $out->writeAttribute('name', $report['filename']); |
|
| 42 | + $out->writeAttribute('errors', 0); |
|
| 43 | + |
|
| 44 | + if (count($report['messages']) === 0) { |
|
| 45 | + $out->writeAttribute('tests', 1); |
|
| 46 | + $out->writeAttribute('failures', 0); |
|
| 47 | + |
|
| 48 | + $out->startElement('testcase'); |
|
| 49 | + $out->writeAttribute('name', $report['filename']); |
|
| 50 | + $out->endElement(); |
|
| 51 | + } else { |
|
| 52 | + $failures = ($report['errors'] + $report['warnings']); |
|
| 53 | + $out->writeAttribute('tests', $failures); |
|
| 54 | + $out->writeAttribute('failures', $failures); |
|
| 55 | + |
|
| 56 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
| 57 | + foreach ($lineErrors as $column => $colErrors) { |
|
| 58 | + foreach ($colErrors as $error) { |
|
| 59 | + $out->startElement('testcase'); |
|
| 60 | + $out->writeAttribute('name', $error['source'].' at '.$report['filename']." ($line:$column)"); |
|
| 61 | + |
|
| 62 | + $error['type'] = strtolower($error['type']); |
|
| 63 | + if ($phpcsFile->config->encoding !== 'utf-8') { |
|
| 64 | + $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $out->startElement('failure'); |
|
| 68 | + $out->writeAttribute('type', $error['type']); |
|
| 69 | + $out->writeAttribute('message', $error['message']); |
|
| 70 | + $out->endElement(); |
|
| 71 | + |
|
| 72 | + $out->endElement(); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + }//end if |
|
| 77 | + |
|
| 78 | + $out->endElement(); |
|
| 79 | + echo $out->flush(); |
|
| 80 | + return true; |
|
| 81 | + |
|
| 82 | + }//end generateFileReport() |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Prints all violations for processed files, in a proprietary XML format. |
|
| 87 | + * |
|
| 88 | + * @param string $cachedData Any partial report data that was returned from |
|
| 89 | + * generateFileReport during the run. |
|
| 90 | + * @param int $totalFiles Total number of files processed during the run. |
|
| 91 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 92 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 93 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
| 94 | + * @param bool $showSources Show sources? |
|
| 95 | + * @param int $width Maximum allowed line width. |
|
| 96 | + * @param bool $interactive Are we running in interactive mode? |
|
| 97 | + * @param bool $toScreen Is the report being printed to screen? |
|
| 98 | + * |
|
| 99 | + * @return void |
|
| 100 | + */ |
|
| 101 | + public function generate( |
|
| 102 | + $cachedData, |
|
| 103 | + $totalFiles, |
|
| 104 | + $totalErrors, |
|
| 105 | + $totalWarnings, |
|
| 106 | + $totalFixable, |
|
| 107 | + $showSources=false, |
|
| 108 | + $width=80, |
|
| 109 | + $interactive=false, |
|
| 110 | + $toScreen=true |
|
| 111 | + ) { |
|
| 112 | + // Figure out the total number of tests. |
|
| 113 | + $tests = 0; |
|
| 114 | + $matches = []; |
|
| 115 | + preg_match_all('/tests="([0-9]+)"/', $cachedData, $matches); |
|
| 116 | + if (isset($matches[1]) === true) { |
|
| 117 | + foreach ($matches[1] as $match) { |
|
| 118 | + $tests += $match; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $failures = ($totalErrors + $totalWarnings); |
|
| 123 | + echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
| 124 | + echo '<testsuites name="PHP_CodeSniffer '.Config::VERSION.'" errors="0" tests="'.$tests.'" failures="'.$failures.'">'.PHP_EOL; |
|
| 125 | + echo $cachedData; |
|
| 126 | + echo '</testsuites>'.PHP_EOL; |
|
| 127 | + |
|
| 128 | + }//end generate() |
|
| 129 | 129 | |
| 130 | 130 | |
| 131 | 131 | }//end class |
@@ -15,76 +15,76 @@ |
||
| 15 | 15 | { |
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Generate a partial report for a single processed file. |
|
| 20 | - * |
|
| 21 | - * Function should return TRUE if it printed or stored data about the file |
|
| 22 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 23 | - * its data should be counted in the grand totals. |
|
| 24 | - * |
|
| 25 | - * @param array $report Prepared report data. |
|
| 26 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 27 | - * @param bool $showSources Show sources? |
|
| 28 | - * @param int $width Maximum allowed line width. |
|
| 29 | - * |
|
| 30 | - * @return bool |
|
| 31 | - */ |
|
| 32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 33 | - { |
|
| 34 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
| 35 | - // Nothing to print. |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 18 | + /** |
|
| 19 | + * Generate a partial report for a single processed file. |
|
| 20 | + * |
|
| 21 | + * Function should return TRUE if it printed or stored data about the file |
|
| 22 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 23 | + * its data should be counted in the grand totals. |
|
| 24 | + * |
|
| 25 | + * @param array $report Prepared report data. |
|
| 26 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 27 | + * @param bool $showSources Show sources? |
|
| 28 | + * @param int $width Maximum allowed line width. |
|
| 29 | + * |
|
| 30 | + * @return bool |
|
| 31 | + */ |
|
| 32 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 33 | + { |
|
| 34 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
| 35 | + // Nothing to print. |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
| 40 | - foreach ($lineErrors as $column => $colErrors) { |
|
| 41 | - foreach ($colErrors as $error) { |
|
| 42 | - $message = $error['message']; |
|
| 43 | - if ($showSources === true) { |
|
| 44 | - $message .= ' ('.$error['source'].')'; |
|
| 45 | - } |
|
| 39 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
| 40 | + foreach ($lineErrors as $column => $colErrors) { |
|
| 41 | + foreach ($colErrors as $error) { |
|
| 42 | + $message = $error['message']; |
|
| 43 | + if ($showSources === true) { |
|
| 44 | + $message .= ' ('.$error['source'].')'; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - $type = strtolower($error['type']); |
|
| 48 | - echo $report['filename'].':'.$line.':'.$column.': '.$type.' - '.$message.PHP_EOL; |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - } |
|
| 47 | + $type = strtolower($error['type']); |
|
| 48 | + echo $report['filename'].':'.$line.':'.$column.': '.$type.' - '.$message.PHP_EOL; |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - return true; |
|
| 53 | + return true; |
|
| 54 | 54 | |
| 55 | - }//end generateFileReport() |
|
| 55 | + }//end generateFileReport() |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Generates an emacs report. |
|
| 60 | - * |
|
| 61 | - * @param string $cachedData Any partial report data that was returned from |
|
| 62 | - * generateFileReport during the run. |
|
| 63 | - * @param int $totalFiles Total number of files processed during the run. |
|
| 64 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 65 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 66 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
| 67 | - * @param bool $showSources Show sources? |
|
| 68 | - * @param int $width Maximum allowed line width. |
|
| 69 | - * @param bool $interactive Are we running in interactive mode? |
|
| 70 | - * @param bool $toScreen Is the report being printed to screen? |
|
| 71 | - * |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - public function generate( |
|
| 75 | - $cachedData, |
|
| 76 | - $totalFiles, |
|
| 77 | - $totalErrors, |
|
| 78 | - $totalWarnings, |
|
| 79 | - $totalFixable, |
|
| 80 | - $showSources=false, |
|
| 81 | - $width=80, |
|
| 82 | - $interactive=false, |
|
| 83 | - $toScreen=true |
|
| 84 | - ) { |
|
| 85 | - echo $cachedData; |
|
| 58 | + /** |
|
| 59 | + * Generates an emacs report. |
|
| 60 | + * |
|
| 61 | + * @param string $cachedData Any partial report data that was returned from |
|
| 62 | + * generateFileReport during the run. |
|
| 63 | + * @param int $totalFiles Total number of files processed during the run. |
|
| 64 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 65 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 66 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
| 67 | + * @param bool $showSources Show sources? |
|
| 68 | + * @param int $width Maximum allowed line width. |
|
| 69 | + * @param bool $interactive Are we running in interactive mode? |
|
| 70 | + * @param bool $toScreen Is the report being printed to screen? |
|
| 71 | + * |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + public function generate( |
|
| 75 | + $cachedData, |
|
| 76 | + $totalFiles, |
|
| 77 | + $totalErrors, |
|
| 78 | + $totalWarnings, |
|
| 79 | + $totalFixable, |
|
| 80 | + $showSources=false, |
|
| 81 | + $width=80, |
|
| 82 | + $interactive=false, |
|
| 83 | + $toScreen=true |
|
| 84 | + ) { |
|
| 85 | + echo $cachedData; |
|
| 86 | 86 | |
| 87 | - }//end generate() |
|
| 87 | + }//end generate() |
|
| 88 | 88 | |
| 89 | 89 | |
| 90 | 90 | }//end class |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @return bool |
| 31 | 31 | */ |
| 32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 32 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
| 33 | 33 | { |
| 34 | 34 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
| 35 | 35 | // Nothing to print. |
@@ -77,10 +77,10 @@ discard block |
||
| 77 | 77 | $totalErrors, |
| 78 | 78 | $totalWarnings, |
| 79 | 79 | $totalFixable, |
| 80 | - $showSources=false, |
|
| 81 | - $width=80, |
|
| 82 | - $interactive=false, |
|
| 83 | - $toScreen=true |
|
| 80 | + $showSources = false, |
|
| 81 | + $width = 80, |
|
| 82 | + $interactive = false, |
|
| 83 | + $toScreen = true |
|
| 84 | 84 | ) { |
| 85 | 85 | echo $cachedData; |
| 86 | 86 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * |
| 31 | 31 | * @return bool |
| 32 | 32 | */ |
| 33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
| 34 | 34 | { |
| 35 | 35 | $out = new \XMLWriter; |
| 36 | 36 | $out->openMemory(); |
@@ -105,10 +105,10 @@ discard block |
||
| 105 | 105 | $totalErrors, |
| 106 | 106 | $totalWarnings, |
| 107 | 107 | $totalFixable, |
| 108 | - $showSources=false, |
|
| 109 | - $width=80, |
|
| 110 | - $interactive=false, |
|
| 111 | - $toScreen=true |
|
| 108 | + $showSources = false, |
|
| 109 | + $width = 80, |
|
| 110 | + $interactive = false, |
|
| 111 | + $toScreen = true |
|
| 112 | 112 | ) { |
| 113 | 113 | echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
| 114 | 114 | echo '<phpcs version="'.Config::VERSION.'">'.PHP_EOL; |
@@ -16,111 +16,111 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Generate a partial report for a single processed file. |
|
| 21 | - * |
|
| 22 | - * Function should return TRUE if it printed or stored data about the file |
|
| 23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 24 | - * its data should be counted in the grand totals. |
|
| 25 | - * |
|
| 26 | - * @param array $report Prepared report data. |
|
| 27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 28 | - * @param bool $showSources Show sources? |
|
| 29 | - * @param int $width Maximum allowed line width. |
|
| 30 | - * |
|
| 31 | - * @return bool |
|
| 32 | - */ |
|
| 33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 34 | - { |
|
| 35 | - $out = new \XMLWriter; |
|
| 36 | - $out->openMemory(); |
|
| 37 | - $out->setIndent(true); |
|
| 38 | - $out->setIndentString(' '); |
|
| 39 | - $out->startDocument('1.0', 'UTF-8'); |
|
| 40 | - |
|
| 41 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
| 42 | - // Nothing to print. |
|
| 43 | - return false; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - $out->startElement('file'); |
|
| 47 | - $out->writeAttribute('name', $report['filename']); |
|
| 48 | - $out->writeAttribute('errors', $report['errors']); |
|
| 49 | - $out->writeAttribute('warnings', $report['warnings']); |
|
| 50 | - $out->writeAttribute('fixable', $report['fixable']); |
|
| 51 | - |
|
| 52 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
| 53 | - foreach ($lineErrors as $column => $colErrors) { |
|
| 54 | - foreach ($colErrors as $error) { |
|
| 55 | - $error['type'] = strtolower($error['type']); |
|
| 56 | - if ($phpcsFile->config->encoding !== 'utf-8') { |
|
| 57 | - $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $out->startElement($error['type']); |
|
| 61 | - $out->writeAttribute('line', $line); |
|
| 62 | - $out->writeAttribute('column', $column); |
|
| 63 | - $out->writeAttribute('source', $error['source']); |
|
| 64 | - $out->writeAttribute('severity', $error['severity']); |
|
| 65 | - $out->writeAttribute('fixable', (int) $error['fixable']); |
|
| 66 | - $out->text($error['message']); |
|
| 67 | - $out->endElement(); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - }//end foreach |
|
| 71 | - |
|
| 72 | - $out->endElement(); |
|
| 73 | - |
|
| 74 | - // Remove the start of the document because we will |
|
| 75 | - // add that manually later. We only have it in here to |
|
| 76 | - // properly set the encoding. |
|
| 77 | - $content = $out->flush(); |
|
| 78 | - if (strpos($content, PHP_EOL) !== false) { |
|
| 79 | - $content = substr($content, (strpos($content, PHP_EOL) + strlen(PHP_EOL))); |
|
| 80 | - } else if (strpos($content, "\n") !== false) { |
|
| 81 | - $content = substr($content, (strpos($content, "\n") + 1)); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - echo $content; |
|
| 85 | - |
|
| 86 | - return true; |
|
| 87 | - |
|
| 88 | - }//end generateFileReport() |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Prints all violations for processed files, in a proprietary XML format. |
|
| 93 | - * |
|
| 94 | - * @param string $cachedData Any partial report data that was returned from |
|
| 95 | - * generateFileReport during the run. |
|
| 96 | - * @param int $totalFiles Total number of files processed during the run. |
|
| 97 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 98 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 99 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
| 100 | - * @param bool $showSources Show sources? |
|
| 101 | - * @param int $width Maximum allowed line width. |
|
| 102 | - * @param bool $interactive Are we running in interactive mode? |
|
| 103 | - * @param bool $toScreen Is the report being printed to screen? |
|
| 104 | - * |
|
| 105 | - * @return void |
|
| 106 | - */ |
|
| 107 | - public function generate( |
|
| 108 | - $cachedData, |
|
| 109 | - $totalFiles, |
|
| 110 | - $totalErrors, |
|
| 111 | - $totalWarnings, |
|
| 112 | - $totalFixable, |
|
| 113 | - $showSources=false, |
|
| 114 | - $width=80, |
|
| 115 | - $interactive=false, |
|
| 116 | - $toScreen=true |
|
| 117 | - ) { |
|
| 118 | - echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
| 119 | - echo '<phpcs version="'.Config::VERSION.'">'.PHP_EOL; |
|
| 120 | - echo $cachedData; |
|
| 121 | - echo '</phpcs>'.PHP_EOL; |
|
| 122 | - |
|
| 123 | - }//end generate() |
|
| 19 | + /** |
|
| 20 | + * Generate a partial report for a single processed file. |
|
| 21 | + * |
|
| 22 | + * Function should return TRUE if it printed or stored data about the file |
|
| 23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 24 | + * its data should be counted in the grand totals. |
|
| 25 | + * |
|
| 26 | + * @param array $report Prepared report data. |
|
| 27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 28 | + * @param bool $showSources Show sources? |
|
| 29 | + * @param int $width Maximum allowed line width. |
|
| 30 | + * |
|
| 31 | + * @return bool |
|
| 32 | + */ |
|
| 33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 34 | + { |
|
| 35 | + $out = new \XMLWriter; |
|
| 36 | + $out->openMemory(); |
|
| 37 | + $out->setIndent(true); |
|
| 38 | + $out->setIndentString(' '); |
|
| 39 | + $out->startDocument('1.0', 'UTF-8'); |
|
| 40 | + |
|
| 41 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
| 42 | + // Nothing to print. |
|
| 43 | + return false; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + $out->startElement('file'); |
|
| 47 | + $out->writeAttribute('name', $report['filename']); |
|
| 48 | + $out->writeAttribute('errors', $report['errors']); |
|
| 49 | + $out->writeAttribute('warnings', $report['warnings']); |
|
| 50 | + $out->writeAttribute('fixable', $report['fixable']); |
|
| 51 | + |
|
| 52 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
| 53 | + foreach ($lineErrors as $column => $colErrors) { |
|
| 54 | + foreach ($colErrors as $error) { |
|
| 55 | + $error['type'] = strtolower($error['type']); |
|
| 56 | + if ($phpcsFile->config->encoding !== 'utf-8') { |
|
| 57 | + $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $out->startElement($error['type']); |
|
| 61 | + $out->writeAttribute('line', $line); |
|
| 62 | + $out->writeAttribute('column', $column); |
|
| 63 | + $out->writeAttribute('source', $error['source']); |
|
| 64 | + $out->writeAttribute('severity', $error['severity']); |
|
| 65 | + $out->writeAttribute('fixable', (int) $error['fixable']); |
|
| 66 | + $out->text($error['message']); |
|
| 67 | + $out->endElement(); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + }//end foreach |
|
| 71 | + |
|
| 72 | + $out->endElement(); |
|
| 73 | + |
|
| 74 | + // Remove the start of the document because we will |
|
| 75 | + // add that manually later. We only have it in here to |
|
| 76 | + // properly set the encoding. |
|
| 77 | + $content = $out->flush(); |
|
| 78 | + if (strpos($content, PHP_EOL) !== false) { |
|
| 79 | + $content = substr($content, (strpos($content, PHP_EOL) + strlen(PHP_EOL))); |
|
| 80 | + } else if (strpos($content, "\n") !== false) { |
|
| 81 | + $content = substr($content, (strpos($content, "\n") + 1)); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + echo $content; |
|
| 85 | + |
|
| 86 | + return true; |
|
| 87 | + |
|
| 88 | + }//end generateFileReport() |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Prints all violations for processed files, in a proprietary XML format. |
|
| 93 | + * |
|
| 94 | + * @param string $cachedData Any partial report data that was returned from |
|
| 95 | + * generateFileReport during the run. |
|
| 96 | + * @param int $totalFiles Total number of files processed during the run. |
|
| 97 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 98 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 99 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
| 100 | + * @param bool $showSources Show sources? |
|
| 101 | + * @param int $width Maximum allowed line width. |
|
| 102 | + * @param bool $interactive Are we running in interactive mode? |
|
| 103 | + * @param bool $toScreen Is the report being printed to screen? |
|
| 104 | + * |
|
| 105 | + * @return void |
|
| 106 | + */ |
|
| 107 | + public function generate( |
|
| 108 | + $cachedData, |
|
| 109 | + $totalFiles, |
|
| 110 | + $totalErrors, |
|
| 111 | + $totalWarnings, |
|
| 112 | + $totalFixable, |
|
| 113 | + $showSources=false, |
|
| 114 | + $width=80, |
|
| 115 | + $interactive=false, |
|
| 116 | + $toScreen=true |
|
| 117 | + ) { |
|
| 118 | + echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
| 119 | + echo '<phpcs version="'.Config::VERSION.'">'.PHP_EOL; |
|
| 120 | + echo $cachedData; |
|
| 121 | + echo '</phpcs>'.PHP_EOL; |
|
| 122 | + |
|
| 123 | + }//end generate() |
|
| 124 | 124 | |
| 125 | 125 | |
| 126 | 126 | }//end class |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * |
| 31 | 31 | * @return bool |
| 32 | 32 | */ |
| 33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
| 34 | 34 | { |
| 35 | 35 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
| 36 | 36 | // Nothing to print. |
@@ -86,10 +86,10 @@ discard block |
||
| 86 | 86 | $totalErrors, |
| 87 | 87 | $totalWarnings, |
| 88 | 88 | $totalFixable, |
| 89 | - $showSources=false, |
|
| 90 | - $width=80, |
|
| 91 | - $interactive=false, |
|
| 92 | - $toScreen=true |
|
| 89 | + $showSources = false, |
|
| 90 | + $width = 80, |
|
| 91 | + $interactive = false, |
|
| 92 | + $toScreen = true |
|
| 93 | 93 | ) { |
| 94 | 94 | $lines = explode(PHP_EOL, $cachedData); |
| 95 | 95 | array_pop($lines); |
@@ -16,321 +16,321 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Generate a partial report for a single processed file. |
|
| 21 | - * |
|
| 22 | - * Function should return TRUE if it printed or stored data about the file |
|
| 23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 24 | - * its data should be counted in the grand totals. |
|
| 25 | - * |
|
| 26 | - * @param array $report Prepared report data. |
|
| 27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 28 | - * @param bool $showSources Show sources? |
|
| 29 | - * @param int $width Maximum allowed line width. |
|
| 30 | - * |
|
| 31 | - * @return bool |
|
| 32 | - */ |
|
| 33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 34 | - { |
|
| 35 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
| 36 | - // Nothing to print. |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - $sources = []; |
|
| 41 | - |
|
| 42 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
| 43 | - foreach ($lineErrors as $column => $colErrors) { |
|
| 44 | - foreach ($colErrors as $error) { |
|
| 45 | - $src = $error['source']; |
|
| 46 | - if (isset($sources[$src]) === false) { |
|
| 47 | - $sources[$src] = [ |
|
| 48 | - 'fixable' => (int) $error['fixable'], |
|
| 49 | - 'count' => 1, |
|
| 50 | - ]; |
|
| 51 | - } else { |
|
| 52 | - $sources[$src]['count']++; |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - foreach ($sources as $source => $data) { |
|
| 59 | - echo $source.'>>'.$data['fixable'].'>>'.$data['count'].PHP_EOL; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - return true; |
|
| 63 | - |
|
| 64 | - }//end generateFileReport() |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Prints the source of all errors and warnings. |
|
| 69 | - * |
|
| 70 | - * @param string $cachedData Any partial report data that was returned from |
|
| 71 | - * generateFileReport during the run. |
|
| 72 | - * @param int $totalFiles Total number of files processed during the run. |
|
| 73 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 74 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 75 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
| 76 | - * @param bool $showSources Show sources? |
|
| 77 | - * @param int $width Maximum allowed line width. |
|
| 78 | - * @param bool $interactive Are we running in interactive mode? |
|
| 79 | - * @param bool $toScreen Is the report being printed to screen? |
|
| 80 | - * |
|
| 81 | - * @return void |
|
| 82 | - */ |
|
| 83 | - public function generate( |
|
| 84 | - $cachedData, |
|
| 85 | - $totalFiles, |
|
| 86 | - $totalErrors, |
|
| 87 | - $totalWarnings, |
|
| 88 | - $totalFixable, |
|
| 89 | - $showSources=false, |
|
| 90 | - $width=80, |
|
| 91 | - $interactive=false, |
|
| 92 | - $toScreen=true |
|
| 93 | - ) { |
|
| 94 | - $lines = explode(PHP_EOL, $cachedData); |
|
| 95 | - array_pop($lines); |
|
| 96 | - |
|
| 97 | - if (empty($lines) === true) { |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $sources = []; |
|
| 102 | - $maxLength = 0; |
|
| 103 | - |
|
| 104 | - foreach ($lines as $line) { |
|
| 105 | - $parts = explode('>>', $line); |
|
| 106 | - $source = $parts[0]; |
|
| 107 | - $fixable = (bool) $parts[1]; |
|
| 108 | - $count = $parts[2]; |
|
| 109 | - |
|
| 110 | - if (isset($sources[$source]) === false) { |
|
| 111 | - if ($showSources === true) { |
|
| 112 | - $parts = null; |
|
| 113 | - $sniff = $source; |
|
| 114 | - } else { |
|
| 115 | - $parts = explode('.', $source); |
|
| 116 | - if ($parts[0] === 'Internal') { |
|
| 117 | - $parts[2] = $parts[1]; |
|
| 118 | - $parts[1] = ''; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $parts[1] = $this->makeFriendlyName($parts[1]); |
|
| 122 | - |
|
| 123 | - $sniff = $this->makeFriendlyName($parts[2]); |
|
| 124 | - if (isset($parts[3]) === true) { |
|
| 125 | - $name = $this->makeFriendlyName($parts[3]); |
|
| 126 | - $name[0] = strtolower($name[0]); |
|
| 127 | - $sniff .= ' '.$name; |
|
| 128 | - unset($parts[3]); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $parts[2] = $sniff; |
|
| 132 | - }//end if |
|
| 133 | - |
|
| 134 | - $maxLength = max($maxLength, strlen($sniff)); |
|
| 135 | - |
|
| 136 | - $sources[$source] = [ |
|
| 137 | - 'count' => $count, |
|
| 138 | - 'fixable' => $fixable, |
|
| 139 | - 'parts' => $parts, |
|
| 140 | - ]; |
|
| 141 | - } else { |
|
| 142 | - $sources[$source]['count'] += $count; |
|
| 143 | - }//end if |
|
| 144 | - }//end foreach |
|
| 145 | - |
|
| 146 | - if ($showSources === true) { |
|
| 147 | - $width = min($width, ($maxLength + 11)); |
|
| 148 | - } else { |
|
| 149 | - $width = min($width, ($maxLength + 41)); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $width = max($width, 70); |
|
| 153 | - |
|
| 154 | - // Sort the data based on counts and source code. |
|
| 155 | - $sourceCodes = array_keys($sources); |
|
| 156 | - $counts = []; |
|
| 157 | - foreach ($sources as $source => $data) { |
|
| 158 | - $counts[$source] = $data['count']; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - array_multisort($counts, SORT_DESC, $sourceCodes, SORT_ASC, SORT_NATURAL, $sources); |
|
| 162 | - |
|
| 163 | - echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL; |
|
| 164 | - echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
| 165 | - if ($showSources === true) { |
|
| 166 | - if ($totalFixable > 0) { |
|
| 167 | - echo ' SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL; |
|
| 168 | - } else { |
|
| 169 | - echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL; |
|
| 170 | - } |
|
| 171 | - } else { |
|
| 172 | - if ($totalFixable > 0) { |
|
| 173 | - echo ' STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL; |
|
| 174 | - } else { |
|
| 175 | - echo 'STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - echo "\033[0m".str_repeat('-', $width).PHP_EOL; |
|
| 180 | - |
|
| 181 | - $fixableSources = 0; |
|
| 182 | - |
|
| 183 | - if ($showSources === true) { |
|
| 184 | - $maxSniffWidth = ($width - 7); |
|
| 185 | - } else { |
|
| 186 | - $maxSniffWidth = ($width - 37); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - if ($totalFixable > 0) { |
|
| 190 | - $maxSniffWidth -= 4; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - foreach ($sources as $source => $sourceData) { |
|
| 194 | - if ($totalFixable > 0) { |
|
| 195 | - echo '['; |
|
| 196 | - if ($sourceData['fixable'] === true) { |
|
| 197 | - echo 'x'; |
|
| 198 | - $fixableSources++; |
|
| 199 | - } else { |
|
| 200 | - echo ' '; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - echo '] '; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if ($showSources === true) { |
|
| 207 | - if (strlen($source) > $maxSniffWidth) { |
|
| 208 | - $source = substr($source, 0, $maxSniffWidth); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - echo $source; |
|
| 212 | - if ($totalFixable > 0) { |
|
| 213 | - echo str_repeat(' ', ($width - 9 - strlen($source))); |
|
| 214 | - } else { |
|
| 215 | - echo str_repeat(' ', ($width - 5 - strlen($source))); |
|
| 216 | - } |
|
| 217 | - } else { |
|
| 218 | - $parts = $sourceData['parts']; |
|
| 219 | - |
|
| 220 | - if (strlen($parts[0]) > 8) { |
|
| 221 | - $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1)); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - echo $parts[0].str_repeat(' ', (10 - strlen($parts[0]))); |
|
| 225 | - |
|
| 226 | - $category = $parts[1]; |
|
| 227 | - if (strlen($category) > 18) { |
|
| 228 | - $category = substr($category, 0, ((strlen($category) - 18) * -1)); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - echo $category.str_repeat(' ', (20 - strlen($category))); |
|
| 232 | - |
|
| 233 | - $sniff = $parts[2]; |
|
| 234 | - if (strlen($sniff) > $maxSniffWidth) { |
|
| 235 | - $sniff = substr($sniff, 0, $maxSniffWidth); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - if ($totalFixable > 0) { |
|
| 239 | - echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff))); |
|
| 240 | - } else { |
|
| 241 | - echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff))); |
|
| 242 | - } |
|
| 243 | - }//end if |
|
| 244 | - |
|
| 245 | - echo $sourceData['count'].PHP_EOL; |
|
| 246 | - }//end foreach |
|
| 247 | - |
|
| 248 | - echo str_repeat('-', $width).PHP_EOL; |
|
| 249 | - echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION'; |
|
| 250 | - if (($totalErrors + $totalWarnings) > 1) { |
|
| 251 | - echo 'S'; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - echo ' WERE FOUND IN '.count($sources).' SOURCE'; |
|
| 255 | - if (count($sources) !== 1) { |
|
| 256 | - echo 'S'; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - echo "\033[0m"; |
|
| 260 | - |
|
| 261 | - if ($totalFixable > 0) { |
|
| 262 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
| 263 | - echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
| 267 | - |
|
| 268 | - if ($toScreen === true && $interactive === false) { |
|
| 269 | - Timing::printRunTime(); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - }//end generate() |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Converts a camel caps name into a readable string. |
|
| 277 | - * |
|
| 278 | - * @param string $name The camel caps name to convert. |
|
| 279 | - * |
|
| 280 | - * @return string |
|
| 281 | - */ |
|
| 282 | - public function makeFriendlyName($name) |
|
| 283 | - { |
|
| 284 | - if (trim($name) === '') { |
|
| 285 | - return ''; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $friendlyName = ''; |
|
| 289 | - $length = strlen($name); |
|
| 290 | - |
|
| 291 | - $lastWasUpper = false; |
|
| 292 | - $lastWasNumeric = false; |
|
| 293 | - for ($i = 0; $i < $length; $i++) { |
|
| 294 | - if (is_numeric($name[$i]) === true) { |
|
| 295 | - if ($lastWasNumeric === false) { |
|
| 296 | - $friendlyName .= ' '; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - $lastWasUpper = false; |
|
| 300 | - $lastWasNumeric = true; |
|
| 301 | - } else { |
|
| 302 | - $lastWasNumeric = false; |
|
| 303 | - |
|
| 304 | - $char = strtolower($name[$i]); |
|
| 305 | - if ($char === $name[$i]) { |
|
| 306 | - // Lowercase. |
|
| 307 | - $lastWasUpper = false; |
|
| 308 | - } else { |
|
| 309 | - // Uppercase. |
|
| 310 | - if ($lastWasUpper === false) { |
|
| 311 | - $friendlyName .= ' '; |
|
| 312 | - if ($i < ($length - 1)) { |
|
| 313 | - $next = $name[($i + 1)]; |
|
| 314 | - if (strtolower($next) === $next) { |
|
| 315 | - // Next char is lowercase so it is a word boundary. |
|
| 316 | - $name[$i] = strtolower($name[$i]); |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $lastWasUpper = true; |
|
| 322 | - } |
|
| 323 | - }//end if |
|
| 324 | - |
|
| 325 | - $friendlyName .= $name[$i]; |
|
| 326 | - }//end for |
|
| 327 | - |
|
| 328 | - $friendlyName = trim($friendlyName); |
|
| 329 | - $friendlyName[0] = strtoupper($friendlyName[0]); |
|
| 330 | - |
|
| 331 | - return $friendlyName; |
|
| 332 | - |
|
| 333 | - }//end makeFriendlyName() |
|
| 19 | + /** |
|
| 20 | + * Generate a partial report for a single processed file. |
|
| 21 | + * |
|
| 22 | + * Function should return TRUE if it printed or stored data about the file |
|
| 23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 24 | + * its data should be counted in the grand totals. |
|
| 25 | + * |
|
| 26 | + * @param array $report Prepared report data. |
|
| 27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 28 | + * @param bool $showSources Show sources? |
|
| 29 | + * @param int $width Maximum allowed line width. |
|
| 30 | + * |
|
| 31 | + * @return bool |
|
| 32 | + */ |
|
| 33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 34 | + { |
|
| 35 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
| 36 | + // Nothing to print. |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + $sources = []; |
|
| 41 | + |
|
| 42 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
| 43 | + foreach ($lineErrors as $column => $colErrors) { |
|
| 44 | + foreach ($colErrors as $error) { |
|
| 45 | + $src = $error['source']; |
|
| 46 | + if (isset($sources[$src]) === false) { |
|
| 47 | + $sources[$src] = [ |
|
| 48 | + 'fixable' => (int) $error['fixable'], |
|
| 49 | + 'count' => 1, |
|
| 50 | + ]; |
|
| 51 | + } else { |
|
| 52 | + $sources[$src]['count']++; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + foreach ($sources as $source => $data) { |
|
| 59 | + echo $source.'>>'.$data['fixable'].'>>'.$data['count'].PHP_EOL; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + return true; |
|
| 63 | + |
|
| 64 | + }//end generateFileReport() |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Prints the source of all errors and warnings. |
|
| 69 | + * |
|
| 70 | + * @param string $cachedData Any partial report data that was returned from |
|
| 71 | + * generateFileReport during the run. |
|
| 72 | + * @param int $totalFiles Total number of files processed during the run. |
|
| 73 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 74 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 75 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
| 76 | + * @param bool $showSources Show sources? |
|
| 77 | + * @param int $width Maximum allowed line width. |
|
| 78 | + * @param bool $interactive Are we running in interactive mode? |
|
| 79 | + * @param bool $toScreen Is the report being printed to screen? |
|
| 80 | + * |
|
| 81 | + * @return void |
|
| 82 | + */ |
|
| 83 | + public function generate( |
|
| 84 | + $cachedData, |
|
| 85 | + $totalFiles, |
|
| 86 | + $totalErrors, |
|
| 87 | + $totalWarnings, |
|
| 88 | + $totalFixable, |
|
| 89 | + $showSources=false, |
|
| 90 | + $width=80, |
|
| 91 | + $interactive=false, |
|
| 92 | + $toScreen=true |
|
| 93 | + ) { |
|
| 94 | + $lines = explode(PHP_EOL, $cachedData); |
|
| 95 | + array_pop($lines); |
|
| 96 | + |
|
| 97 | + if (empty($lines) === true) { |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $sources = []; |
|
| 102 | + $maxLength = 0; |
|
| 103 | + |
|
| 104 | + foreach ($lines as $line) { |
|
| 105 | + $parts = explode('>>', $line); |
|
| 106 | + $source = $parts[0]; |
|
| 107 | + $fixable = (bool) $parts[1]; |
|
| 108 | + $count = $parts[2]; |
|
| 109 | + |
|
| 110 | + if (isset($sources[$source]) === false) { |
|
| 111 | + if ($showSources === true) { |
|
| 112 | + $parts = null; |
|
| 113 | + $sniff = $source; |
|
| 114 | + } else { |
|
| 115 | + $parts = explode('.', $source); |
|
| 116 | + if ($parts[0] === 'Internal') { |
|
| 117 | + $parts[2] = $parts[1]; |
|
| 118 | + $parts[1] = ''; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $parts[1] = $this->makeFriendlyName($parts[1]); |
|
| 122 | + |
|
| 123 | + $sniff = $this->makeFriendlyName($parts[2]); |
|
| 124 | + if (isset($parts[3]) === true) { |
|
| 125 | + $name = $this->makeFriendlyName($parts[3]); |
|
| 126 | + $name[0] = strtolower($name[0]); |
|
| 127 | + $sniff .= ' '.$name; |
|
| 128 | + unset($parts[3]); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $parts[2] = $sniff; |
|
| 132 | + }//end if |
|
| 133 | + |
|
| 134 | + $maxLength = max($maxLength, strlen($sniff)); |
|
| 135 | + |
|
| 136 | + $sources[$source] = [ |
|
| 137 | + 'count' => $count, |
|
| 138 | + 'fixable' => $fixable, |
|
| 139 | + 'parts' => $parts, |
|
| 140 | + ]; |
|
| 141 | + } else { |
|
| 142 | + $sources[$source]['count'] += $count; |
|
| 143 | + }//end if |
|
| 144 | + }//end foreach |
|
| 145 | + |
|
| 146 | + if ($showSources === true) { |
|
| 147 | + $width = min($width, ($maxLength + 11)); |
|
| 148 | + } else { |
|
| 149 | + $width = min($width, ($maxLength + 41)); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $width = max($width, 70); |
|
| 153 | + |
|
| 154 | + // Sort the data based on counts and source code. |
|
| 155 | + $sourceCodes = array_keys($sources); |
|
| 156 | + $counts = []; |
|
| 157 | + foreach ($sources as $source => $data) { |
|
| 158 | + $counts[$source] = $data['count']; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + array_multisort($counts, SORT_DESC, $sourceCodes, SORT_ASC, SORT_NATURAL, $sources); |
|
| 162 | + |
|
| 163 | + echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL; |
|
| 164 | + echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
| 165 | + if ($showSources === true) { |
|
| 166 | + if ($totalFixable > 0) { |
|
| 167 | + echo ' SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL; |
|
| 168 | + } else { |
|
| 169 | + echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL; |
|
| 170 | + } |
|
| 171 | + } else { |
|
| 172 | + if ($totalFixable > 0) { |
|
| 173 | + echo ' STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL; |
|
| 174 | + } else { |
|
| 175 | + echo 'STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + echo "\033[0m".str_repeat('-', $width).PHP_EOL; |
|
| 180 | + |
|
| 181 | + $fixableSources = 0; |
|
| 182 | + |
|
| 183 | + if ($showSources === true) { |
|
| 184 | + $maxSniffWidth = ($width - 7); |
|
| 185 | + } else { |
|
| 186 | + $maxSniffWidth = ($width - 37); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + if ($totalFixable > 0) { |
|
| 190 | + $maxSniffWidth -= 4; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + foreach ($sources as $source => $sourceData) { |
|
| 194 | + if ($totalFixable > 0) { |
|
| 195 | + echo '['; |
|
| 196 | + if ($sourceData['fixable'] === true) { |
|
| 197 | + echo 'x'; |
|
| 198 | + $fixableSources++; |
|
| 199 | + } else { |
|
| 200 | + echo ' '; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + echo '] '; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if ($showSources === true) { |
|
| 207 | + if (strlen($source) > $maxSniffWidth) { |
|
| 208 | + $source = substr($source, 0, $maxSniffWidth); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + echo $source; |
|
| 212 | + if ($totalFixable > 0) { |
|
| 213 | + echo str_repeat(' ', ($width - 9 - strlen($source))); |
|
| 214 | + } else { |
|
| 215 | + echo str_repeat(' ', ($width - 5 - strlen($source))); |
|
| 216 | + } |
|
| 217 | + } else { |
|
| 218 | + $parts = $sourceData['parts']; |
|
| 219 | + |
|
| 220 | + if (strlen($parts[0]) > 8) { |
|
| 221 | + $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1)); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + echo $parts[0].str_repeat(' ', (10 - strlen($parts[0]))); |
|
| 225 | + |
|
| 226 | + $category = $parts[1]; |
|
| 227 | + if (strlen($category) > 18) { |
|
| 228 | + $category = substr($category, 0, ((strlen($category) - 18) * -1)); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + echo $category.str_repeat(' ', (20 - strlen($category))); |
|
| 232 | + |
|
| 233 | + $sniff = $parts[2]; |
|
| 234 | + if (strlen($sniff) > $maxSniffWidth) { |
|
| 235 | + $sniff = substr($sniff, 0, $maxSniffWidth); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + if ($totalFixable > 0) { |
|
| 239 | + echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff))); |
|
| 240 | + } else { |
|
| 241 | + echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff))); |
|
| 242 | + } |
|
| 243 | + }//end if |
|
| 244 | + |
|
| 245 | + echo $sourceData['count'].PHP_EOL; |
|
| 246 | + }//end foreach |
|
| 247 | + |
|
| 248 | + echo str_repeat('-', $width).PHP_EOL; |
|
| 249 | + echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION'; |
|
| 250 | + if (($totalErrors + $totalWarnings) > 1) { |
|
| 251 | + echo 'S'; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + echo ' WERE FOUND IN '.count($sources).' SOURCE'; |
|
| 255 | + if (count($sources) !== 1) { |
|
| 256 | + echo 'S'; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + echo "\033[0m"; |
|
| 260 | + |
|
| 261 | + if ($totalFixable > 0) { |
|
| 262 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
| 263 | + echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
| 267 | + |
|
| 268 | + if ($toScreen === true && $interactive === false) { |
|
| 269 | + Timing::printRunTime(); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + }//end generate() |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Converts a camel caps name into a readable string. |
|
| 277 | + * |
|
| 278 | + * @param string $name The camel caps name to convert. |
|
| 279 | + * |
|
| 280 | + * @return string |
|
| 281 | + */ |
|
| 282 | + public function makeFriendlyName($name) |
|
| 283 | + { |
|
| 284 | + if (trim($name) === '') { |
|
| 285 | + return ''; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $friendlyName = ''; |
|
| 289 | + $length = strlen($name); |
|
| 290 | + |
|
| 291 | + $lastWasUpper = false; |
|
| 292 | + $lastWasNumeric = false; |
|
| 293 | + for ($i = 0; $i < $length; $i++) { |
|
| 294 | + if (is_numeric($name[$i]) === true) { |
|
| 295 | + if ($lastWasNumeric === false) { |
|
| 296 | + $friendlyName .= ' '; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + $lastWasUpper = false; |
|
| 300 | + $lastWasNumeric = true; |
|
| 301 | + } else { |
|
| 302 | + $lastWasNumeric = false; |
|
| 303 | + |
|
| 304 | + $char = strtolower($name[$i]); |
|
| 305 | + if ($char === $name[$i]) { |
|
| 306 | + // Lowercase. |
|
| 307 | + $lastWasUpper = false; |
|
| 308 | + } else { |
|
| 309 | + // Uppercase. |
|
| 310 | + if ($lastWasUpper === false) { |
|
| 311 | + $friendlyName .= ' '; |
|
| 312 | + if ($i < ($length - 1)) { |
|
| 313 | + $next = $name[($i + 1)]; |
|
| 314 | + if (strtolower($next) === $next) { |
|
| 315 | + // Next char is lowercase so it is a word boundary. |
|
| 316 | + $name[$i] = strtolower($name[$i]); |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $lastWasUpper = true; |
|
| 322 | + } |
|
| 323 | + }//end if |
|
| 324 | + |
|
| 325 | + $friendlyName .= $name[$i]; |
|
| 326 | + }//end for |
|
| 327 | + |
|
| 328 | + $friendlyName = trim($friendlyName); |
|
| 329 | + $friendlyName[0] = strtoupper($friendlyName[0]); |
|
| 330 | + |
|
| 331 | + return $friendlyName; |
|
| 332 | + |
|
| 333 | + }//end makeFriendlyName() |
|
| 334 | 334 | |
| 335 | 335 | |
| 336 | 336 | }//end class |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | * |
| 95 | 95 | * @return bool |
| 96 | 96 | */ |
| 97 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 97 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
| 98 | 98 | { |
| 99 | 99 | echo $report['filename'].PHP_EOL; |
| 100 | 100 | |
@@ -127,10 +127,10 @@ discard block |
||
| 127 | 127 | $totalErrors, |
| 128 | 128 | $totalWarnings, |
| 129 | 129 | $totalFixable, |
| 130 | - $showSources=false, |
|
| 131 | - $width=80, |
|
| 132 | - $interactive=false, |
|
| 133 | - $toScreen=true |
|
| 130 | + $showSources = false, |
|
| 131 | + $width = 80, |
|
| 132 | + $interactive = false, |
|
| 133 | + $toScreen = true |
|
| 134 | 134 | ) { |
| 135 | 135 | $checkedFiles = explode(PHP_EOL, trim($cachedData)); |
| 136 | 136 | |
@@ -23,220 +23,220 @@ |
||
| 23 | 23 | class Notifysend implements Report |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Notification timeout in milliseconds. |
|
| 28 | - * |
|
| 29 | - * @var integer |
|
| 30 | - */ |
|
| 31 | - protected $timeout = 3000; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Path to notify-send command. |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $path = 'notify-send'; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Show "ok, all fine" messages. |
|
| 42 | - * |
|
| 43 | - * @var boolean |
|
| 44 | - */ |
|
| 45 | - protected $showOk = true; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Version of installed notify-send executable. |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $version = null; |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Load configuration data. |
|
| 57 | - */ |
|
| 58 | - public function __construct() |
|
| 59 | - { |
|
| 60 | - $path = Config::getExecutablePath('notifysend'); |
|
| 61 | - if ($path !== null) { |
|
| 62 | - $this->path = Common::escapeshellcmd($path); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $timeout = Config::getConfigData('notifysend_timeout'); |
|
| 66 | - if ($timeout !== null) { |
|
| 67 | - $this->timeout = (int) $timeout; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $showOk = Config::getConfigData('notifysend_showok'); |
|
| 71 | - if ($showOk !== null) { |
|
| 72 | - $this->showOk = (bool) $showOk; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $this->version = str_replace( |
|
| 76 | - 'notify-send ', |
|
| 77 | - '', |
|
| 78 | - exec($this->path.' --version') |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - }//end __construct() |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Generate a partial report for a single processed file. |
|
| 86 | - * |
|
| 87 | - * Function should return TRUE if it printed or stored data about the file |
|
| 88 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 89 | - * its data should be counted in the grand totals. |
|
| 90 | - * |
|
| 91 | - * @param array $report Prepared report data. |
|
| 92 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 93 | - * @param bool $showSources Show sources? |
|
| 94 | - * @param int $width Maximum allowed line width. |
|
| 95 | - * |
|
| 96 | - * @return bool |
|
| 97 | - */ |
|
| 98 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 99 | - { |
|
| 100 | - echo $report['filename'].PHP_EOL; |
|
| 101 | - |
|
| 102 | - // We want this file counted in the total number |
|
| 103 | - // of checked files even if it has no errors. |
|
| 104 | - return true; |
|
| 105 | - |
|
| 106 | - }//end generateFileReport() |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Generates a summary of errors and warnings for each file processed. |
|
| 111 | - * |
|
| 112 | - * @param string $cachedData Any partial report data that was returned from |
|
| 113 | - * generateFileReport during the run. |
|
| 114 | - * @param int $totalFiles Total number of files processed during the run. |
|
| 115 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 116 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 117 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
| 118 | - * @param bool $showSources Show sources? |
|
| 119 | - * @param int $width Maximum allowed line width. |
|
| 120 | - * @param bool $interactive Are we running in interactive mode? |
|
| 121 | - * @param bool $toScreen Is the report being printed to screen? |
|
| 122 | - * |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function generate( |
|
| 126 | - $cachedData, |
|
| 127 | - $totalFiles, |
|
| 128 | - $totalErrors, |
|
| 129 | - $totalWarnings, |
|
| 130 | - $totalFixable, |
|
| 131 | - $showSources=false, |
|
| 132 | - $width=80, |
|
| 133 | - $interactive=false, |
|
| 134 | - $toScreen=true |
|
| 135 | - ) { |
|
| 136 | - $checkedFiles = explode(PHP_EOL, trim($cachedData)); |
|
| 137 | - |
|
| 138 | - $msg = $this->generateMessage($checkedFiles, $totalErrors, $totalWarnings); |
|
| 139 | - if ($msg === null) { |
|
| 140 | - if ($this->showOk === true) { |
|
| 141 | - $this->notifyAllFine(); |
|
| 142 | - } |
|
| 143 | - } else { |
|
| 144 | - $this->notifyErrors($msg); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - }//end generate() |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Generate the error message to show to the user. |
|
| 152 | - * |
|
| 153 | - * @param string[] $checkedFiles The files checked during the run. |
|
| 154 | - * @param int $totalErrors Total number of errors found during the run. |
|
| 155 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
| 156 | - * |
|
| 157 | - * @return string Error message or NULL if no error/warning found. |
|
| 158 | - */ |
|
| 159 | - protected function generateMessage($checkedFiles, $totalErrors, $totalWarnings) |
|
| 160 | - { |
|
| 161 | - if ($totalErrors === 0 && $totalWarnings === 0) { |
|
| 162 | - // Nothing to print. |
|
| 163 | - return null; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $totalFiles = count($checkedFiles); |
|
| 167 | - |
|
| 168 | - $msg = ''; |
|
| 169 | - if ($totalFiles > 1) { |
|
| 170 | - $msg .= 'Checked '.$totalFiles.' files'.PHP_EOL; |
|
| 171 | - } else { |
|
| 172 | - $msg .= $checkedFiles[0].PHP_EOL; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - if ($totalWarnings > 0) { |
|
| 176 | - $msg .= $totalWarnings.' warnings'.PHP_EOL; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - if ($totalErrors > 0) { |
|
| 180 | - $msg .= $totalErrors.' errors'.PHP_EOL; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $msg; |
|
| 184 | - |
|
| 185 | - }//end generateMessage() |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Tell the user that all is fine and no error/warning has been found. |
|
| 190 | - * |
|
| 191 | - * @return void |
|
| 192 | - */ |
|
| 193 | - protected function notifyAllFine() |
|
| 194 | - { |
|
| 195 | - $cmd = $this->getBasicCommand(); |
|
| 196 | - $cmd .= ' -i info'; |
|
| 197 | - $cmd .= ' "PHP CodeSniffer: Ok"'; |
|
| 198 | - $cmd .= ' "All fine"'; |
|
| 199 | - exec($cmd); |
|
| 200 | - |
|
| 201 | - }//end notifyAllFine() |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Tell the user that errors/warnings have been found. |
|
| 206 | - * |
|
| 207 | - * @param string $msg Message to display. |
|
| 208 | - * |
|
| 209 | - * @return void |
|
| 210 | - */ |
|
| 211 | - protected function notifyErrors($msg) |
|
| 212 | - { |
|
| 213 | - $cmd = $this->getBasicCommand(); |
|
| 214 | - $cmd .= ' -i error'; |
|
| 215 | - $cmd .= ' "PHP CodeSniffer: Error"'; |
|
| 216 | - $cmd .= ' '.escapeshellarg(trim($msg)); |
|
| 217 | - exec($cmd); |
|
| 218 | - |
|
| 219 | - }//end notifyErrors() |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Generate and return the basic notify-send command string to execute. |
|
| 224 | - * |
|
| 225 | - * @return string Shell command with common parameters. |
|
| 226 | - */ |
|
| 227 | - protected function getBasicCommand() |
|
| 228 | - { |
|
| 229 | - $cmd = $this->path; |
|
| 230 | - $cmd .= ' --category dev.validate'; |
|
| 231 | - $cmd .= ' -h int:transient:1'; |
|
| 232 | - $cmd .= ' -t '.(int) $this->timeout; |
|
| 233 | - if (version_compare($this->version, '0.7.3', '>=') === true) { |
|
| 234 | - $cmd .= ' -a phpcs'; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return $cmd; |
|
| 238 | - |
|
| 239 | - }//end getBasicCommand() |
|
| 26 | + /** |
|
| 27 | + * Notification timeout in milliseconds. |
|
| 28 | + * |
|
| 29 | + * @var integer |
|
| 30 | + */ |
|
| 31 | + protected $timeout = 3000; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Path to notify-send command. |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $path = 'notify-send'; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Show "ok, all fine" messages. |
|
| 42 | + * |
|
| 43 | + * @var boolean |
|
| 44 | + */ |
|
| 45 | + protected $showOk = true; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Version of installed notify-send executable. |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $version = null; |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Load configuration data. |
|
| 57 | + */ |
|
| 58 | + public function __construct() |
|
| 59 | + { |
|
| 60 | + $path = Config::getExecutablePath('notifysend'); |
|
| 61 | + if ($path !== null) { |
|
| 62 | + $this->path = Common::escapeshellcmd($path); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $timeout = Config::getConfigData('notifysend_timeout'); |
|
| 66 | + if ($timeout !== null) { |
|
| 67 | + $this->timeout = (int) $timeout; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $showOk = Config::getConfigData('notifysend_showok'); |
|
| 71 | + if ($showOk !== null) { |
|
| 72 | + $this->showOk = (bool) $showOk; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $this->version = str_replace( |
|
| 76 | + 'notify-send ', |
|
| 77 | + '', |
|
| 78 | + exec($this->path.' --version') |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + }//end __construct() |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Generate a partial report for a single processed file. |
|
| 86 | + * |
|
| 87 | + * Function should return TRUE if it printed or stored data about the file |
|
| 88 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
| 89 | + * its data should be counted in the grand totals. |
|
| 90 | + * |
|
| 91 | + * @param array $report Prepared report data. |
|
| 92 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
| 93 | + * @param bool $showSources Show sources? |
|
| 94 | + * @param int $width Maximum allowed line width. |
|
| 95 | + * |
|
| 96 | + * @return bool |
|
| 97 | + */ |
|
| 98 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
| 99 | + { |
|
| 100 | + echo $report['filename'].PHP_EOL; |
|
| 101 | + |
|
| 102 | + // We want this file counted in the total number |
|
| 103 | + // of checked files even if it has no errors. |
|
| 104 | + return true; |
|
| 105 | + |
|
| 106 | + }//end generateFileReport() |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Generates a summary of errors and warnings for each file processed. |
|
| 111 | + * |
|
| 112 | + * @param string $cachedData Any partial report data that was returned from |
|
| 113 | + * generateFileReport during the run. |
|
| 114 | + * @param int $totalFiles Total number of files processed during the run. |
|
| 115 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 116 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 117 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
| 118 | + * @param bool $showSources Show sources? |
|
| 119 | + * @param int $width Maximum allowed line width. |
|
| 120 | + * @param bool $interactive Are we running in interactive mode? |
|
| 121 | + * @param bool $toScreen Is the report being printed to screen? |
|
| 122 | + * |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function generate( |
|
| 126 | + $cachedData, |
|
| 127 | + $totalFiles, |
|
| 128 | + $totalErrors, |
|
| 129 | + $totalWarnings, |
|
| 130 | + $totalFixable, |
|
| 131 | + $showSources=false, |
|
| 132 | + $width=80, |
|
| 133 | + $interactive=false, |
|
| 134 | + $toScreen=true |
|
| 135 | + ) { |
|
| 136 | + $checkedFiles = explode(PHP_EOL, trim($cachedData)); |
|
| 137 | + |
|
| 138 | + $msg = $this->generateMessage($checkedFiles, $totalErrors, $totalWarnings); |
|
| 139 | + if ($msg === null) { |
|
| 140 | + if ($this->showOk === true) { |
|
| 141 | + $this->notifyAllFine(); |
|
| 142 | + } |
|
| 143 | + } else { |
|
| 144 | + $this->notifyErrors($msg); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + }//end generate() |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Generate the error message to show to the user. |
|
| 152 | + * |
|
| 153 | + * @param string[] $checkedFiles The files checked during the run. |
|
| 154 | + * @param int $totalErrors Total number of errors found during the run. |
|
| 155 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
| 156 | + * |
|
| 157 | + * @return string Error message or NULL if no error/warning found. |
|
| 158 | + */ |
|
| 159 | + protected function generateMessage($checkedFiles, $totalErrors, $totalWarnings) |
|
| 160 | + { |
|
| 161 | + if ($totalErrors === 0 && $totalWarnings === 0) { |
|
| 162 | + // Nothing to print. |
|
| 163 | + return null; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $totalFiles = count($checkedFiles); |
|
| 167 | + |
|
| 168 | + $msg = ''; |
|
| 169 | + if ($totalFiles > 1) { |
|
| 170 | + $msg .= 'Checked '.$totalFiles.' files'.PHP_EOL; |
|
| 171 | + } else { |
|
| 172 | + $msg .= $checkedFiles[0].PHP_EOL; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + if ($totalWarnings > 0) { |
|
| 176 | + $msg .= $totalWarnings.' warnings'.PHP_EOL; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + if ($totalErrors > 0) { |
|
| 180 | + $msg .= $totalErrors.' errors'.PHP_EOL; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $msg; |
|
| 184 | + |
|
| 185 | + }//end generateMessage() |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Tell the user that all is fine and no error/warning has been found. |
|
| 190 | + * |
|
| 191 | + * @return void |
|
| 192 | + */ |
|
| 193 | + protected function notifyAllFine() |
|
| 194 | + { |
|
| 195 | + $cmd = $this->getBasicCommand(); |
|
| 196 | + $cmd .= ' -i info'; |
|
| 197 | + $cmd .= ' "PHP CodeSniffer: Ok"'; |
|
| 198 | + $cmd .= ' "All fine"'; |
|
| 199 | + exec($cmd); |
|
| 200 | + |
|
| 201 | + }//end notifyAllFine() |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Tell the user that errors/warnings have been found. |
|
| 206 | + * |
|
| 207 | + * @param string $msg Message to display. |
|
| 208 | + * |
|
| 209 | + * @return void |
|
| 210 | + */ |
|
| 211 | + protected function notifyErrors($msg) |
|
| 212 | + { |
|
| 213 | + $cmd = $this->getBasicCommand(); |
|
| 214 | + $cmd .= ' -i error'; |
|
| 215 | + $cmd .= ' "PHP CodeSniffer: Error"'; |
|
| 216 | + $cmd .= ' '.escapeshellarg(trim($msg)); |
|
| 217 | + exec($cmd); |
|
| 218 | + |
|
| 219 | + }//end notifyErrors() |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Generate and return the basic notify-send command string to execute. |
|
| 224 | + * |
|
| 225 | + * @return string Shell command with common parameters. |
|
| 226 | + */ |
|
| 227 | + protected function getBasicCommand() |
|
| 228 | + { |
|
| 229 | + $cmd = $this->path; |
|
| 230 | + $cmd .= ' --category dev.validate'; |
|
| 231 | + $cmd .= ' -h int:transient:1'; |
|
| 232 | + $cmd .= ' -t '.(int) $this->timeout; |
|
| 233 | + if (version_compare($this->version, '0.7.3', '>=') === true) { |
|
| 234 | + $cmd .= ' -a phpcs'; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return $cmd; |
|
| 238 | + |
|
| 239 | + }//end getBasicCommand() |
|
| 240 | 240 | |
| 241 | 241 | |
| 242 | 242 | }//end class |