PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file Json.php

Documentation is available at Json.php

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

Documentation generated on Mon, 11 Mar 2019 15:27:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.