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

Source for file Junit.php

Documentation is available at Junit.php

  1. <?php
  2. /**
  3.  * JUnit report for PHP_CodeSniffer.
  4.  *
  5.  * @author    Oleg Lobach <oleg@lobach.info>
  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\Config;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class Junit implements Report
  17. {
  18.  
  19.  
  20.     /**
  21.      * Generate a partial report for a single processed file.
  22.      *
  23.      * Function should return TRUE if it printed or stored data about the file
  24.      * and FALSE if it ignored the file. Returning TRUE indicates that the file and
  25.      * its data should be counted in the grand totals.
  26.      *
  27.      * @param array                 $report      Prepared report data.
  28.      * @param \PHP_CodeSniffer\File $phpcsFile   The file being reported on.
  29.      * @param bool                  $showSources Show sources?
  30.      * @param int                   $width       Maximum allowed line width.
  31.      *
  32.      * @return bool 
  33.      */
  34.     public function generateFileReport($reportFile $phpcsFile$showSources=false$width=80)
  35.     {
  36.         $out = new \XMLWriter;
  37.         $out->openMemory();
  38.         $out->setIndent(true);
  39.  
  40.         $out->startElement('testsuite');
  41.         $out->writeAttribute('name'$report['filename']);
  42.  
  43.         if (count($report['messages']=== 0{
  44.             $out->writeAttribute('tests'1);
  45.             $out->writeAttribute('failures'0);
  46.  
  47.             $out->startElement('testcase');
  48.             $out->writeAttribute('name'$report['filename']);
  49.             $out->endElement();
  50.         else {
  51.             $failures ($report['errors'$report['warnings']);
  52.             $out->writeAttribute('tests'$failures);
  53.             $out->writeAttribute('failures'$failures);
  54.  
  55.             foreach ($report['messages'as $line => $lineErrors{
  56.                 foreach ($lineErrors as $column => $colErrors{
  57.                     foreach ($colErrors as $error{
  58.                         $out->startElement('testcase');
  59.                         $out->writeAttribute('name'$error['source'].' at '.$report['filename']." ($line:$column)");
  60.  
  61.                         $error['type'strtolower($error['type']);
  62.                         if ($phpcsFile->config->encoding !== 'utf-8'{
  63.                             $error['message'iconv($phpcsFile->config->encoding'utf-8'$error['message']);
  64.                         }
  65.  
  66.                         $out->startElement('failure');
  67.                         $out->writeAttribute('type'$error['type']);
  68.                         $out->writeAttribute('message'$error['message']);
  69.                         $out->endElement();
  70.  
  71.                         $out->endElement();
  72.                     }
  73.                 }
  74.             }
  75.         }//end if
  76.  
  77.         $out->endElement();
  78.         echo $out->flush();
  79.         return true;
  80.  
  81.     }//end generateFileReport()
  82.  
  83.  
  84.     /**
  85.      * Prints all violations for processed files, in a proprietary XML format.
  86.      *
  87.      * @param string $cachedData    Any partial report data that was returned from
  88.      *                               generateFileReport during the run.
  89.      * @param int    $totalFiles    Total number of files processed during the run.
  90.      * @param int    $totalErrors   Total number of errors found during the run.
  91.      * @param int    $totalWarnings Total number of warnings found during the run.
  92.      * @param int    $totalFixable  Total number of problems that can be fixed.
  93.      * @param bool   $showSources   Show sources?
  94.      * @param int    $width         Maximum allowed line width.
  95.      * @param bool   $interactive   Are we running in interactive mode?
  96.      * @param bool   $toScreen      Is the report being printed to screen?
  97.      *
  98.      * @return void 
  99.      */
  100.     public function generate(
  101.         $cachedData,
  102.         $totalFiles,
  103.         $totalErrors,
  104.         $totalWarnings,
  105.         $totalFixable,
  106.         $showSources=false,
  107.         $width=80,
  108.         $interactive=false,
  109.         $toScreen=true
  110.     {
  111.         // Figure out the total number of tests.
  112.         $tests   = 0;
  113.         $matches = array();
  114.         preg_match_all('/tests="([0-9]+)"/'$cachedData$matches);
  115.         if (isset($matches[1]=== true{
  116.             foreach ($matches[1as $match{
  117.                 $tests += $match;
  118.             }
  119.         }
  120.  
  121.         $failures ($totalErrors $totalWarnings);
  122.         echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
  123.         echo '<testsuites name="PHP_CodeSniffer '.Config::VERSION.'" tests="'.$tests.'" failures="'.$failures.'">'.PHP_EOL;
  124.         echo $cachedData;
  125.         echo '</testsuites>'.PHP_EOL;
  126.  
  127.     }//end generate()
  128.  
  129.  
  130. }//end class

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