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

Source for file Xml.php

Documentation is available at Xml.php

  1. <?php
  2. /**
  3.  * XML 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 Xml 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.         $out->writeAttribute('errors'$report['errors']);
  47.         $out->writeAttribute('warnings'$report['warnings']);
  48.         $out->writeAttribute('fixable'$report['fixable']);
  49.  
  50.         foreach ($report['messages'as $line => $lineErrors{
  51.             foreach ($lineErrors as $column => $colErrors{
  52.                 foreach ($colErrors as $error{
  53.                     $error['type'strtolower($error['type']);
  54.                     if ($phpcsFile->config->encoding !== 'utf-8'{
  55.                         $error['message'iconv($phpcsFile->config->encoding'utf-8'$error['message']);
  56.                     }
  57.  
  58.                     $out->startElement($error['type']);
  59.                     $out->writeAttribute('line'$line);
  60.                     $out->writeAttribute('column'$column);
  61.                     $out->writeAttribute('source'$error['source']);
  62.                     $out->writeAttribute('severity'$error['severity']);
  63.                     $out->writeAttribute('fixable'(int) $error['fixable']);
  64.                     $out->text($error['message']);
  65.                     $out->endElement();
  66.                 }
  67.             }
  68.         }//end foreach
  69.  
  70.         $out->endElement();
  71.         echo $out->flush();
  72.  
  73.         return true;
  74.  
  75.     }//end generateFileReport()
  76.  
  77.  
  78.     /**
  79.      * Prints all violations for processed files, in a proprietary XML format.
  80.      *
  81.      * @param string $cachedData    Any partial report data that was returned from
  82.      *                               generateFileReport during the run.
  83.      * @param int    $totalFiles    Total number of files processed during the run.
  84.      * @param int    $totalErrors   Total number of errors found during the run.
  85.      * @param int    $totalWarnings Total number of warnings found during the run.
  86.      * @param int    $totalFixable  Total number of problems that can be fixed.
  87.      * @param bool   $showSources   Show sources?
  88.      * @param int    $width         Maximum allowed line width.
  89.      * @param bool   $interactive   Are we running in interactive mode?
  90.      * @param bool   $toScreen      Is the report being printed to screen?
  91.      *
  92.      * @return void 
  93.      */
  94.     public function generate(
  95.         $cachedData,
  96.         $totalFiles,
  97.         $totalErrors,
  98.         $totalWarnings,
  99.         $totalFixable,
  100.         $showSources=false,
  101.         $width=80,
  102.         $interactive=false,
  103.         $toScreen=true
  104.     {
  105.         echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
  106.         echo '<phpcs version="'.Config::VERSION.'">'.PHP_EOL;
  107.         echo $cachedData;
  108.         echo '</phpcs>'.PHP_EOL;
  109.  
  110.     }//end generate()
  111.  
  112.  
  113. }//end class

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