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.     public $lineLimit = 20;
  50.  
  51.     /**
  52.      * The format the end comment should be in.
  53.      *
  54.      * The placeholder %s will be replaced with the type of condition opener.
  55.      *
  56.      * @var string 
  57.      */
  58.     public $commentFormat '//end %s';
  59.  
  60.  
  61.     /**
  62.      * Returns an array of tokens this test wants to listen for.
  63.      *
  64.      * @return array 
  65.      */
  66.     public function register()
  67.     {
  68.         return array(T_CLOSE_CURLY_BRACKET);
  69.  
  70.     }//end register()
  71.  
  72.  
  73.     /**
  74.      * Processes this test, when one of its tokens is encountered.
  75.      *
  76.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  77.      * @param int                         $stackPtr  The position of the current token in the
  78.      *                                                stack passed in $tokens.
  79.      *
  80.      * @return void 
  81.      */
  82.     public function process(File $phpcsFile$stackPtr)
  83.     {
  84.         $tokens $phpcsFile->getTokens();
  85.  
  86.         if (isset($tokens[$stackPtr]['scope_condition']=== false{
  87.             // No scope condition. It is a function closer.
  88.             return;
  89.         }
  90.  
  91.         $startCondition $tokens[$tokens[$stackPtr]['scope_condition']];
  92.         $startBrace     $tokens[$tokens[$stackPtr]['scope_opener']];
  93.         $endBrace       $tokens[$stackPtr];
  94.  
  95.         // We are only interested in some code blocks.
  96.         if (in_array($startCondition['code']self::$openers=== false{
  97.             return;
  98.         }
  99.  
  100.         if ($startCondition['code'=== T_IF{
  101.             // If this is actually an ELSE IF, skip it as the brace
  102.             // will be checked by the original IF.
  103.             $else $phpcsFile->findPrevious(T_WHITESPACE($tokens[$stackPtr]['scope_condition'- 1)nulltrue);
  104.             if ($tokens[$else]['code'=== T_ELSE{
  105.                 return;
  106.             }
  107.  
  108.             // IF statements that have an ELSE block need to use
  109.             // "end if" rather than "end else" or "end elseif".
  110.             do {
  111.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  112.                 if ($tokens[$nextToken]['code'=== T_ELSE || $tokens[$nextToken]['code'=== T_ELSEIF{
  113.                     // Check for ELSE IF (2 tokens) as opposed to ELSEIF (1 token).
  114.                     if ($tokens[$nextToken]['code'=== T_ELSE
  115.                         && isset($tokens[$nextToken]['scope_closer']=== false
  116.                     {
  117.                         $nextToken $phpcsFile->findNext(T_WHITESPACE($nextToken + 1)nulltrue);
  118.                         if ($tokens[$nextToken]['code'!== T_IF
  119.                             || isset($tokens[$nextToken]['scope_closer']=== false
  120.                         {
  121.                             // Not an ELSE IF or is an inline ELSE IF.
  122.                             break;
  123.                         }
  124.                     }
  125.  
  126.                     if (isset($tokens[$nextToken]['scope_closer']=== false{
  127.                         // There isn't going to be anywhere to print the "end if" comment
  128.                         // because there is no closer.
  129.                         return;
  130.                     }
  131.  
  132.                     // The end brace becomes the ELSE's end brace.
  133.                     $stackPtr $tokens[$nextToken]['scope_closer'];
  134.                     $endBrace $tokens[$stackPtr];
  135.                 else {
  136.                     break;
  137.                 }//end if
  138.             while (isset($tokens[$nextToken]['scope_closer']=== true);
  139.         }//end if
  140.  
  141.         if ($startCondition['code'=== T_TRY{
  142.             // TRY statements need to check until the end of all CATCH statements.
  143.             do {
  144.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  145.                 if ($tokens[$nextToken]['code'=== T_CATCH
  146.                     || $tokens[$nextToken]['code'=== T_FINALLY
  147.                 {
  148.                     // The end brace becomes the CATCH's end brace.
  149.                     $stackPtr $tokens[$nextToken]['scope_closer'];
  150.                     $endBrace $tokens[$stackPtr];
  151.                 else {
  152.                     break;
  153.                 }
  154.             while (isset($tokens[$nextToken]['scope_closer']=== true);
  155.         }
  156.  
  157.         $lineDifference ($endBrace['line'$startBrace['line']);
  158.  
  159.         $expected sprintf($this->commentFormat$startCondition['content']);
  160.         $comment  $phpcsFile->findNext(array(T_COMMENT)$stackPtrnullfalse);
  161.  
  162.         if (($comment === false|| ($tokens[$comment]['line'!== $endBrace['line'])) {
  163.             if ($lineDifference >= $this->lineLimit{
  164.                 $error 'End comment for long condition not found; expected "%s"';
  165.                 $data  = array($expected);
  166.                 $fix   $phpcsFile->addFixableError($error$stackPtr'Missing'$data);
  167.  
  168.                 if ($fix === true{
  169.                     $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  170.                     if ($next !== false && $tokens[$next]['line'=== $tokens[$stackPtr]['line']{
  171.                         $expected .= $phpcsFile->eolChar;
  172.                     }
  173.  
  174.                     $phpcsFile->fixer->addContent($stackPtr$expected);
  175.                 }
  176.             }
  177.  
  178.             return;
  179.         }
  180.  
  181.         if (($comment $stackPtr!== 1{
  182.             $error 'Space found before closing comment; expected "%s"';
  183.             $data  = array($expected);
  184.             $phpcsFile->addError($error$stackPtr'SpacingBefore'$data);
  185.         }
  186.  
  187.         if (trim($tokens[$comment]['content']!== $expected{
  188.             $found trim($tokens[$comment]['content']);
  189.             $error 'Incorrect closing comment; expected "%s" but found "%s"';
  190.             $data  = array(
  191.                       $expected,
  192.                       $found,
  193.                      );
  194.  
  195.             $fix $phpcsFile->addFixableError($error$stackPtr'Invalid'$data);
  196.             if ($fix === true{
  197.                 $phpcsFile->fixer->replaceToken($comment$expected.$phpcsFile->eolChar);
  198.             }
  199.  
  200.             return;
  201.         }
  202.  
  203.     }//end process()
  204.  
  205.  
  206. }//end class

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