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

Source for file Source.php

Documentation is available at Source.php

  1. <?php
  2. /**
  3.  * Source 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\Files\File;
  13. use PHP_CodeSniffer\Util\Timing;
  14.  
  15. class Source 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($report, File $phpcsFile, $showSources=false, $width=80)
  34.     {
  35.         if ($report['errors'] === 0 && $report['warnings'] === 0) {
  36.             // Nothing to print.
  37.             return false;
  38.         }
  39.  
  40.         $sources = array();
  41.  
  42.         foreach ($report['messages'] as $line => $lineErrors) {
  43.             foreach ($lineErrors as $column => $colErrors) {
  44.                 foreach ($colErrors as $error) {
  45.                     $src = $error['source'];
  46.                     if (isset($sources[$src]) === false) {
  47.                         $sources[$src] = array(
  48.                                           'fixable' => (int) $error['fixable'],
  49.                                           'count'   => 1,
  50.                                          );
  51.                     } else {
  52.                         $sources[$src]['count']++;
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.  
  58.         foreach ($sources as $source => $data) {
  59.             echo $source.'>>'.$data['fixable'].'>>'.$data['count'].PHP_EOL;
  60.         }
  61.  
  62.         return true;
  63.  
  64.     }//end generateFileReport()
  65.  
  66.  
  67.     /**
  68.      * Prints the source of all errors and warnings.
  69.      *
  70.      * @param string $cachedData    Any partial report data that was returned from
  71.      *                               generateFileReport during the run.
  72.      * @param int    $totalFiles    Total number of files processed during the run.
  73.      * @param int    $totalErrors   Total number of errors found during the run.
  74.      * @param int    $totalWarnings Total number of warnings found during the run.
  75.      * @param int    $totalFixable  Total number of problems that can be fixed.
  76.      * @param bool   $showSources   Show sources?
  77.      * @param int    $width         Maximum allowed line width.
  78.      * @param bool   $interactive   Are we running in interactive mode?
  79.      * @param bool   $toScreen      Is the report being printed to screen?
  80.      *
  81.      * @return void 
  82.      */
  83.     public function generate(
  84.         $cachedData,
  85.         $totalFiles,
  86.         $totalErrors,
  87.         $totalWarnings,
  88.         $totalFixable,
  89.         $showSources=false,
  90.         $width=80,
  91.         $interactive=false,
  92.         $toScreen=true
  93.     ) {
  94.         $lines = explode(PHP_EOL, $cachedData);
  95.         array_pop($lines);
  96.  
  97.         if (empty($lines) === true) {
  98.             return;
  99.         }
  100.  
  101.         $sources   = array();
  102.         $maxLength = 0;
  103.  
  104.         foreach ($lines as $line) {
  105.             $parts   = explode('>>', $line);
  106.             $source  = $parts[0];
  107.             $fixable = (bool) $parts[1];
  108.             $count   = $parts[2];
  109.  
  110.             if (isset($sources[$source]) === false) {
  111.                 if ($showSources === true) {
  112.                     $parts = null;
  113.                     $sniff = $source;
  114.                 } else {
  115.                     $parts = explode('.', $source);
  116.                     if ($parts[0] === 'Internal') {
  117.                         $parts[2] = $parts[1];
  118.                         $parts[1] = '';
  119.                     }
  120.  
  121.                     $parts[1] = $this->makeFriendlyName($parts[1]);
  122.  
  123.                     $sniff = $this->makeFriendlyName($parts[2]);
  124.                     if (isset($parts[3]) === true) {
  125.                         $name    = $this->makeFriendlyName($parts[3]);
  126.                         $name[0] = strtolower($name[0]);
  127.                         $sniff  .= ' '.$name;
  128.                         unset($parts[3]);
  129.                     }
  130.  
  131.                     $parts[2] = $sniff;
  132.                 }//end if
  133.  
  134.                 $maxLength = max($maxLength, strlen($sniff));
  135.  
  136.                 $sources[$source] = array(
  137.                                      'count'   => $count,
  138.                                      'fixable' => $fixable,
  139.                                      'parts'   => $parts,
  140.                                     );
  141.             } else {
  142.                 $sources[$source]['count'] += $count;
  143.             }//end if
  144.  
  145.             $fileLen = strlen($parts[0]);
  146.             $reportFiles[$parts[0]] = array(
  147.                                        'errors'   => $parts[1],
  148.                                        'warnings' => $parts[2],
  149.                                        'strlen'   => $fileLen,
  150.                                       );
  151.         }//end foreach
  152.  
  153.         if ($showSources === true) {
  154.             $width = min($width, ($maxLength + 11));
  155.         } else {
  156.             $width = min($width, ($maxLength + 41));
  157.         }
  158.  
  159.         $width = max($width, 70);
  160.  
  161.         asort($sources);
  162.         $sources = array_reverse($sources);
  163.  
  164.         echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL;
  165.         echo str_repeat('-', $width).PHP_EOL."\033[1m";
  166.         if ($showSources === true) {
  167.             if ($totalFixable > 0) {
  168.                 echo '    SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL;
  169.             } else {
  170.                 echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL;
  171.             }
  172.         } else {
  173.             if ($totalFixable > 0) {
  174.                 echo '    STANDARD  CATEGORY            SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL;
  175.             } else {
  176.                 echo 'STANDARD  CATEGORY            SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL;
  177.             }
  178.         }
  179.  
  180.         echo "\033[0m".str_repeat('-', $width).PHP_EOL;
  181.  
  182.         $fixableSources = 0;
  183.  
  184.         if ($showSources === true) {
  185.             $maxSniffWidth = ($width - 7);
  186.         } else {
  187.             $maxSniffWidth = ($width - 37);
  188.         }
  189.  
  190.         if ($totalFixable > 0) {
  191.             $maxSniffWidth -= 4;
  192.         }
  193.  
  194.         foreach ($sources as $source => $sourceData) {
  195.             if ($totalFixable > 0) {
  196.                 echo '[';
  197.                 if ($sourceData['fixable'] === true) {
  198.                     echo 'x';
  199.                     $fixableSources++;
  200.                 } else {
  201.                     echo ' ';
  202.                 }
  203.  
  204.                 echo '] ';
  205.             }
  206.  
  207.             if ($showSources === true) {
  208.                 if (strlen($source) > $maxSniffWidth) {
  209.                     $source = substr($source, 0, $maxSniffWidth);
  210.                 }
  211.  
  212.                 echo $source;
  213.                 if ($totalFixable > 0) {
  214.                     echo str_repeat(' ', ($width - 9 - strlen($source)));
  215.                 } else {
  216.                     echo str_repeat(' ', ($width - 5 - strlen($source)));
  217.                 }
  218.             } else {
  219.                 $parts = $sourceData['parts'];
  220.  
  221.                 if (strlen($parts[0]) > 8) {
  222.                     $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1));
  223.                 }
  224.  
  225.                 echo $parts[0].str_repeat(' ', (10 - strlen($parts[0])));
  226.  
  227.                 $category = $parts[1];
  228.                 if (strlen($category) > 18) {
  229.                     $category = substr($category, 0, ((strlen($category) - 18) * -1));
  230.                 }
  231.  
  232.                 echo $category.str_repeat(' ', (20 - strlen($category)));
  233.  
  234.                 $sniff = $parts[2];
  235.                 if (strlen($sniff) > $maxSniffWidth) {
  236.                     $sniff = substr($sniff, 0, $maxSniffWidth);
  237.                 }
  238.  
  239.                 if ($totalFixable > 0) {
  240.                     echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff)));
  241.                 } else {
  242.                     echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff)));
  243.                 }
  244.             }//end if
  245.  
  246.             echo $sourceData['count'].PHP_EOL;
  247.         }//end foreach
  248.  
  249.         echo str_repeat('-', $width).PHP_EOL;
  250.         echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION';
  251.         if (($totalErrors + $totalWarnings) > 1) {
  252.             echo 'S';
  253.         }
  254.  
  255.         echo ' WERE FOUND IN '.count($sources).' SOURCE';
  256.         if (count($sources) !== 1) {
  257.             echo 'S';
  258.         }
  259.  
  260.         echo "\033[0m";
  261.  
  262.         if ($totalFixable > 0) {
  263.             echo PHP_EOL.str_repeat('-', $width).PHP_EOL;
  264.             echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m";
  265.         }
  266.  
  267.         echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
  268.  
  269.         if ($toScreen === true && $interactive === false) {
  270.             Timing::printRunTime();
  271.         }
  272.  
  273.     }//end generate()
  274.  
  275.  
  276.     /**
  277.      * Converts a camel caps name into a readable string.
  278.      *
  279.      * @param string $name The camel caps name to convert.
  280.      *
  281.      * @return string 
  282.      */
  283.     public function makeFriendlyName($name)
  284.     {
  285.         if (trim($name) === '') {
  286.             return '';
  287.         }
  288.  
  289.         $friendlyName = '';
  290.         $length       = strlen($name);
  291.  
  292.         $lastWasUpper   = false;
  293.         $lastWasNumeric = false;
  294.         for ($i = 0; $i < $length; $i++) {
  295.             if (is_numeric($name[$i]) === true) {
  296.                 if ($lastWasNumeric === false) {
  297.                     $friendlyName .= ' ';
  298.                 }
  299.  
  300.                 $lastWasUpper   = false;
  301.                 $lastWasNumeric = true;
  302.             } else {
  303.                 $lastWasNumeric = false;
  304.  
  305.                 $char = strtolower($name[$i]);
  306.                 if ($char === $name[$i]) {
  307.                     // Lowercase.
  308.                     $lastWasUpper = false;
  309.                 } else {
  310.                     // Uppercase.
  311.                     if ($lastWasUpper === false) {
  312.                         $friendlyName .= ' ';
  313.                         if ($i < ($length - 1)) {
  314.                             $next = $name[($i + 1)];
  315.                             if (strtolower($next) === $next) {
  316.                                 // Next char is lowercase so it is a word boundary.
  317.                                 $name[$i] = strtolower($name[$i]);
  318.                             }
  319.                         }
  320.                     }
  321.  
  322.                     $lastWasUpper = true;
  323.                 }
  324.             }//end if
  325.  
  326.             $friendlyName .= $name[$i];
  327.         }//end for
  328.  
  329.         $friendlyName    = trim($friendlyName);
  330.         $friendlyName[0] = strtoupper($friendlyName[0]);
  331.  
  332.         return $friendlyName;
  333.  
  334.     }//end makeFriendlyName()
  335.  
  336.  
  337. }//end class

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