annotate vendor/squizlabs/php_codesniffer/src/Reports/Json.php @ 4:a9cd425dd02b

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