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