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

Source for file Cbf.php

Documentation is available at Cbf.php

  1. <?php
  2. /**
  3.  * CBF report for PHP_CodeSniffer.
  4.  *
  5.  * This report implements the various auto-fixing features of the
  6.  * PHPCBF script and is not intended (or allowed) to be selected as a
  7.  * report from the command line.
  8.  *
  9.  * @author    Greg Sherwood <gsherwood@squiz.net>
  10.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  11.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12.  */
  13.  
  14. namespace PHP_CodeSniffer\Reports;
  15.  
  16. use PHP_CodeSniffer\Files\File;
  17.  
  18. class Cbf implements Report
  19. {
  20.  
  21.  
  22.     /**
  23.      * Generate a partial report for a single processed file.
  24.      *
  25.      * Function should return TRUE if it printed or stored data about the file
  26.      * and FALSE if it ignored the file. Returning TRUE indicates that the file and
  27.      * its data should be counted in the grand totals.
  28.      *
  29.      * @param array                 $report      Prepared report data.
  30.      * @param \PHP_CodeSniffer\File $phpcsFile   The file being reported on.
  31.      * @param bool                  $showSources Show sources?
  32.      * @param int                   $width       Maximum allowed line width.
  33.      *
  34.      * @return bool 
  35.      */
  36.     public function generateFileReport($reportFile $phpcsFile$showSources=false$width=80)
  37.     {
  38.         $errors $phpcsFile->getFixableCount();
  39.         if ($errors !== 0{
  40.             if ($phpcsFile->config->stdin === false{
  41.                 ob_end_clean();
  42.                 $startTime microtime(true);
  43.                 echo "\t=> Fixing file: $errors/$errors violations remaining";
  44.             }
  45.  
  46.             $fixed $phpcsFile->fixer->fixFile();
  47.         }
  48.  
  49.         if ($phpcsFile->config->stdin === true{
  50.             // Replacing STDIN, so output current file to STDOUT
  51.             // even if nothing was fixed. Exit here because we
  52.             // can't process any more than 1 file in this setup.
  53.             echo $phpcsFile->fixer->getContents();
  54.             ob_end_flush();
  55.             exit(1);
  56.         }
  57.  
  58.         if ($errors === 0{
  59.             return false;
  60.         }
  61.  
  62.         if ($fixed === false{
  63.             echo 'ERROR';
  64.         else {
  65.             echo 'DONE';
  66.         }
  67.  
  68.         $timeTaken ((microtime(true$startTime* 1000);
  69.         if ($timeTaken < 1000{
  70.             $timeTaken round($timeTaken);
  71.             echo " in {$timeTaken}ms".PHP_EOL;
  72.         else {
  73.             $timeTaken round(($timeTaken / 1000)2);
  74.             echo " in $timeTaken secs".PHP_EOL;
  75.         }
  76.  
  77.         if ($fixed === true{
  78.             $newFilename $report['filename'].$phpcsFile->config->suffix;
  79.             $newContent  $phpcsFile->fixer->getContents();
  80.             file_put_contents($newFilename$newContent);
  81.  
  82.             if ($newFilename === $report['filename']{
  83.                 echo "\t=> File was overwritten".PHP_EOL;
  84.             else {
  85.                 echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL;
  86.             }
  87.         }
  88.  
  89.         ob_start();
  90.  
  91.         // This output is for the report and not printed to screen.
  92.         if ($fixed === false{
  93.             echo 'E|';
  94.         else {
  95.             echo $errors.'|';
  96.         }
  97.  
  98.         return $fixed;
  99.  
  100.     }//end generateFileReport()
  101.  
  102.  
  103.     /**
  104.      * Prints a summary of fixed files.
  105.      *
  106.      * @param string $cachedData    Any partial report data that was returned from
  107.      *                               generateFileReport during the run.
  108.      * @param int    $totalFiles    Total number of files processed during the run.
  109.      * @param int    $totalErrors   Total number of errors found during the run.
  110.      * @param int    $totalWarnings Total number of warnings found during the run.
  111.      * @param int    $totalFixable  Total number of problems that can be fixed.
  112.      * @param bool   $showSources   Show sources?
  113.      * @param int    $width         Maximum allowed line width.
  114.      * @param bool   $interactive   Are we running in interactive mode?
  115.      * @param bool   $toScreen      Is the report being printed to screen?
  116.      *
  117.      * @return void 
  118.      */
  119.     public function generate(
  120.         $cachedData,
  121.         $totalFiles,
  122.         $totalErrors,
  123.         $totalWarnings,
  124.         $totalFixable,
  125.         $showSources=false,
  126.         $width=80,
  127.         $interactive=false,
  128.         $toScreen=true
  129.     {
  130.         $fixed = 0;
  131.         $fails = 0;
  132.  
  133.         $errorCounts explode('|'rtrim($cachedData'|'));
  134.         foreach ($errorCounts as $count{
  135.             if ($count === 'E'{
  136.                 $fails++;
  137.             else {
  138.                 $fixed += $count;
  139.             }
  140.         }
  141.  
  142.         echo PHP_EOL;
  143.  
  144.         if ($fixed === 0{
  145.             echo 'No fixable errors were found';
  146.         else {
  147.             echo "Fixed $fixed errors in $totalFiles file";
  148.             if ($totalFiles !== 1{
  149.                 echo 's';
  150.             }
  151.         }
  152.  
  153.         if ($fails > 0{
  154.             echo "; failed fixing $fails file";
  155.             if ($fails !== 1{
  156.                 echo 's';
  157.             }
  158.         }
  159.  
  160.         echo PHP_EOL;
  161.  
  162.     }//end generate()
  163.  
  164.  
  165. }//end class

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