Chris@0: Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Info report for PHP_CodeSniffer. Chris@0: * Chris@0: * PHP version 5 Chris@0: * Chris@0: * @category PHP Chris@0: * @package PHP_CodeSniffer Chris@0: * @author Greg Sherwood Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @version Release: @package_version@ Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: class PHP_CodeSniffer_Reports_Info implements PHP_CodeSniffer_Report Chris@0: { Chris@0: Chris@0: /** Chris@0: * TRUE if this report needs error messages instead of just totals. Chris@0: * Chris@0: * @var boolean Chris@0: */ Chris@0: public $recordErrors = false; Chris@0: Chris@0: /** Chris@0: * A cache of metrics collected during the run. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: private $_metricCache = array(); Chris@0: Chris@0: Chris@0: /** Chris@0: * Generate a partial report for a single processed file. Chris@0: * Chris@0: * Function should return TRUE if it printed or stored data about the file Chris@0: * and FALSE if it ignored the file. Returning TRUE indicates that the file and Chris@0: * its data should be counted in the grand totals. Chris@0: * Chris@0: * @param array $report Prepared report data. Chris@0: * @param PHP_CodeSniffer_File $phpcsFile The file being reported on. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * Chris@0: * @return boolean Chris@0: */ Chris@0: public function generateFileReport( Chris@0: $report, Chris@0: PHP_CodeSniffer_File $phpcsFile, Chris@0: $showSources=false, Chris@0: $width=80 Chris@0: ) { Chris@0: $metrics = $phpcsFile->getMetrics(); Chris@0: foreach ($metrics as $metric => $data) { Chris@0: if (isset($this->_metricCache[$metric]) === false) { Chris@0: $this->_metricCache[$metric] = array(); Chris@0: } Chris@0: Chris@0: foreach ($data['values'] as $value => $locations) { Chris@0: $locations = array_unique($locations); Chris@0: $count = count($locations); Chris@0: Chris@0: if (isset($this->_metricCache[$metric][$value]) === false) { Chris@0: $this->_metricCache[$metric][$value] = $count; Chris@0: } else { Chris@0: $this->_metricCache[$metric][$value] += $count; Chris@0: } Chris@0: } Chris@0: }//end foreach Chris@0: Chris@0: return true; Chris@0: Chris@0: }//end generateFileReport() Chris@0: Chris@0: Chris@0: /** Chris@0: * Prints the source of all errors and warnings. Chris@0: * Chris@0: * @param string $cachedData Any partial report data that was returned from Chris@0: * generateFileReport during the run. Chris@0: * @param int $totalFiles Total number of files processed during the run. Chris@0: * @param int $totalErrors Total number of errors found during the run. Chris@0: * @param int $totalWarnings Total number of warnings found during the run. Chris@0: * @param int $totalFixable Total number of problems that can be fixed. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * @param boolean $toScreen Is the report being printed to screen? Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function generate( Chris@0: $cachedData, Chris@0: $totalFiles, Chris@0: $totalErrors, Chris@0: $totalWarnings, Chris@0: $totalFixable, Chris@0: $showSources=false, Chris@0: $width=80, Chris@0: $toScreen=true Chris@0: ) { Chris@0: if (empty($this->_metricCache) === true) { Chris@0: // Nothing to show. Chris@0: return; Chris@0: } Chris@0: Chris@0: ksort($this->_metricCache); Chris@0: Chris@0: echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL; Chris@0: echo str_repeat('-', 70).PHP_EOL; Chris@0: Chris@0: foreach ($this->_metricCache as $metric => $values) { Chris@0: $winner = ''; Chris@0: $winnerCount = 0; Chris@0: $totalCount = 0; Chris@0: foreach ($values as $value => $count) { Chris@0: $totalCount += $count; Chris@0: if ($count > $winnerCount) { Chris@0: $winner = $value; Chris@0: $winnerCount = $count; Chris@0: } Chris@0: } Chris@0: Chris@0: $winPercent = round(($winnerCount / $totalCount * 100), 2); Chris@0: echo "$metric: \033[4m$winner\033[0m [$winnerCount/$totalCount, $winPercent%]".PHP_EOL; Chris@0: Chris@0: asort($values); Chris@0: $values = array_reverse($values, true); Chris@0: foreach ($values as $value => $count) { Chris@0: if ($value === $winner) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $percent = round(($count / $totalCount * 100), 2); Chris@0: echo "\t$value => $count ($percent%)".PHP_EOL; Chris@0: } Chris@0: Chris@0: echo PHP_EOL; Chris@0: }//end foreach Chris@0: Chris@0: echo str_repeat('-', 70).PHP_EOL; Chris@0: Chris@0: if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) { Chris@0: PHP_CodeSniffer_Reporting::printRunTime(); Chris@0: } Chris@0: Chris@0: }//end generate() Chris@0: Chris@0: Chris@0: }//end class