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

Source for file LongConditionClosingCommentSniff.php

Documentation is available at LongConditionClosingCommentSniff.php

  1. <?php
  2. /**
  3.  * Ensures long conditions have a comment at the end.
  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\Commenting;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class LongConditionClosingCommentSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                   );
  27.  
  28.     /**
  29.      * The openers that we are interested in.
  30.      *
  31.      * @var integer[] 
  32.      */
  33.     private static $openers = array(
  34.                                T_SWITCH,
  35.                                T_IF,
  36.                                T_FOR,
  37.                                T_FOREACH,
  38.                                T_WHILE,
  39.                                T_TRY,
  40.                                T_CASE,
  41.                               );
  42.  
  43.     /**
  44.      * The length that a code block must be before
  45.      * requiring a closing comment.
  46.      *
  47.      * @var integer 
  48.      */
  49.     protected $lineLimit = 20;
  50.  
  51.  
  52.     /**
  53.      * Returns an array of tokens this test wants to listen for.
  54.      *
  55.      * @return array 
  56.      */
  57.     public function register()
  58.     {
  59.         return array(T_CLOSE_CURLY_BRACKET);
  60.  
  61.     }//end register()
  62.  
  63.  
  64.     /**
  65.      * Processes this test, when one of its tokens is encountered.
  66.      *
  67.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  68.      * @param int                  $stackPtr  The position of the current token in the
  69.      *                                         stack passed in $tokens.
  70.      *
  71.      * @return void 
  72.      */
  73.     public function process(File $phpcsFile$stackPtr)
  74.     {
  75.         $tokens $phpcsFile->getTokens();
  76.  
  77.         if (isset($tokens[$stackPtr]['scope_condition']=== false{
  78.             // No scope condition. It is a function closer.
  79.             return;
  80.         }
  81.  
  82.         $startCondition $tokens[$tokens[$stackPtr]['scope_condition']];
  83.         $startBrace     $tokens[$tokens[$stackPtr]['scope_opener']];
  84.         $endBrace       $tokens[$stackPtr];
  85.  
  86.         // We are only interested in some code blocks.
  87.         if (in_array($startCondition['code']self::$openers=== false{
  88.             return;
  89.         }
  90.  
  91.         if ($startCondition['code'=== T_IF{
  92.             // If this is actually and ELSE IF, skip it as the brace
  93.             // will be checked by the original IF.
  94.             $else $phpcsFile->findPrevious(T_WHITESPACE($tokens[$stackPtr]['scope_condition'- 1)nulltrue);
  95.             if ($tokens[$else]['code'=== T_ELSE{
  96.                 return;
  97.             }
  98.  
  99.             // IF statements that have an ELSE block need to use
  100.             // "end if" rather than "end else" or "end elseif".
  101.             do {
  102.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  103.                 if ($tokens[$nextToken]['code'=== T_ELSE || $tokens[$nextToken]['code'=== T_ELSEIF{
  104.                     // Check for ELSE IF (2 tokens) as opposed to ELSEIF (1 token).
  105.                     if ($tokens[$nextToken]['code'=== T_ELSE
  106.                         && isset($tokens[$nextToken]['scope_closer']=== false
  107.                     {
  108.                         $nextToken $phpcsFile->findNext(T_WHITESPACE($nextToken + 1)nulltrue);
  109.                         if ($tokens[$nextToken]['code'!== T_IF
  110.                             || isset($tokens[$nextToken]['scope_closer']=== false
  111.                         {
  112.                             // Not an ELSE IF or is an inline ELSE IF.
  113.                             break;
  114.                         }
  115.                     }
  116.  
  117.                     if (isset($tokens[$nextToken]['scope_closer']=== false{
  118.                         // There isn't going to be anywhere to print the "end if" comment
  119.                         // because there is no closer.
  120.                         return;
  121.                     }
  122.  
  123.                     // The end brace becomes the ELSE's end brace.
  124.                     $stackPtr $tokens[$nextToken]['scope_closer'];
  125.                     $endBrace $tokens[$stackPtr];
  126.                 else {
  127.                     break;
  128.                 }//end if
  129.             while (isset($tokens[$nextToken]['scope_closer']=== true);
  130.         }//end if
  131.  
  132.         if ($startCondition['code'=== T_TRY{
  133.             // TRY statements need to check until the end of all CATCH statements.
  134.             do {
  135.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  136.                 if ($tokens[$nextToken]['code'=== T_CATCH{
  137.                     // The end brace becomes the CATCH's end brace.
  138.                     $stackPtr $tokens[$nextToken]['scope_closer'];
  139.                     $endBrace $tokens[$stackPtr];
  140.                 else {
  141.                     break;
  142.                 }
  143.             while (isset($tokens[$nextToken]['scope_closer']=== true);
  144.         }
  145.  
  146.         $lineDifference ($endBrace['line'$startBrace['line']);
  147.  
  148.         $expected '//end '.$startCondition['content'];
  149.         $comment  $phpcsFile->findNext(array(T_COMMENT)$stackPtrnullfalse);
  150.  
  151.         if (($comment === false|| ($tokens[$comment]['line'!== $endBrace['line'])) {
  152.             if ($lineDifference >= $this->lineLimit{
  153.                 $error 'End comment for long condition not found; expected "%s"';
  154.                 $data  = array($expected);
  155.                 $fix   $phpcsFile->addFixableError($error$stackPtr'Missing'$data);
  156.  
  157.                 if ($fix === true{
  158.                     $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  159.                     if ($next !== false && $tokens[$next]['line'=== $tokens[$stackPtr]['line']{
  160.                         $expected .= $phpcsFile->eolChar;
  161.                     }
  162.  
  163.                     $phpcsFile->fixer->addContent($stackPtr$expected);
  164.                 }
  165.             }
  166.  
  167.             return;
  168.         }
  169.  
  170.         if (($comment $stackPtr!== 1{
  171.             $error 'Space found before closing comment; expected "%s"';
  172.             $data  = array($expected);
  173.             $phpcsFile->addError($error$stackPtr'SpacingBefore'$data);
  174.         }
  175.  
  176.         if (trim($tokens[$comment]['content']!== $expected{
  177.             $found trim($tokens[$comment]['content']);
  178.             $error 'Incorrect closing comment; expected "%s" but found "%s"';
  179.             $data  = array(
  180.                       $expected,
  181.                       $found,
  182.                      );
  183.  
  184.             $fix $phpcsFile->addFixableError($error$stackPtr'Invalid'$data);
  185.             if ($fix === true{
  186.                 $phpcsFile->fixer->replaceToken($comment$expected.$phpcsFile->eolChar);
  187.             }
  188.  
  189.             return;
  190.         }
  191.  
  192.     }//end process()
  193.  
  194.  
  195. }//end class

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