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

Source for file Checkstyle.php

Documentation is available at Checkstyle.php

  1. <?php
  2. /**
  3.  * Checkstyle report for PHP_CodeSniffer.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Reports;
  11.  
  12. use PHP_CodeSniffer\Config;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class Checkstyle 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.         $out = new \XMLWriter;
  36.         $out->openMemory();
  37.         $out->setIndent(true);
  38.  
  39.         if ($report['errors'=== 0 && $report['warnings'=== 0{
  40.             // Nothing to print.
  41.             return false;
  42.         }
  43.  
  44.         $out->startElement('file');
  45.         $out->writeAttribute('name'$report['filename']);
  46.  
  47.         foreach ($report['messages'as $line => $lineErrors{
  48.             foreach ($lineErrors as $column => $colErrors{
  49.                 foreach ($colErrors as $error{
  50.                     $error['type'strtolower($error['type']);
  51.                     if ($phpcsFile->config->encoding !== 'utf-8'{
  52.                         $error['message'iconv($phpcsFile->config->encoding'utf-8'$error['message']);
  53.                     }
  54.  
  55.                     $out->startElement('error');
  56.                     $out->writeAttribute('line'$line);
  57.                     $out->writeAttribute('column'$column);
  58.                     $out->writeAttribute('severity'$error['type']);
  59.                     $out->writeAttribute('message'$error['message']);
  60.                     $out->writeAttribute('source'$error['source']);
  61.                     $out->endElement();
  62.                 }
  63.             }
  64.         }//end foreach
  65.  
  66.         $out->endElement();
  67.         echo $out->flush();
  68.  
  69.         return true;
  70.  
  71.     }//end generateFileReport()
  72.  
  73.  
  74.     /**
  75.      * Prints all violations for processed files, in a Checkstyle format.
  76.      *
  77.      * @param string $cachedData    Any partial report data that was returned from
  78.      *                               generateFileReport during the run.
  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.         echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
  102.         echo '<checkstyle version="'.Config::VERSION.'">'.PHP_EOL;
  103.         echo $cachedData;
  104.         echo '</checkstyle>'.PHP_EOL;
  105.  
  106.     }//end generate()
  107.  
  108.  
  109. }//end class

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