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

Source for file IncrementDecrementUsageSniff.php

Documentation is available at IncrementDecrementUsageSniff.php

  1. <?php
  2. /**
  3.  * Ensures that the ++ operators are used when possible.
  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\Operators;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class IncrementDecrementUsageSniff 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(
  28.                 T_EQUAL,
  29.                 T_PLUS_EQUAL,
  30.                 T_MINUS_EQUAL,
  31.                 T_INC,
  32.                 T_DEC,
  33.                );
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes this test, when one of its tokens is encountered.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  42.      * @param int                         $stackPtr  The position of the current token
  43.      *                                                in the stack passed in $tokens.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile$stackPtr)
  48.     {
  49.         $tokens $phpcsFile->getTokens();
  50.  
  51.         if ($tokens[$stackPtr]['code'=== T_INC || $tokens[$stackPtr]['code'=== T_DEC{
  52.             $this->processIncDec($phpcsFile$stackPtr);
  53.         else {
  54.             $this->processAssignment($phpcsFile$stackPtr);
  55.         }
  56.  
  57.     }//end process()
  58.  
  59.  
  60.     /**
  61.      * Checks to ensure increment and decrement operators are not confusing.
  62.      *
  63.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  64.      * @param int                         $stackPtr  The position of the current token
  65.      *                                                in the stack passed in $tokens.
  66.      *
  67.      * @return void 
  68.      */
  69.     protected function processIncDec($phpcsFile$stackPtr)
  70.     {
  71.         $tokens $phpcsFile->getTokens();
  72.  
  73.         // Work out where the variable is so we know where to
  74.         // start looking for other operators.
  75.         if ($tokens[($stackPtr - 1)]['code'=== T_VARIABLE
  76.             || ($tokens[($stackPtr - 1)]['code'=== T_STRING
  77.             && $tokens[($stackPtr - 2)]['code'=== T_OBJECT_OPERATOR)
  78.         {
  79.             $start ($stackPtr + 1);
  80.         else {
  81.             $start ($stackPtr + 2);
  82.         }
  83.  
  84.         $next $phpcsFile->findNext(Tokens::$emptyTokens$startnulltrue);
  85.         if ($next === false{
  86.             return;
  87.         }
  88.  
  89.         if (isset(Tokens::$arithmeticTokens[$tokens[$next]['code']]=== true{
  90.             $error 'Increment and decrement operators cannot be used in an arithmetic operation';
  91.             $phpcsFile->addError($error$stackPtr'NotAllowed');
  92.             return;
  93.         }
  94.  
  95.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($start - 3)nulltrue);
  96.         if ($prev === false{
  97.             return;
  98.         }
  99.  
  100.         // Check if this is in a string concat.
  101.         if ($tokens[$next]['code'=== T_STRING_CONCAT || $tokens[$prev]['code'=== T_STRING_CONCAT{
  102.             $error 'Increment and decrement operators must be bracketed when used in string concatenation';
  103.             $phpcsFile->addError($error$stackPtr'NoBrackets');
  104.         }
  105.  
  106.     }//end processIncDec()
  107.  
  108.  
  109.     /**
  110.      * Checks to ensure increment and decrement operators are used.
  111.      *
  112.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  113.      * @param int                         $stackPtr  The position of the current token
  114.      *                                                in the stack passed in $tokens.
  115.      *
  116.      * @return void 
  117.      */
  118.     protected function processAssignment($phpcsFile$stackPtr)
  119.     {
  120.         $tokens $phpcsFile->getTokens();
  121.  
  122.         $assignedVar $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  123.         // Not an assignment, return.
  124.         if ($tokens[$assignedVar]['code'!== T_VARIABLE{
  125.             return;
  126.         }
  127.  
  128.         $statementEnd $phpcsFile->findNext(array(T_SEMICOLONT_CLOSE_PARENTHESIST_CLOSE_SQUARE_BRACKETT_CLOSE_CURLY_BRACKET)$stackPtr);
  129.  
  130.         // If there is anything other than variables, numbers, spaces or operators we need to return.
  131.         $noiseTokens $phpcsFile->findNext(array(T_LNUMBERT_VARIABLET_WHITESPACET_PLUST_MINUST_OPEN_PARENTHESIS)($stackPtr + 1)$statementEndtrue);
  132.  
  133.         if ($noiseTokens !== false{
  134.             return;
  135.         }
  136.  
  137.         // If we are already using += or -=, we need to ignore
  138.         // the statement if a variable is being used.
  139.         if ($tokens[$stackPtr]['code'!== T_EQUAL{
  140.             $nextVar $phpcsFile->findNext(T_VARIABLE($stackPtr + 1)$statementEnd);
  141.             if ($nextVar !== false{
  142.                 return;
  143.             }
  144.         }
  145.  
  146.         if ($tokens[$stackPtr]['code'=== T_EQUAL{
  147.             $nextVar          ($stackPtr + 1);
  148.             $previousVariable ($stackPtr + 1);
  149.             $variableCount    = 0;
  150.             while (($nextVar $phpcsFile->findNext(T_VARIABLE($nextVar + 1)$statementEnd)) !== false{
  151.                 $previousVariable $nextVar;
  152.                 $variableCount++;
  153.             }
  154.  
  155.             if ($variableCount !== 1{
  156.                 return;
  157.             }
  158.  
  159.             $nextVar $previousVariable;
  160.             if ($tokens[$nextVar]['content'!== $tokens[$assignedVar]['content']{
  161.                 return;
  162.             }
  163.         }
  164.  
  165.         // We have only one variable, and it's the same as what is being assigned,
  166.         // so we need to check what is being added or subtracted.
  167.         $nextNumber     ($stackPtr + 1);
  168.         $previousNumber ($stackPtr + 1);
  169.         $numberCount    = 0;
  170.         while (($nextNumber $phpcsFile->findNext(array(T_LNUMBER)($nextNumber + 1)$statementEndfalse)) !== false{
  171.             $previousNumber $nextNumber;
  172.             $numberCount++;
  173.         }
  174.  
  175.         if ($numberCount !== 1{
  176.             return;
  177.         }
  178.  
  179.         $nextNumber $previousNumber;
  180.         if ($tokens[$nextNumber]['content'=== '1'{
  181.             if ($tokens[$stackPtr]['code'=== T_EQUAL{
  182.                 $opToken $phpcsFile->findNext(array(T_PLUST_MINUS)($nextVar + 1)$statementEnd);
  183.                 if ($opToken === false{
  184.                     // Operator was before the variable, like:
  185.                     // $var = 1 + $var;
  186.                     // So we ignore it.
  187.                     return;
  188.                 }
  189.  
  190.                 $operator $tokens[$opToken]['content'];
  191.             else {
  192.                 $operator substr($tokens[$stackPtr]['content']01);
  193.             }
  194.  
  195.             // If we are adding or subtracting negative value, the operator
  196.             // needs to be reversed.
  197.             if ($tokens[$stackPtr]['code'!== T_EQUAL{
  198.                 $negative $phpcsFile->findPrevious(T_MINUS($nextNumber - 1)$stackPtr);
  199.                 if ($negative !== false{
  200.                     if ($operator === '+'{
  201.                         $operator '-';
  202.                     else {
  203.                         $operator '+';
  204.                     }
  205.                 }
  206.             }
  207.  
  208.             $expected $tokens[$assignedVar]['content'].$operator.$operator;
  209.             $found    $phpcsFile->getTokensAsString($assignedVar($statementEnd $assignedVar + 1));
  210.  
  211.             if ($operator === '+'{
  212.                 $error 'Increment';
  213.             else {
  214.                 $error 'Decrement';
  215.             }
  216.  
  217.             $error .= " operators should be used where possible; found \"$found\" but expected \"$expected\"";
  218.             $phpcsFile->addError($error$stackPtr'Found');
  219.         }//end if
  220.  
  221.     }//end processAssignment()
  222.  
  223.  
  224. }//end class

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