annotate vendor/squizlabs/php_codesniffer/src/Reports/Json.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2 /**
Chris@17 3 * JSON report for PHP_CodeSniffer.
Chris@17 4 *
Chris@17 5 * @author Jeffrey Fisher <jeffslofish@gmail.com>
Chris@17 6 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@17 7 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@17 8 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@17 9 */
Chris@17 10
Chris@17 11 namespace PHP_CodeSniffer\Reports;
Chris@17 12
Chris@17 13 use PHP_CodeSniffer\Files\File;
Chris@17 14
Chris@17 15 class Json implements Report
Chris@17 16 {
Chris@17 17
Chris@17 18
Chris@17 19 /**
Chris@17 20 * Generate a partial report for a single processed file.
Chris@17 21 *
Chris@17 22 * Function should return TRUE if it printed or stored data about the file
Chris@17 23 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
Chris@17 24 * its data should be counted in the grand totals.
Chris@17 25 *
Chris@17 26 * @param array $report Prepared report data.
Chris@17 27 * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
Chris@17 28 * @param bool $showSources Show sources?
Chris@17 29 * @param int $width Maximum allowed line width.
Chris@17 30 *
Chris@17 31 * @return bool
Chris@17 32 */
Chris@17 33 public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
Chris@17 34 {
Chris@17 35 $filename = str_replace('\\', '\\\\', $report['filename']);
Chris@17 36 $filename = str_replace('"', '\"', $filename);
Chris@17 37 $filename = str_replace('/', '\/', $filename);
Chris@17 38 echo '"'.$filename.'":{';
Chris@17 39 echo '"errors":'.$report['errors'].',"warnings":'.$report['warnings'].',"messages":[';
Chris@17 40
Chris@17 41 $messages = '';
Chris@17 42 foreach ($report['messages'] as $line => $lineErrors) {
Chris@17 43 foreach ($lineErrors as $column => $colErrors) {
Chris@17 44 foreach ($colErrors as $error) {
Chris@17 45 $error['message'] = str_replace("\n", '\n', $error['message']);
Chris@17 46 $error['message'] = str_replace("\r", '\r', $error['message']);
Chris@17 47 $error['message'] = str_replace("\t", '\t', $error['message']);
Chris@17 48
Chris@17 49 $fixable = false;
Chris@17 50 if ($error['fixable'] === true) {
Chris@17 51 $fixable = true;
Chris@17 52 }
Chris@17 53
Chris@17 54 $messagesObject = (object) $error;
Chris@17 55 $messagesObject->line = $line;
Chris@17 56 $messagesObject->column = $column;
Chris@17 57 $messagesObject->fixable = $fixable;
Chris@17 58
Chris@17 59 $messages .= json_encode($messagesObject).",";
Chris@17 60 }
Chris@17 61 }
Chris@17 62 }//end foreach
Chris@17 63
Chris@17 64 echo rtrim($messages, ',');
Chris@17 65 echo ']},';
Chris@17 66
Chris@17 67 return true;
Chris@17 68
Chris@17 69 }//end generateFileReport()
Chris@17 70
Chris@17 71
Chris@17 72 /**
Chris@17 73 * Generates a JSON report.
Chris@17 74 *
Chris@17 75 * @param string $cachedData Any partial report data that was returned from
Chris@17 76 * generateFileReport during the run.
Chris@17 77 * @param int $totalFiles Total number of files processed during the run.
Chris@17 78 * @param int $totalErrors Total number of errors found during the run.
Chris@17 79 * @param int $totalWarnings Total number of warnings found during the run.
Chris@17 80 * @param int $totalFixable Total number of problems that can be fixed.
Chris@17 81 * @param bool $showSources Show sources?
Chris@17 82 * @param int $width Maximum allowed line width.
Chris@17 83 * @param bool $interactive Are we running in interactive mode?
Chris@17 84 * @param bool $toScreen Is the report being printed to screen?
Chris@17 85 *
Chris@17 86 * @return void
Chris@17 87 */
Chris@17 88 public function generate(
Chris@17 89 $cachedData,
Chris@17 90 $totalFiles,
Chris@17 91 $totalErrors,
Chris@17 92 $totalWarnings,
Chris@17 93 $totalFixable,
Chris@17 94 $showSources=false,
Chris@17 95 $width=80,
Chris@17 96 $interactive=false,
Chris@17 97 $toScreen=true
Chris@17 98 ) {
Chris@17 99 echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{';
Chris@17 100 echo rtrim($cachedData, ',');
Chris@17 101 echo "}}";
Chris@17 102
Chris@17 103 }//end generate()
Chris@17 104
Chris@17 105
Chris@17 106 }//end class