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

Source for file DisallowComparisonAssignmentSniff.php

Documentation is available at DisallowComparisonAssignmentSniff.php

  1. <?php
  2. /**
  3.  * Ensures that the value of a comparison is not assigned to a variable.
  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.  
  16. class DisallowComparisonAssignmentSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return array(T_EQUAL);
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this test, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  36.      * @param int                         $stackPtr  The position of the current token
  37.      *                                                in the stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile, $stackPtr)
  42.     {
  43.         $tokens = $phpcsFile->getTokens();
  44.  
  45.         // Ignore default value assignments in function definitions.
  46.         $function = $phpcsFile->findPrevious(T_FUNCTION, ($stackPtr - 1), null, false, null, true);
  47.         if ($function !== false) {
  48.             $opener = $tokens[$function]['parenthesis_opener'];
  49.             $closer = $tokens[$function]['parenthesis_closer'];
  50.             if ($opener < $stackPtr && $closer > $stackPtr) {
  51.                 return;
  52.             }
  53.         }
  54.  
  55.         // Ignore values in array definitions.
  56.         $array = $phpcsFile->findNext(
  57.             T_ARRAY,
  58.             ($stackPtr + 1),
  59.             null,
  60.             false,
  61.             null,
  62.             true
  63.         );
  64.  
  65.         if ($array !== false) {
  66.             return;
  67.         }
  68.  
  69.         // Ignore function calls.
  70.         $ignore = array(
  71.                    T_STRING,
  72.                    T_WHITESPACE,
  73.                    T_OBJECT_OPERATOR,
  74.                   );
  75.  
  76.         $next = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true);
  77.         if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS
  78.             && $tokens[($next - 1)]['code'] === T_STRING
  79.         ) {
  80.             // Code will look like: $var = myFunction(
  81.             // and will be ignored.
  82.             return;
  83.         }
  84.  
  85.         $endStatement = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
  86.         if ($tokens[$stackPtr]['conditions'] !== $tokens[$endStatement]['conditions']) {
  87.             // This statement doesn't end with a semicolon, which is the case for
  88.             // the last expression in a for loop.
  89.             return;
  90.         }
  91.  
  92.         for ($i = ($stackPtr + 1); $i < $endStatement; $i++) {
  93.             if (isset(Tokens::$comparisonTokens[$tokens[$i]['code']]) === true
  94.                 || $tokens[$i]['code'] === T_INLINE_THEN
  95.             ) {
  96.                 $error = 'The value of a comparison must not be assigned to a variable';
  97.                 $phpcsFile->addError($error, $stackPtr, 'AssignedComparison');
  98.                 break;
  99.             }
  100.  
  101.             if (isset(Tokens::$booleanOperators[$tokens[$i]['code']]) === true
  102.                 || $tokens[$i]['code'] === T_BOOLEAN_NOT
  103.             ) {
  104.                 $error = 'The value of a boolean operation must not be assigned to a variable';
  105.                 $phpcsFile->addError($error, $stackPtr, 'AssignedBool');
  106.                 break;
  107.             }
  108.         }
  109.  
  110.     }//end process()
  111.  
  112.  
  113. }//end class

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