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. use PHP_CodeSniffer\Util;
  18.  
  19. class Cbf implements Report
  20. {
  21.  
  22.  
  23.     /**
  24.      * Generate a partial report for a single processed file.
  25.      *
  26.      * Function should return TRUE if it printed or stored data about the file
  27.      * and FALSE if it ignored the file. Returning TRUE indicates that the file and
  28.      * its data should be counted in the grand totals.
  29.      *
  30.      * @param array                 $report      Prepared report data.
  31.      * @param \PHP_CodeSniffer\File $phpcsFile   The file being reported on.
  32.      * @param bool                  $showSources Show sources?
  33.      * @param int                   $width       Maximum allowed line width.
  34.      *
  35.      * @return bool 
  36.      */
  37.     public function generateFileReport($reportFile $phpcsFile$showSources=false$width=80)
  38.     {
  39.         $errors $phpcsFile->getFixableCount();
  40.         if ($errors !== 0{
  41.             if (PHP_CODESNIFFER_VERBOSITY > 0{
  42.                 ob_end_clean();
  43.                 $startTime microtime(true);
  44.                 echo "\t=> Fixing file: $errors/$errors violations remaining";
  45.             }
  46.  
  47.             $fixed $phpcsFile->fixer->fixFile();
  48.         }
  49.  
  50.         if ($phpcsFile->config->stdin === true{
  51.             // Replacing STDIN, so output current file to STDOUT
  52.             // even if nothing was fixed. Exit here because we
  53.             // can't process any more than 1 file in this setup.
  54.             echo $phpcsFile->fixer->getContents();
  55.             ob_end_flush();
  56.             exit(1);
  57.         }
  58.  
  59.         if ($errors === 0{
  60.             return false;
  61.         }
  62.  
  63.         if (PHP_CODESNIFFER_VERBOSITY > 0{
  64.             if ($fixed === false{
  65.                 echo 'ERROR';
  66.             else {
  67.                 echo 'DONE';
  68.             }
  69.  
  70.             $timeTaken ((microtime(true$startTime* 1000);
  71.             if ($timeTaken < 1000{
  72.                 $timeTaken round($timeTaken);
  73.                 echo " in {$timeTaken}ms".PHP_EOL;
  74.             else {
  75.                 $timeTaken round(($timeTaken / 1000)2);
  76.                 echo " in $timeTaken secs".PHP_EOL;
  77.             }
  78.         }
  79.  
  80.         if ($fixed === true{
  81.             $newFilename $report['filename'].$phpcsFile->config->suffix;
  82.             $newContent  $phpcsFile->fixer->getContents();
  83.             file_put_contents($newFilename$newContent);
  84.  
  85.             if (PHP_CODESNIFFER_VERBOSITY > 0{
  86.                 if ($newFilename === $report['filename']{
  87.                     echo "\t=> File was overwritten".PHP_EOL;
  88.                 else {
  89.                     echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         if (PHP_CODESNIFFER_VERBOSITY > 0{
  95.             ob_start();
  96.         }
  97.  
  98.         $errorCount   $phpcsFile->getErrorCount();
  99.         $warningCount $phpcsFile->getWarningCount();
  100.         $fixableCount $phpcsFile->getFixableCount();
  101.         $fixedCount   ($errors $fixableCount);
  102.         echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL;
  103.  
  104.         return $fixed;
  105.  
  106.     }//end generateFileReport()
  107.  
  108.  
  109.     /**
  110.      * Prints a summary of fixed files.
  111.      *
  112.      * @param string $cachedData    Any partial report data that was returned from
  113.      *                               generateFileReport during the run.
  114.      * @param int    $totalFiles    Total number of files processed during the run.
  115.      * @param int    $totalErrors   Total number of errors found during the run.
  116.      * @param int    $totalWarnings Total number of warnings found during the run.
  117.      * @param int    $totalFixable  Total number of problems that can be fixed.
  118.      * @param bool   $showSources   Show sources?
  119.      * @param int    $width         Maximum allowed line width.
  120.      * @param bool   $interactive   Are we running in interactive mode?
  121.      * @param bool   $toScreen      Is the report being printed to screen?
  122.      *
  123.      * @return void 
  124.      */
  125.     public function generate(
  126.         $cachedData,
  127.         $totalFiles,
  128.         $totalErrors,
  129.         $totalWarnings,
  130.         $totalFixable,
  131.         $showSources=false,
  132.         $width=80,
  133.         $interactive=false,
  134.         $toScreen=true
  135.     {
  136.         $lines explode(PHP_EOL$cachedData);
  137.         array_pop($lines);
  138.  
  139.         if (empty($lines=== true{
  140.             echo PHP_EOL.'No fixable errors were found'.PHP_EOL;
  141.             return;
  142.         }
  143.  
  144.         $reportFiles = array();
  145.         $maxLength   = 0;
  146.         $totalFixed  = 0;
  147.         $failures    = 0;
  148.  
  149.         foreach ($lines as $line{
  150.             $parts   explode('>>'$line);
  151.             $fileLen strlen($parts[0]);
  152.             $reportFiles[$parts[0]] = array(
  153.                                        'errors'   => $parts[1],
  154.                                        'warnings' => $parts[2],
  155.                                        'fixable'  => $parts[3],
  156.                                        'fixed'    => $parts[4],
  157.                                        'strlen'   => $fileLen,
  158.                                       );
  159.  
  160.             $maxLength max($maxLength$fileLen);
  161.  
  162.             $totalFixed += $parts[4];
  163.  
  164.             if ($parts[3> 0{
  165.                 $failures++;
  166.             }
  167.         }
  168.  
  169.         $width min($width($maxLength + 21));
  170.         $width max($width70);
  171.  
  172.         echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL;
  173.         echo str_repeat('-'$width).PHP_EOL;
  174.         echo "\033[1m".'FILE'.str_repeat(' '($width - 20)).'FIXED  REMAINING'."\033[0m".PHP_EOL;
  175.         echo str_repeat('-'$width).PHP_EOL;
  176.  
  177.         foreach ($reportFiles as $file => $data{
  178.             $padding ($width - 18 - $data['strlen']);
  179.             if ($padding < 0{
  180.                 $file    '...'.substr($file(($padding * -1+ 3));
  181.                 $padding = 0;
  182.             }
  183.  
  184.             echo $file.str_repeat(' '$padding).'  ';
  185.  
  186.             if ($data['fixable'> 0{
  187.                 echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL;
  188.                 continue;
  189.             }
  190.  
  191.             $remaining ($data['errors'$data['warnings']);
  192.  
  193.             if ($data['fixed'!== 0{
  194.                 echo $data['fixed'];
  195.                 echo str_repeat(' '(7 - strlen((string) $data['fixed'])));
  196.             else {
  197.                 echo '0      ';
  198.             }
  199.  
  200.             if ($remaining !== 0{
  201.                 echo $remaining;
  202.             else {
  203.                 echo '0';
  204.             }
  205.  
  206.             echo PHP_EOL;
  207.         }//end foreach
  208.  
  209.         echo str_repeat('-'$width).PHP_EOL;
  210.         echo "\033[1mA TOTAL OF $totalFixed ERROR";
  211.         if ($totalFixed !== 1{
  212.             echo 'S';
  213.         }
  214.  
  215.         $numFiles count($reportFiles);
  216.         echo ' WERE FIXED IN '.$numFiles.' FILE';
  217.         if ($numFiles !== 1{
  218.             echo 'S';
  219.         }
  220.  
  221.         echo "\033[0m";
  222.  
  223.         if ($failures > 0{
  224.             echo PHP_EOL.str_repeat('-'$width).PHP_EOL;
  225.             echo "\033[1mPHPCBF FAILED TO FIX $failures FILE";
  226.             if ($failures !== 1{
  227.                 echo 'S';
  228.             }
  229.  
  230.             echo "\033[0m";
  231.         }
  232.  
  233.         echo PHP_EOL.str_repeat('-'$width).PHP_EOL.PHP_EOL;
  234.  
  235.         if ($toScreen === true && $interactive === false{
  236.             Util\Timing::printRunTime();
  237.         }
  238.  
  239.     }//end generate()
  240.  
  241.  
  242. }//end class

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