Chris@17: Chris@17: * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) Chris@17: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@17: */ Chris@17: Chris@17: namespace PHP_CodeSniffer\Reports; Chris@17: Chris@17: use PHP_CodeSniffer\Files\File; Chris@17: use PHP_CodeSniffer\Util\Timing; Chris@17: Chris@17: class Info implements Report Chris@17: { Chris@17: Chris@17: Chris@17: /** Chris@17: * Generate a partial report for a single processed file. Chris@17: * Chris@17: * Function should return TRUE if it printed or stored data about the file Chris@17: * and FALSE if it ignored the file. Returning TRUE indicates that the file and Chris@17: * its data should be counted in the grand totals. Chris@17: * Chris@17: * @param array $report Prepared report data. Chris@17: * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. Chris@17: * @param bool $showSources Show sources? Chris@17: * @param int $width Maximum allowed line width. Chris@17: * Chris@17: * @return bool Chris@17: */ Chris@17: public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) Chris@17: { Chris@17: $metrics = $phpcsFile->getMetrics(); Chris@17: foreach ($metrics as $metric => $data) { Chris@17: foreach ($data['values'] as $value => $count) { Chris@17: echo "$metric>>$value>>$count".PHP_EOL; Chris@17: } Chris@17: } Chris@17: Chris@17: return true; Chris@17: Chris@17: }//end generateFileReport() Chris@17: Chris@17: Chris@17: /** Chris@17: * Prints the source of all errors and warnings. Chris@17: * Chris@17: * @param string $cachedData Any partial report data that was returned from Chris@17: * generateFileReport during the run. Chris@17: * @param int $totalFiles Total number of files processed during the run. Chris@17: * @param int $totalErrors Total number of errors found during the run. Chris@17: * @param int $totalWarnings Total number of warnings found during the run. Chris@17: * @param int $totalFixable Total number of problems that can be fixed. Chris@17: * @param bool $showSources Show sources? Chris@17: * @param int $width Maximum allowed line width. Chris@17: * @param bool $interactive Are we running in interactive mode? Chris@17: * @param bool $toScreen Is the report being printed to screen? Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: public function generate( Chris@17: $cachedData, Chris@17: $totalFiles, Chris@17: $totalErrors, Chris@17: $totalWarnings, Chris@17: $totalFixable, Chris@17: $showSources=false, Chris@17: $width=80, Chris@17: $interactive=false, Chris@17: $toScreen=true Chris@17: ) { Chris@17: $lines = explode(PHP_EOL, $cachedData); Chris@17: array_pop($lines); Chris@17: Chris@17: if (empty($lines) === true) { Chris@17: return; Chris@17: } Chris@17: Chris@17: $metrics = []; Chris@17: foreach ($lines as $line) { Chris@17: $parts = explode('>>', $line); Chris@17: $metric = $parts[0]; Chris@17: $value = $parts[1]; Chris@17: $count = $parts[2]; Chris@17: if (isset($metrics[$metric]) === false) { Chris@17: $metrics[$metric] = []; Chris@17: } Chris@17: Chris@17: if (isset($metrics[$metric][$value]) === false) { Chris@17: $metrics[$metric][$value] = $count; Chris@17: } else { Chris@17: $metrics[$metric][$value] += $count; Chris@17: } Chris@17: } Chris@17: Chris@17: ksort($metrics); Chris@17: Chris@17: echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL; Chris@17: echo str_repeat('-', 70).PHP_EOL; Chris@17: Chris@17: foreach ($metrics as $metric => $values) { Chris@17: if (count($values) === 1) { Chris@17: $count = reset($values); Chris@17: $value = key($values); Chris@17: Chris@17: echo "$metric: \033[4m$value\033[0m [$count/$count, 100%]".PHP_EOL; Chris@17: } else { Chris@17: $totalCount = 0; Chris@17: $valueWidth = 0; Chris@17: foreach ($values as $value => $count) { Chris@17: $totalCount += $count; Chris@17: $valueWidth = max($valueWidth, strlen($value)); Chris@17: } Chris@17: Chris@17: $countWidth = strlen($totalCount); Chris@17: $nrOfThousandSeps = floor($countWidth / 3); Chris@17: $countWidth += $nrOfThousandSeps; Chris@17: Chris@17: // Account for 'total' line. Chris@17: $valueWidth = max(5, $valueWidth); Chris@17: Chris@17: echo "$metric:".PHP_EOL; Chris@17: Chris@17: ksort($values, SORT_NATURAL); Chris@17: arsort($values); Chris@17: Chris@17: $percentPrefixWidth = 0; Chris@17: $percentWidth = 6; Chris@17: foreach ($values as $value => $count) { Chris@17: $percent = round(($count / $totalCount * 100), 2); Chris@17: $percentPrefix = ''; Chris@17: if ($percent === 0.00) { Chris@17: $percent = 0.01; Chris@17: $percentPrefix = '<'; Chris@17: $percentPrefixWidth = 2; Chris@17: $percentWidth = 4; Chris@17: } Chris@17: Chris@17: printf( Chris@17: "\t%-{$valueWidth}s => %{$countWidth}s (%{$percentPrefixWidth}s%{$percentWidth}.2f%%)".PHP_EOL, Chris@17: $value, Chris@17: number_format($count), Chris@17: $percentPrefix, Chris@17: $percent Chris@17: ); Chris@17: } Chris@17: Chris@17: echo "\t".str_repeat('-', ($valueWidth + $countWidth + 15)).PHP_EOL; Chris@17: printf( Chris@17: "\t%-{$valueWidth}s => %{$countWidth}s (100.00%%)".PHP_EOL, Chris@17: 'total', Chris@17: number_format($totalCount) Chris@17: ); Chris@17: }//end if Chris@17: Chris@17: echo PHP_EOL; Chris@17: }//end foreach Chris@17: Chris@17: echo str_repeat('-', 70).PHP_EOL; Chris@17: Chris@17: if ($toScreen === true && $interactive === false) { Chris@17: Timing::printRunTime(); Chris@17: } Chris@17: Chris@17: }//end generate() Chris@17: Chris@17: Chris@17: }//end class