Chris@0: Chris@0: * @copyright 2009-2014 SQLI 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: * Version control report base class for PHP_CodeSniffer. Chris@0: * Chris@0: * PHP version 5 Chris@0: * Chris@0: * @category PHP Chris@0: * @package PHP_CodeSniffer Chris@0: * @author Ben Selby Chris@0: * @copyright 2009-2014 SQLI 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: 1.2.2 Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: abstract class PHP_CodeSniffer_Reports_VersionControl implements PHP_CodeSniffer_Report Chris@0: { Chris@0: Chris@0: /** Chris@0: * The name of the report we want in the output. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $reportName = 'VERSION CONTROL'; Chris@0: Chris@0: /** Chris@0: * A cache of author stats collected during the run. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: private $_authorCache = array(); Chris@0: Chris@0: /** Chris@0: * A cache of blame stats collected during the run. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: private $_praiseCache = array(); Chris@0: Chris@0: /** Chris@0: * A cache of source stats collected during the run. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: private $_sourceCache = 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: $blames = $this->getBlameContent($report['filename']); Chris@0: Chris@0: foreach ($report['messages'] as $line => $lineErrors) { Chris@0: $author = 'Unknown'; Chris@0: if (isset($blames[($line - 1)]) === true) { Chris@0: $blameAuthor = $this->getAuthor($blames[($line - 1)]); Chris@0: if ($blameAuthor !== false) { Chris@0: $author = $blameAuthor; Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($this->_authorCache[$author]) === false) { Chris@0: $this->_authorCache[$author] = 0; Chris@0: $this->_praiseCache[$author] = array( Chris@0: 'good' => 0, Chris@0: 'bad' => 0, Chris@0: ); Chris@0: } Chris@0: Chris@0: $this->_praiseCache[$author]['bad']++; Chris@0: Chris@0: foreach ($lineErrors as $column => $colErrors) { Chris@0: foreach ($colErrors as $error) { Chris@0: $this->_authorCache[$author]++; Chris@0: Chris@0: if ($showSources === true) { Chris@0: $source = $error['source']; Chris@0: if (isset($this->_sourceCache[$author][$source]) === false) { Chris@0: $this->_sourceCache[$author][$source] = array( Chris@0: 'count' => 1, Chris@0: 'fixable' => $error['fixable'], Chris@0: ); Chris@0: } else { Chris@0: $this->_sourceCache[$author][$source]['count']++; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: unset($blames[($line - 1)]); Chris@0: }//end foreach Chris@0: Chris@0: // No go through and give the authors some credit for Chris@0: // all the lines that do not have errors. Chris@0: foreach ($blames as $line) { Chris@0: $author = $this->getAuthor($line); Chris@0: if ($author === false) { Chris@0: $author = 'Unknown'; Chris@0: } Chris@0: Chris@0: if (isset($this->_authorCache[$author]) === false) { Chris@0: // This author doesn't have any errors. Chris@0: if (PHP_CODESNIFFER_VERBOSITY === 0) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $this->_authorCache[$author] = 0; Chris@0: $this->_praiseCache[$author] = array( Chris@0: 'good' => 0, Chris@0: 'bad' => 0, Chris@0: ); Chris@0: } Chris@0: Chris@0: $this->_praiseCache[$author]['good']++; 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 author of all errors and warnings, as given by "version control blame". 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: $errorsShown = ($totalErrors + $totalWarnings); Chris@0: if ($errorsShown === 0) { Chris@0: // Nothing to show. Chris@0: return; Chris@0: } Chris@0: Chris@0: // Make sure the report width isn't too big. Chris@0: $maxLength = 0; Chris@0: foreach ($this->_authorCache as $author => $count) { Chris@0: $maxLength = max($maxLength, strlen($author)); Chris@0: if ($showSources === true && isset($this->_sourceCache[$author]) === true) { Chris@0: foreach ($this->_sourceCache[$author] as $source => $sourceData) { Chris@0: if ($source === 'count') { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $maxLength = max($maxLength, (strlen($source) + 9)); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $width = min($width, ($maxLength + 30)); Chris@0: $width = max($width, 70); Chris@0: arsort($this->_authorCache); Chris@0: Chris@0: echo PHP_EOL."\033[1m".'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'."\033[0m".PHP_EOL; Chris@0: echo str_repeat('-', $width).PHP_EOL."\033[1m"; Chris@0: if ($showSources === true) { Chris@0: echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL; Chris@0: echo str_repeat('-', $width).PHP_EOL; Chris@0: } else { Chris@0: echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL; Chris@0: echo str_repeat('-', $width).PHP_EOL; Chris@0: } Chris@0: Chris@0: echo "\033[0m"; Chris@0: Chris@0: if ($showSources === true) { Chris@0: $maxSniffWidth = ($width - 15); Chris@0: Chris@0: if ($totalFixable > 0) { Chris@0: $maxSniffWidth -= 4; Chris@0: } Chris@0: } Chris@0: Chris@0: $fixableSources = 0; Chris@0: Chris@0: foreach ($this->_authorCache as $author => $count) { Chris@0: if ($this->_praiseCache[$author]['good'] === 0) { Chris@0: $percent = 0; Chris@0: } else { Chris@0: $total = ($this->_praiseCache[$author]['bad'] + $this->_praiseCache[$author]['good']); Chris@0: $percent = round(($this->_praiseCache[$author]['bad'] / $total * 100), 2); Chris@0: } Chris@0: Chris@0: $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')'; Chris@0: $authorPercent = '('.$percent.')'; Chris@0: $line = str_repeat(' ', (6 - strlen($count))).$count; Chris@0: $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line; Chris@0: $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line; Chris@0: $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line; Chris@0: Chris@0: if ($showSources === true) { Chris@0: $line = "\033[1m$line\033[0m"; Chris@0: } Chris@0: Chris@0: echo $line.PHP_EOL; Chris@0: Chris@0: if ($showSources === true && isset($this->_sourceCache[$author]) === true) { Chris@0: $errors = $this->_sourceCache[$author]; Chris@0: asort($errors); Chris@0: $errors = array_reverse($errors); Chris@0: Chris@0: foreach ($errors as $source => $sourceData) { Chris@0: if ($source === 'count') { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $count = $sourceData['count']; Chris@0: Chris@0: $srcLength = strlen($source); Chris@0: if ($srcLength > $maxSniffWidth) { Chris@0: $source = substr($source, 0, $maxSniffWidth); Chris@0: } Chris@0: Chris@0: $line = str_repeat(' ', (5 - strlen($count))).$count; Chris@0: Chris@0: echo ' '; Chris@0: if ($totalFixable > 0) { Chris@0: echo '['; Chris@0: if ($sourceData['fixable'] === true) { Chris@0: echo 'x'; Chris@0: $fixableSources++; Chris@0: } else { Chris@0: echo ' '; Chris@0: } Chris@0: Chris@0: echo '] '; Chris@0: } Chris@0: Chris@0: echo $source; Chris@0: if ($totalFixable > 0) { Chris@0: echo str_repeat(' ', ($width - 18 - strlen($source))); Chris@0: } else { Chris@0: echo str_repeat(' ', ($width - 14 - strlen($source))); Chris@0: } Chris@0: Chris@0: echo $line.PHP_EOL; Chris@0: }//end foreach Chris@0: }//end if Chris@0: }//end foreach Chris@0: Chris@0: echo str_repeat('-', $width).PHP_EOL; Chris@0: echo "\033[1m".'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION'; Chris@0: if ($errorsShown !== 1) { Chris@0: echo 'S'; Chris@0: } Chris@0: Chris@0: echo ' WERE COMMITTED BY '.count($this->_authorCache).' AUTHOR'; Chris@0: if (count($this->_authorCache) !== 1) { Chris@0: echo 'S'; Chris@0: } Chris@0: Chris@0: echo "\033[0m"; Chris@0: Chris@0: if ($totalFixable > 0) { Chris@0: if ($showSources === true) { Chris@0: echo PHP_EOL.str_repeat('-', $width).PHP_EOL; Chris@0: echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; Chris@0: } else { Chris@0: echo PHP_EOL.str_repeat('-', $width).PHP_EOL; Chris@0: echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; Chris@0: } Chris@0: } Chris@0: Chris@0: echo PHP_EOL.str_repeat('-', $width).PHP_EOL.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: /** Chris@0: * Extract the author from a blame line. Chris@0: * Chris@0: * @param string $line Line to parse. Chris@0: * Chris@0: * @return mixed string or false if impossible to recover. Chris@0: */ Chris@0: abstract protected function getAuthor($line); Chris@0: Chris@0: Chris@0: /** Chris@0: * Gets the blame output. Chris@0: * Chris@0: * @param string $filename File to blame. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: abstract protected function getBlameContent($filename); Chris@0: Chris@0: Chris@0: }//end class