/** * Prints a summary of fixed files. * * @param string $cachedData Any partial report data that was returned from * generateFileReport during the run. * @param int $totalFiles Total number of files processed during the run. * @param int $totalErrors Total number of errors found during the run. * @param int $totalWarnings Total number of warnings found during the run. * @param int $totalFixable Total number of problems that can be fixed. * @param bool $showSources Show sources? * @param int $width Maximum allowed line width. * @param bool $interactive Are we running in interactive mode? * @param bool $toScreen Is the report being printed to screen? * * @return void */ public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $interactive = false, $toScreen = true) { $lines = explode(PHP_EOL, $cachedData); array_pop($lines); if (empty($lines) === true) { echo PHP_EOL . 'No fixable errors were found' . PHP_EOL; return; } $reportFiles = array(); $maxLength = 0; $totalFixed = 0; $failures = 0; foreach ($lines as $line) { $parts = explode('>>', $line); $fileLen = strlen($parts[0]); $reportFiles[$parts[0]] = array('errors' => $parts[1], 'warnings' => $parts[2], 'fixable' => $parts[3], 'fixed' => $parts[4], 'strlen' => $fileLen); $maxLength = max($maxLength, $fileLen); $totalFixed += $parts[4]; if ($parts[3] > 0) { $failures++; } } $width = min($width, $maxLength + 21); $width = max($width, 70); echo PHP_EOL . "[1m" . 'PHPCBF RESULT SUMMARY' . "[0m" . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL; echo "[1m" . 'FILE' . str_repeat(' ', $width - 20) . 'FIXED REMAINING' . "[0m" . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL; foreach ($reportFiles as $file => $data) { $padding = $width - 18 - $data['strlen']; if ($padding < 0) { $file = '...' . substr($file, $padding * -1 + 3); $padding = 0; } echo $file . str_repeat(' ', $padding) . ' '; if ($data['fixable'] > 0) { echo "[31mFAILED TO FIX[0m" . PHP_EOL; continue; } $remaining = $data['errors'] + $data['warnings']; if ($data['fixed'] !== 0) { echo $data['fixed']; echo str_repeat(' ', 7 - strlen((string) $data['fixed'])); } else { echo '0 '; } if ($data['reamining'] !== 0) { echo $remaining; } else { echo '0'; } echo PHP_EOL; } //end foreach echo str_repeat('-', $width) . PHP_EOL; echo "[1mA TOTAL OF {$totalFixed} ERROR"; if ($totalFixed !== 1) { echo 'S'; } $numFiles = count($reportFiles); echo ' WERE FIXED IN ' . $numFiles . ' FILE'; if ($numFiles !== 1) { echo 'S'; } echo "[0m"; if ($failures > 0) { echo PHP_EOL . str_repeat('-', $width) . PHP_EOL; echo "[1mPHPCBF FAILED TO FIX {$failures} FILE"; if ($failures !== 1) { echo 'S'; } echo "[0m"; } echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL; if ($toScreen === true && $interactive === false) { Util\Timing::printRunTime(); } }
/** * Prints all errors and warnings for each file processed. * * @param string $cachedData Any partial report data that was returned from * generateFileReport during the run. * @param int $totalFiles Total number of files processed during the run. * @param int $totalErrors Total number of errors found during the run. * @param int $totalWarnings Total number of warnings found during the run. * @param int $totalFixable Total number of problems that can be fixed. * @param bool $showSources Show sources? * @param int $width Maximum allowed line width. * @param bool $interactive Are we running in interactive mode? * @param bool $toScreen Is the report being printed to screen? * * @return void */ public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $interactive = false, $toScreen = true) { if ($cachedData === '') { return; } echo $cachedData; if ($toScreen === true && $interactive === false) { Util\Timing::printRunTime(); } }
/** * Prints the source of all errors and warnings. * * @param string $cachedData Any partial report data that was returned from * generateFileReport during the run. * @param int $totalFiles Total number of files processed during the run. * @param int $totalErrors Total number of errors found during the run. * @param int $totalWarnings Total number of warnings found during the run. * @param int $totalFixable Total number of problems that can be fixed. * @param bool $showSources Show sources? * @param int $width Maximum allowed line width. * @param bool $interactive Are we running in interactive mode? * @param bool $toScreen Is the report being printed to screen? * * @return void */ public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $interactive = false, $toScreen = true) { $lines = explode(PHP_EOL, $cachedData); array_pop($lines); if (empty($lines) === true) { return; } $sources = array(); $maxLength = 0; foreach ($lines as $line) { $parts = explode('>>', $line); $source = $parts[0]; $fixable = (bool) $parts[1]; $count = $parts[2]; if (isset($sources[$source]) === false) { if ($showSources === true) { $parts = null; $sniff = $source; } else { $parts = explode('.', $source); if ($parts[0] === 'Internal') { $parts[2] = $parts[1]; $parts[1] = ''; } $parts[1] = $this->makeFriendlyName($parts[1]); $sniff = $this->makeFriendlyName($parts[2]); if (isset($parts[3]) === true) { $name = $this->makeFriendlyName($parts[3]); $name[0] = strtolower($name[0]); $sniff .= ' ' . $name; unset($parts[3]); } $parts[2] = $sniff; } //end if $maxLength = max($maxLength, strlen($sniff)); $sources[$source] = array('count' => 1, 'fixable' => $fixable, 'parts' => $parts); } else { $sources[$source]['count']++; } //end if $fileLen = strlen($parts[0]); $reportFiles[$parts[0]] = array('errors' => $parts[1], 'warnings' => $parts[2], 'strlen' => $fileLen); } //end foreach if ($showSources === true) { $width = min($width, $maxLength + 11); } else { $width = min($width, $maxLength + 41); } $width = max($width, 70); asort($sources); $sources = array_reverse($sources); echo PHP_EOL . "[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY[0m" . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL . "[1m"; if ($showSources === true) { if ($totalFixable > 0) { echo ' SOURCE' . str_repeat(' ', $width - 15) . 'COUNT' . PHP_EOL; } else { echo 'SOURCE' . str_repeat(' ', $width - 11) . 'COUNT' . PHP_EOL; } } else { if ($totalFixable > 0) { echo ' STANDARD CATEGORY SNIFF' . str_repeat(' ', $width - 44) . 'COUNT' . PHP_EOL; } else { echo 'STANDARD CATEGORY SNIFF' . str_repeat(' ', $width - 40) . 'COUNT' . PHP_EOL; } } echo "[0m" . str_repeat('-', $width) . PHP_EOL; $fixableSources = 0; if ($showSources === true) { $maxSniffWidth = $width - 7; } else { $maxSniffWidth = $width - 37; } if ($totalFixable > 0) { $maxSniffWidth -= 4; } foreach ($sources as $source => $sourceData) { if ($totalFixable > 0) { echo '['; if ($sourceData['fixable'] === true) { echo 'x'; $fixableSources++; } else { echo ' '; } echo '] '; } if ($showSources === true) { if (strlen($source) > $maxSniffWidth) { $source = substr($source, 0, $maxSniffWidth); } echo $source; if ($totalFixable > 0) { echo str_repeat(' ', $width - 9 - strlen($source)); } else { echo str_repeat(' ', $width - 5 - strlen($source)); } } else { $parts = $sourceData['parts']; if (strlen($parts[0]) > 8) { $parts[0] = substr($parts[0], 0, (strlen($parts[0]) - 8) * -1); } echo $parts[0] . str_repeat(' ', 10 - strlen($parts[0])); $category = $parts[1]; if (strlen($category) > 18) { $category = substr($category, 0, (strlen($category) - 18) * -1); } echo $category . str_repeat(' ', 20 - strlen($category)); $sniff = $parts[2]; if (strlen($sniff) > $maxSniffWidth) { $sniff = substr($sniff, 0, $maxSniffWidth); } if ($totalFixable > 0) { echo $sniff . str_repeat(' ', $width - 39 - strlen($sniff)); } else { echo $sniff . str_repeat(' ', $width - 35 - strlen($sniff)); } } //end if echo $sourceData['count'] . PHP_EOL; } //end foreach echo str_repeat('-', $width) . PHP_EOL; echo "[1m" . 'A TOTAL OF ' . ($totalErrors + $totalWarnings) . ' SNIFF VIOLATION'; if ($totalErrors + $totalWarnings > 1) { echo 'S'; } echo ' WERE FOUND IN ' . count($sources) . ' SOURCE'; if (count($sources) !== 1) { echo 'S'; } echo "[0m"; if ($totalFixable > 0) { echo PHP_EOL . str_repeat('-', $width) . PHP_EOL; echo "[1mPHPCBF CAN FIX THE {$fixableSources} MARKED SOURCES AUTOMATICALLY ({$totalFixable} VIOLATIONS IN TOTAL)[0m"; } echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL; if ($toScreen === true && $interactive === false) { Timing::printRunTime(); } }
/** * Prints the source of all errors and warnings. * * @param string $cachedData Any partial report data that was returned from * generateFileReport during the run. * @param int $totalFiles Total number of files processed during the run. * @param int $totalErrors Total number of errors found during the run. * @param int $totalWarnings Total number of warnings found during the run. * @param int $totalFixable Total number of problems that can be fixed. * @param bool $showSources Show sources? * @param int $width Maximum allowed line width. * @param bool $interactive Are we running in interactive mode? * @param bool $toScreen Is the report being printed to screen? * * @return void */ public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $interactive = false, $toScreen = true) { $lines = explode(PHP_EOL, $cachedData); array_pop($lines); if (empty($lines) === true) { return; } $metrics = array(); foreach ($lines as $line) { $parts = explode('>>', $line); $metric = $parts[0]; $value = $parts[1]; $count = $parts[2]; if (isset($metrics[$metric]) === false) { $metrics[$metric] = array(); } if (isset($metrics[$metric][$value]) === false) { $metrics[$metric][$value] = $count; } else { $metrics[$metric][$value] += $count; } } ksort($metrics); echo PHP_EOL . "[1m" . 'PHP CODE SNIFFER INFORMATION REPORT' . "[0m" . PHP_EOL; echo str_repeat('-', 70) . PHP_EOL; foreach ($metrics as $metric => $values) { $winner = ''; $winnerCount = 0; $totalCount = 0; foreach ($values as $value => $count) { $totalCount += $count; if ($count > $winnerCount) { $winner = $value; $winnerCount = $count; } } $winPercent = round($winnerCount / $totalCount * 100, 2); echo "{$metric}: [4m{$winner}[0m [{$winnerCount}/{$totalCount}, {$winPercent}%]" . PHP_EOL; asort($values); $values = array_reverse($values, true); foreach ($values as $value => $count) { if ($value === $winner) { continue; } $percent = round($count / $totalCount * 100, 2); echo "\t{$value} => {$count} ({$percent}%)" . PHP_EOL; } echo PHP_EOL; } //end foreach echo str_repeat('-', 70) . PHP_EOL; if ($toScreen === true && $interactive === false) { Timing::printRunTime(); } }
/** * Prints the author of all errors and warnings, as given by "version control blame". * * @param string $cachedData Any partial report data that was returned from * generateFileReport during the run. * @param int $totalFiles Total number of files processed during the run. * @param int $totalErrors Total number of errors found during the run. * @param int $totalWarnings Total number of warnings found during the run. * @param int $totalFixable Total number of problems that can be fixed. * @param bool $showSources Show sources? * @param int $width Maximum allowed line width. * @param bool $interactive Are we running in interactive mode? * @param bool $toScreen Is the report being printed to screen? * * @return void */ public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $interactive = false, $toScreen = true) { $errorsShown = $totalErrors + $totalWarnings; if ($errorsShown === 0) { // Nothing to show. return; } $lines = explode(PHP_EOL, $cachedData); array_pop($lines); if (empty($lines) === true) { return; } $authorCache = array(); $praiseCache = array(); $sourceCache = array(); foreach ($lines as $line) { $parts = explode('>>', $line); switch ($parts[0]) { case 'AUTHOR': if (isset($authorCache[$parts[1]]) === false) { $authorCache[$parts[1]] = $parts[2]; } else { $authorCache[$parts[1]] += $parts[2]; } break; case 'PRAISE': if (isset($praiseCache[$parts[1]]) === false) { $praiseCache[$parts[1]] = array('good' => $parts[2], 'bad' => $parts[3]); } else { $praiseCache[$parts[1]]['good'] += $parts[2]; $praiseCache[$parts[1]]['bad'] += $parts[3]; } break; case 'SOURCE': if (isset($praiseCache[$parts[1]]) === false) { $praiseCache[$parts[1]] = array(); } if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { $sourceCache[$parts[1]][$parts[2]] = array('count' => $parts[3], 'fixable' => (bool) $parts[4]); } else { $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; } break; default: break; } //end switch } //end foreach // Make sure the report width isn't too big. $maxLength = 0; foreach ($authorCache as $author => $count) { $maxLength = max($maxLength, strlen($author)); if ($showSources === true && isset($sourceCache[$author]) === true) { foreach ($sourceCache[$author] as $source => $sourceData) { if ($source === 'count') { continue; } $maxLength = max($maxLength, strlen($source) + 9); } } } $width = min($width, $maxLength + 30); $width = max($width, 70); arsort($authorCache); echo PHP_EOL . "[1m" . 'PHP CODE SNIFFER ' . $this->reportName . ' BLAME SUMMARY' . "[0m" . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL . "[1m"; if ($showSources === true) { echo 'AUTHOR SOURCE' . str_repeat(' ', $width - 43) . '(Author %) (Overall %) COUNT' . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL; } else { echo 'AUTHOR' . str_repeat(' ', $width - 34) . '(Author %) (Overall %) COUNT' . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL; } echo "[0m"; if ($showSources === true) { $maxSniffWidth = $width - 15; if ($totalFixable > 0) { $maxSniffWidth -= 4; } } $fixableSources = 0; foreach ($authorCache as $author => $count) { if ($praiseCache[$author]['good'] === 0) { $percent = 0; } else { $total = $praiseCache[$author]['bad'] + $praiseCache[$author]['good']; $percent = round($praiseCache[$author]['bad'] / $total * 100, 2); } $overallPercent = '(' . round($count / $errorsShown * 100, 2) . ')'; $authorPercent = '(' . $percent . ')'; $line = str_repeat(' ', 6 - strlen($count)) . $count; $line = str_repeat(' ', 12 - strlen($overallPercent)) . $overallPercent . $line; $line = str_repeat(' ', 11 - strlen($authorPercent)) . $authorPercent . $line; $line = $author . str_repeat(' ', $width - strlen($author) - strlen($line)) . $line; if ($showSources === true) { $line = "[1m{$line}[0m"; } echo $line . PHP_EOL; if ($showSources === true && isset($sourceCache[$author]) === true) { $errors = $sourceCache[$author]; asort($errors); $errors = array_reverse($errors); foreach ($errors as $source => $sourceData) { if ($source === 'count') { continue; } $count = $sourceData['count']; $srcLength = strlen($source); if ($srcLength > $maxSniffWidth) { $source = substr($source, 0, $maxSniffWidth); } $line = str_repeat(' ', 5 - strlen($count)) . $count; echo ' '; if ($totalFixable > 0) { echo '['; if ($sourceData['fixable'] === true) { echo 'x'; $fixableSources++; } else { echo ' '; } echo '] '; } echo $source; if ($totalFixable > 0) { echo str_repeat(' ', $width - 18 - strlen($source)); } else { echo str_repeat(' ', $width - 14 - strlen($source)); } echo $line . PHP_EOL; } //end foreach } //end if } //end foreach echo str_repeat('-', $width) . PHP_EOL; echo "[1m" . 'A TOTAL OF ' . $errorsShown . ' SNIFF VIOLATION'; if ($errorsShown !== 1) { echo 'S'; } echo ' WERE COMMITTED BY ' . count($authorCache) . ' AUTHOR'; if (count($authorCache) !== 1) { echo 'S'; } echo "[0m"; if ($totalFixable > 0) { if ($showSources === true) { echo PHP_EOL . str_repeat('-', $width) . PHP_EOL; echo "[1mPHPCBF CAN FIX THE {$fixableSources} MARKED SOURCES AUTOMATICALLY ({$totalFixable} VIOLATIONS IN TOTAL)[0m"; } else { echo PHP_EOL . str_repeat('-', $width) . PHP_EOL; echo "[1mPHPCBF CAN FIX {$totalFixable} OF THESE SNIFF VIOLATIONS AUTOMATICALLY[0m"; } } echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL; if ($toScreen === true && $interactive === false) { Timing::printRunTime(); } }
/** * Generates a summary of errors and warnings for each file processed. * * @param string $cachedData Any partial report data that was returned from * generateFileReport during the run. * @param int $totalFiles Total number of files processed during the run. * @param int $totalErrors Total number of errors found during the run. * @param int $totalWarnings Total number of warnings found during the run. * @param int $totalFixable Total number of problems that can be fixed. * @param bool $showSources Show sources? * @param int $width Maximum allowed line width. * @param bool $interactive Are we running in interactive mode? * @param bool $toScreen Is the report being printed to screen? * * @return void */ public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $interactive = false, $toScreen = true) { $lines = explode(PHP_EOL, $cachedData); array_pop($lines); if (empty($lines) === true) { return; } $reportFiles = array(); $maxLength = 0; foreach ($lines as $line) { $parts = explode('>>', $line); $fileLen = strlen($parts[0]); $reportFiles[$parts[0]] = array('errors' => $parts[1], 'warnings' => $parts[2], 'strlen' => $fileLen); $maxLength = max($maxLength, $fileLen); } $width = min($width, $maxLength + 21); $width = max($width, 70); echo PHP_EOL . "[1m" . 'PHP CODE SNIFFER REPORT SUMMARY' . "[0m" . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL; echo "[1m" . 'FILE' . str_repeat(' ', $width - 20) . 'ERRORS WARNINGS' . "[0m" . PHP_EOL; echo str_repeat('-', $width) . PHP_EOL; foreach ($reportFiles as $file => $data) { $padding = $width - 18 - $data['strlen']; if ($padding < 0) { $file = '...' . substr($file, $padding * -1 + 3); $padding = 0; } echo $file . str_repeat(' ', $padding) . ' '; if ($data['errors'] !== 0) { echo "[31m" . $data['errors'] . "[0m"; echo str_repeat(' ', 8 - strlen((string) $data['errors'])); } else { echo '0 '; } if ($data['warnings'] !== 0) { echo "[33m" . $data['warnings'] . "[0m"; } else { echo '0'; } echo PHP_EOL; } //end foreach echo str_repeat('-', $width) . PHP_EOL; echo "[1mA TOTAL OF {$totalErrors} ERROR"; if ($totalErrors !== 1) { echo 'S'; } echo ' AND ' . $totalWarnings . ' WARNING'; if ($totalWarnings !== 1) { echo 'S'; } echo ' WERE FOUND IN ' . $totalFiles . ' FILE'; if ($totalFiles !== 1) { echo 'S'; } echo "[0m"; if ($totalFixable > 0) { echo PHP_EOL . str_repeat('-', $width) . PHP_EOL; echo "[1mPHPCBF CAN FIX {$totalFixable} OF THESE SNIFF VIOLATIONS AUTOMATICALLY[0m"; } echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL; if ($toScreen === true && $interactive === false) { Util\Timing::printRunTime(); } }
/** * Run the PHPCBF script. * * @return array */ public function runPHPCBF() { if (defined('PHP_CODESNIFFER_CBF') === false) { define('PHP_CODESNIFFER_CBF', true); } Util\Timing::startTiming(); Runner::checkRequirements(); // Creating the Config object populates it with all required settings // based on the CLI arguments provided to the script and any config // values the user has set. $this->config = new Config(); // When processing STDIN, we can't output anything to the screen // or it will end up mixed in with the file output. if ($this->config->stdin === true) { $this->config->verbosity = 0; } // Init the run and load the rulesets to set additional config vars. $this->init(); // Override some of the command line settings that might break the fixes. $this->config->generator = null; $this->config->explain = false; $this->config->interactive = false; $this->config->cache = false; $this->config->showSources = false; $this->config->recordErrors = false; $this->config->reportFile = null; $this->config->reports = array('cbf' => null); // If a standard tries to set command line arguments itself, some // may be blocked because PHPCBF is running, so stop the script // dying if any are found. $this->config->dieOnUnknownArg = false; $numErrors = $this->run(); $this->reporter->printReports(); echo PHP_EOL; Util\Timing::printRunTime(); // We can't tell exactly how many errors were fixed, but // we know how many errors were found. exit($numErrors); }