|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * GitHub annotations report for PHP_CodeSniffer. |
| 4 | + * |
| 5 | + * @author Nico Hiort af Ornäs <[email protected]> |
| 6 | + * @author Brian Henry <[email protected]> |
| 7 | + * @author Greg Sherwood <[email protected]> |
| 8 | + * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
| 9 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 10 | + */ |
| 11 | + |
| 12 | +namespace PHP_CodeSniffer\Reports; |
| 13 | + |
| 14 | +use PHP_CodeSniffer\Files\File; |
| 15 | + |
| 16 | +class Github implements Report |
| 17 | +{ |
| 18 | + |
| 19 | + |
| 20 | + /** |
| 21 | + * Escapes a message for GitHub Actions annotation syntax. |
| 22 | + * |
| 23 | + * @param string $message The message to escape. |
| 24 | + * |
| 25 | + * @return string The escaped message. |
| 26 | + */ |
| 27 | + private function escapeMessage($message) |
| 28 | + { |
| 29 | + // Replace problematic characters: ::, %, and newlines. |
| 30 | + $message = str_replace('::', ':', $message); |
| 31 | + $message = str_replace('%', '%25', $message); |
| 32 | + $message = str_replace("\r", '%0D', $message); |
| 33 | + $message = str_replace("\n", '%0A', $message); |
| 34 | + return $message; |
| 35 | + |
| 36 | + }//end escapeMessage() |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Generate a partial report for a single processed file. |
| 41 | + * |
| 42 | + * @param array $report Prepared report data. |
| 43 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
| 44 | + * @param bool $showSources Show sources? |
| 45 | + * @param int $width Maximum allowed line width. |
| 46 | + * |
| 47 | + * @return bool |
| 48 | + */ |
| 49 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
| 50 | + { |
| 51 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + $output = ''; |
| 56 | + |
| 57 | + foreach ($report['messages'] as $line => $lineErrors) { |
| 58 | + foreach ($lineErrors as $column => $colErrors) { |
| 59 | + foreach ($colErrors as $error) { |
| 60 | + $type = strtolower($error['type']); |
| 61 | + $filename = $report['filename']; |
| 62 | + $log = $this->escapeMessage($error['message']).'%0A%0A'.$this->escapeMessage($error['source']); |
| 63 | + |
| 64 | + $output .= "::{$type} file={$filename},line={$line},col={$column}::{$log}".PHP_EOL; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + echo $output; |
| 70 | + return true; |
| 71 | + |
| 72 | + }//end generateFileReport() |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Generates a report with lines parsable by GitHub Actions. |
| 77 | + * |
| 78 | + * @param string $cachedData Any partial report data from generateFileReport. |
| 79 | + * @param int $totalFiles Total number of files processed during the run. |
| 80 | + * @param int $totalErrors Total number of errors found during the run. |
| 81 | + * @param int $totalWarnings Total number of warnings found during the run. |
| 82 | + * @param int $totalFixable Total number of problems that can be fixed. |
| 83 | + * @param bool $showSources Show sources? |
| 84 | + * @param int $width Maximum allowed line width. |
| 85 | + * @param bool $interactive Are we running in interactive mode? |
| 86 | + * @param bool $toScreen Is the report being printed to screen? |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public function generate( |
| 91 | + $cachedData, |
| 92 | + $totalFiles, |
| 93 | + $totalErrors, |
| 94 | + $totalWarnings, |
| 95 | + $totalFixable, |
| 96 | + $showSources=false, |
| 97 | + $width=80, |
| 98 | + $interactive=false, |
| 99 | + $toScreen=true |
| 100 | + ) { |
| 101 | + if ($toScreen === true && $cachedData !== '') { |
| 102 | + echo "PHP_CodeSniffer Report: $totalErrors errors, $totalWarnings warnings, $totalFixable fixable".PHP_EOL; |
| 103 | + echo $cachedData; |
| 104 | + } |
| 105 | + |
| 106 | + }//end generate() |
| 107 | + |
| 108 | + |
| 109 | +}//end class |
0 commit comments