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

Source for file CommentedOutCodeSniff.php

Documentation is available at CommentedOutCodeSniff.php

  1. <?php
  2. /**
  3.  * Warn about commented out code.
  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\Standards\Squiz\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15. use PHP_CodeSniffer\Exceptions\TokenizerException;
  16.  
  17. class CommentedOutCodeSniff implements Sniff
  18. {
  19.  
  20.     /**
  21.      * A list of tokenizers this sniff supports.
  22.      *
  23.      * @var array 
  24.      */
  25.     public $supportedTokenizers = array(
  26.                                    'PHP',
  27.                                    'CSS',
  28.                                   );
  29.  
  30.     /**
  31.      * If a comment is more than $maxPercentage% code, a warning will be shown.
  32.      *
  33.      * @var integer 
  34.      */
  35.     public $maxPercentage = 35;
  36.  
  37.  
  38.     /**
  39.      * Returns an array of tokens this test wants to listen for.
  40.      *
  41.      * @return array 
  42.      */
  43.     public function register()
  44.     {
  45.         return array(T_COMMENT);
  46.  
  47.     }//end register()
  48.  
  49.  
  50.     /**
  51.      * Processes this test, when one of its tokens is encountered.
  52.      *
  53.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  54.      * @param int                         $stackPtr  The position of the current token
  55.      *                                                in the stack passed in $tokens.
  56.      *
  57.      * @return void 
  58.      */
  59.     public function process(File $phpcsFile$stackPtr)
  60.     {
  61.         $tokens $phpcsFile->getTokens();
  62.  
  63.         // Process whole comment blocks at once, so skip all but the first token.
  64.         if ($stackPtr > 0 && $tokens[$stackPtr]['code'=== $tokens[($stackPtr - 1)]['code']{
  65.             return;
  66.         }
  67.  
  68.         // Ignore comments at the end of code blocks.
  69.         if (substr($tokens[$stackPtr]['content']06=== '//end '{
  70.             return;
  71.         }
  72.  
  73.         $content '';
  74.         if ($phpcsFile->tokenizerType === 'PHP'{
  75.             $content '<?php ';
  76.         }
  77.  
  78.         for ($i $stackPtr$i $phpcsFile->numTokens; $i++{
  79.             if ($tokens[$stackPtr]['code'!== $tokens[$i]['code']{
  80.                 break;
  81.             }
  82.  
  83.             /*
  84.                 Trim as much off the comment as possible so we don't
  85.                 have additional whitespace tokens or comment tokens
  86.             */
  87.  
  88.             $tokenContent trim($tokens[$i]['content']);
  89.  
  90.             if (substr($tokenContent02=== '//'{
  91.                 $tokenContent substr($tokenContent2);
  92.             }
  93.  
  94.             if (substr($tokenContent01=== '#'{
  95.                 $tokenContent substr($tokenContent1);
  96.             }
  97.  
  98.             if (substr($tokenContent03=== '/**'{
  99.                 $tokenContent substr($tokenContent3);
  100.             }
  101.  
  102.             if (substr($tokenContent02=== '/*'{
  103.                 $tokenContent substr($tokenContent2);
  104.             }
  105.  
  106.             if (substr($tokenContent-2=== '*/'{
  107.                 $tokenContent substr($tokenContent0-2);
  108.             }
  109.  
  110.             if (substr($tokenContent01=== '*'{
  111.                 $tokenContent substr($tokenContent1);
  112.             }
  113.  
  114.             $content .= $tokenContent.$phpcsFile->eolChar;
  115.         }//end for
  116.  
  117.         $content trim($content);
  118.  
  119.         if ($phpcsFile->tokenizerType === 'PHP'{
  120.             $content .= ' ?>';
  121.         }
  122.  
  123.         // Quite a few comments use multiple dashes, equals signs etc
  124.         // to frame comments and licence headers.
  125.         $content preg_replace('/[-=*]+/''-'$content);
  126.  
  127.         // Random numbers sitting inside the content can throw parse errors
  128.         // for invalid literals in PHP7+, so strip those.
  129.         $content preg_replace('/\d+/'''$content);
  130.  
  131.         // Because we are not really parsing code, the tokenizer can throw all sorts
  132.         // of errors that don't mean anything, so ignore them.
  133.         $oldErrors ini_get('error_reporting');
  134.         ini_set('error_reporting'0);
  135.         try {
  136.             $tokenizerClass get_class($phpcsFile->tokenizer);
  137.             $tokenizer      = new $tokenizerClass($content$phpcsFile->config$phpcsFile->eolChar);
  138.             $stringTokens   $tokenizer->getTokens();
  139.         catch (TokenizerException $e{
  140.             // We couldn't check the comment, so ignore it.
  141.             ini_set('error_reporting'$oldErrors);
  142.             return;
  143.         }
  144.  
  145.         ini_set('error_reporting'$oldErrors);
  146.  
  147.         $emptyTokens = array(
  148.                         T_WHITESPACE              => true,
  149.                         T_STRING                  => true,
  150.                         T_STRING_CONCAT           => true,
  151.                         T_ENCAPSED_AND_WHITESPACE => true,
  152.                         T_NONE                    => true,
  153.                         T_COMMENT                 => true,
  154.                        );
  155.  
  156.         $numTokens count($stringTokens);
  157.  
  158.         /*
  159.             We know what the first two and last two tokens should be
  160.             (because we put them there) so ignore this comment if those
  161.             tokens were not parsed correctly. It obviously means this is not
  162.             valid code.
  163.         */
  164.  
  165.         // First token is always the opening PHP tag.
  166.         if ($stringTokens[0]['code'!== T_OPEN_TAG{
  167.             return;
  168.         }
  169.  
  170.         // Last token is always the closing PHP tag, unless something went wrong.
  171.         if (isset($stringTokens[($numTokens - 1)]=== false
  172.             || $stringTokens[($numTokens - 1)]['code'!== T_CLOSE_TAG
  173.         {
  174.             return;
  175.         }
  176.  
  177.         // Second last token is always whitespace or a comment, depending
  178.         // on the code inside the comment.
  179.         if ($phpcsFile->tokenizerType === 'PHP'
  180.             && isset(Tokens::$emptyTokens[$stringTokens[($numTokens - 2)]['code']]=== false
  181.         {
  182.             return;
  183.         }
  184.  
  185.         $numComment  = 0;
  186.         $numPossible = 0;
  187.         $numCode     = 0;
  188.  
  189.         for ($i = 0; $i $numTokens$i++{
  190.             if (isset($emptyTokens[$stringTokens[$i]['code']]=== true{
  191.                 // Looks like comment.
  192.                 $numComment++;
  193.             else if (in_array($stringTokens[$i]['code']Tokens::$comparisonTokens=== true
  194.                 || in_array($stringTokens[$i]['code']Tokens::$arithmeticTokens=== true
  195.                 || $stringTokens[$i]['code'=== T_GOTO_LABEL
  196.             {
  197.                 // Commented out HTML/XML and other docs contain a lot of these
  198.                 // characters, so it is best to not use them directly.
  199.                 $numPossible++;
  200.             else {
  201.                 // Looks like code.
  202.                 $numCode++;
  203.             }
  204.         }
  205.  
  206.         // We subtract 3 from the token number so we ignore the start/end tokens
  207.         // and their surrounding whitespace. We take 2 off the number of code
  208.         // tokens so we ignore the start/end tokens.
  209.         if ($numTokens > 3{
  210.             $numTokens -= 3;
  211.         }
  212.  
  213.         if ($numCode >= 2{
  214.             $numCode -= 2;
  215.         }
  216.  
  217.         $percentCode ceil((($numCode $numTokens* 100));
  218.         if ($percentCode $this->maxPercentage{
  219.             // Just in case.
  220.             $percentCode min(100$percentCode);
  221.  
  222.             $error 'This comment is %s%% valid code; is this commented out code?';
  223.             $data  = array($percentCode);
  224.             $phpcsFile->addWarning($error$stackPtr'Found'$data);
  225.         }
  226.  
  227.     }//end process()
  228.  
  229.  
  230. }//end class

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