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

Source for file AssignmentInConditionSniff.php

Documentation is available at AssignmentInConditionSniff.php

  1. <?php
  2. /**
  3.  * Detects variable assignments being made within conditions.
  4.  *
  5.  * This is a typical code smell and more often than not a comparison was intended.
  6.  *
  7.  * Note: this sniff does not detect variable assignments in the conditional part of ternaries!
  8.  *
  9.  * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
  10.  * @copyright 2017 Juliette Reinders Folmer. All rights reserved.
  11.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12.  */
  13.  
  14. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
  15.  
  16. use PHP_CodeSniffer\Sniffs\Sniff;
  17. use PHP_CodeSniffer\Files\File;
  18. use PHP_CodeSniffer\Util\Tokens;
  19.  
  20. class AssignmentInConditionSniff implements Sniff
  21. {
  22.  
  23.  
  24.     /**
  25.      * Assignment tokens to trigger on.
  26.      *
  27.      * Set in the register() method.
  28.      *
  29.      * @var array 
  30.      */
  31.     protected $assignmentTokens = array();
  32.  
  33.     /**
  34.      * The tokens that indicate the start of a condition.
  35.      *
  36.      * @var array 
  37.      */
  38.     protected $conditionStartTokens = array();
  39.  
  40.  
  41.     /**
  42.      * Registers the tokens that this sniff wants to listen for.
  43.      *
  44.      * @return int[] 
  45.      */
  46.     public function register()
  47.     {
  48.         $this->assignmentTokens = Tokens::$assignmentTokens;
  49.         unset($this->assignmentTokens[T_DOUBLE_ARROW]);
  50.  
  51.         $starters = Tokens::$booleanOperators;
  52.         $starters[T_SEMICOLON]        T_SEMICOLON;
  53.         $starters[T_OPEN_PARENTHESIST_OPEN_PARENTHESIS;
  54.  
  55.         $this->conditionStartTokens $starters;
  56.  
  57.         return array(
  58.                 T_IF,
  59.                 T_ELSEIF,
  60.                 T_FOR,
  61.                 T_SWITCH,
  62.                 T_CASE,
  63.                 T_WHILE,
  64.                );
  65.  
  66.     }//end register()
  67.  
  68.  
  69.     /**
  70.      * Processes this test, when one of its tokens is encountered.
  71.      *
  72.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  73.      * @param int                         $stackPtr  The position of the current token
  74.      *                                                in the stack passed in $tokens.
  75.      *
  76.      * @return void 
  77.      */
  78.     public function process(File $phpcsFile$stackPtr)
  79.     {
  80.         $tokens $phpcsFile->getTokens();
  81.         $token  $tokens[$stackPtr];
  82.  
  83.         // Find the condition opener/closer.
  84.         if ($token['code'=== T_FOR{
  85.             if (isset($token['parenthesis_opener']$token['parenthesis_closer']=== false{
  86.                 return;
  87.             }
  88.  
  89.             $semicolon $phpcsFile->findNext(T_SEMICOLON($token['parenthesis_opener'+ 1)($token['parenthesis_closer']));
  90.             if ($semicolon === false{
  91.                 return;
  92.             }
  93.  
  94.             $opener $semicolon;
  95.  
  96.             $semicolon $phpcsFile->findNext(T_SEMICOLON($opener + 1)($token['parenthesis_closer']));
  97.             if ($semicolon === false{
  98.                 return;
  99.             }
  100.  
  101.             $closer $semicolon;
  102.             unset($semicolon);
  103.         else if ($token['code'=== T_CASE{
  104.             if (isset($token['scope_opener']=== false{
  105.                 return;
  106.             }
  107.  
  108.             $opener $stackPtr;
  109.             $closer $token['scope_opener'];
  110.         else {
  111.             if (isset($token['parenthesis_opener']$token['parenthesis_closer']=== false{
  112.                 return;
  113.             }
  114.  
  115.             $opener $token['parenthesis_opener'];
  116.             $closer $token['parenthesis_closer'];
  117.         }//end if
  118.  
  119.         $startPos $opener;
  120.  
  121.         do {
  122.             $hasAssignment $phpcsFile->findNext($this->assignmentTokens($startPos + 1)$closer);
  123.             if ($hasAssignment === false{
  124.                 return;
  125.             }
  126.  
  127.             // Examine whether the left side is a variable.
  128.             $hasVariable       = false;
  129.             $conditionStart    $startPos;
  130.             $altConditionStart $phpcsFile->findPrevious($this->conditionStartTokens($hasAssignment - 1)$startPos);
  131.             if ($altConditionStart !== false{
  132.                 $conditionStart $altConditionStart;
  133.             }
  134.  
  135.             for ($i $hasAssignment$i $conditionStart$i--{
  136.                 if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]=== true{
  137.                     continue;
  138.                 }
  139.  
  140.                 // If this is a variable or array, we've seen all we need to see.
  141.                 if ($tokens[$i]['code'=== T_VARIABLE || $tokens[$i]['code'=== T_CLOSE_SQUARE_BRACKET{
  142.                     $hasVariable = true;
  143.                     break;
  144.                 }
  145.  
  146.                 // If this is a function call or something, we are OK.
  147.                 if ($tokens[$i]['code'=== T_CLOSE_PARENTHESIS{
  148.                     break;
  149.                 }
  150.             }
  151.  
  152.             if ($hasVariable === true{
  153.                 $phpcsFile->addWarning(
  154.                     'Variable assignment found within a condition. Did you mean to do a comparison ?',
  155.                     $hasAssignment,
  156.                     'Found'
  157.                 );
  158.             }
  159.  
  160.             $startPos $hasAssignment;
  161.         while ($startPos $closer);
  162.  
  163.     }//end process()
  164.  
  165.  
  166. }//end class

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