These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * PHPCompatibility_Sniffs_PHP_EmptyNonVariableSniff. |
||
| 4 | * |
||
| 5 | * PHP version 5.5 |
||
| 6 | * |
||
| 7 | * @category PHP |
||
| 8 | * @package PHPCompatibility |
||
| 9 | * @author Juliette Reinders Folmer <[email protected]> |
||
| 10 | */ |
||
| 11 | |||
| 12 | /** |
||
| 13 | * PHPCompatibility_Sniffs_PHP_EmptyNonVariableSniff. |
||
| 14 | * |
||
| 15 | * Verify that nothing but variables are passed to empty(). |
||
| 16 | * |
||
| 17 | * PHP version 5.5 |
||
| 18 | * |
||
| 19 | * @category PHP |
||
| 20 | * @package PHPCompatibility |
||
| 21 | * @author Juliette Reinders Folmer <[email protected]> |
||
| 22 | */ |
||
| 23 | class PHPCompatibility_Sniffs_PHP_EmptyNonVariableSniff extends PHPCompatibility_Sniff |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * List of tokens to check against. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $tokenBlackList = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * List of brackets which can be part of a variable variable. |
||
| 34 | * |
||
| 35 | * Key is the open bracket token, value the close bracket token. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $bracketTokens = array( |
||
| 40 | T_OPEN_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET, |
||
| 41 | T_OPEN_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET, |
||
| 42 | ); |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns an array of tokens this test wants to listen for. |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function register() |
||
| 51 | { |
||
| 52 | // Set the token blacklist only once. |
||
| 53 | $tokenBlackList = array_unique(array_merge( |
||
| 54 | PHP_CodeSniffer_Tokens::$assignmentTokens, |
||
| 55 | PHP_CodeSniffer_Tokens::$equalityTokens, |
||
| 56 | PHP_CodeSniffer_Tokens::$comparisonTokens, |
||
| 57 | PHP_CodeSniffer_Tokens::$operators, |
||
| 58 | PHP_CodeSniffer_Tokens::$booleanOperators, |
||
| 59 | PHP_CodeSniffer_Tokens::$castTokens, |
||
| 60 | array(T_OPEN_PARENTHESIS, T_STRING_CONCAT) |
||
| 61 | )); |
||
| 62 | $this->tokenBlackList = array_combine($tokenBlackList, $tokenBlackList); |
||
| 63 | |||
| 64 | return array(T_EMPTY); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Processes this test, when one of its tokens is encountered. |
||
| 69 | * |
||
| 70 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 71 | * @param int $stackPtr The position of the current token in the |
||
| 72 | * stack passed in $tokens. |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 77 | { |
||
| 78 | if ($this->supportsBelow('5.4') === false) { |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | $tokens = $phpcsFile->getTokens(); |
||
| 83 | |||
| 84 | $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true); |
||
| 85 | View Code Duplication | if ($open === false || isset($tokens[$open]['parenthesis_closer']) === false) { |
|
|
0 ignored issues
–
show
|
|||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | $close = $tokens[$open]['parenthesis_closer']; |
||
| 90 | |||
| 91 | // If no variable at all was found, then it's definitely a no-no. |
||
| 92 | $hasVariable = $phpcsFile->findNext(T_VARIABLE, $open + 1, $close); |
||
| 93 | if ($hasVariable === false) { |
||
| 94 | $this->addError($phpcsFile, $stackPtr); |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | // Check if the variable found is at the right level. Deeper levels are always an error. |
||
| 99 | if (isset($tokens[$open + 1]['nested_parenthesis'], $tokens[$hasVariable]['nested_parenthesis'])) { |
||
| 100 | $nestingLevel = count($tokens[$open + 1]['nested_parenthesis']); |
||
| 101 | if (count($tokens[$hasVariable]['nested_parenthesis']) !== $nestingLevel) { |
||
| 102 | $this->addError($phpcsFile, $stackPtr); |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | // Ok, so the first variable is at the right level, now are there any |
||
| 108 | // blacklisted tokens within the empty() ? |
||
| 109 | $hasBadToken = $phpcsFile->findNext($this->tokenBlackList, $open + 1, $close); |
||
| 110 | if ($hasBadToken === false) { |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | |||
| 114 | // If there are also bracket tokens, the blacklisted token might be part of a variable |
||
| 115 | // variable, but if there are no bracket tokens, we know we have an error. |
||
| 116 | $hasBrackets = $phpcsFile->findNext($this->bracketTokens, $open + 1, $close); |
||
| 117 | if ($hasBrackets === false) { |
||
| 118 | $this->addError($phpcsFile, $stackPtr); |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | // Ok, we have both a blacklisted token as well as brackets, so we need to walk |
||
| 123 | // the tokens of the variable variable. |
||
| 124 | for ($i = ($open + 1); $i < $close; $i++) { |
||
| 125 | // If this is a bracket token, skip to the end of the bracketed expression. |
||
| 126 | if (isset($this->bracketTokens[$tokens[$i]['code']], $tokens[$i]['bracket_closer'])) { |
||
| 127 | $i = $tokens[$i]['bracket_closer']; |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | |||
| 131 | // If it's a blacklisted token, not within brackets, we have an error. |
||
| 132 | if (isset($this->tokenBlackList[$tokens[$i]['code']])) { |
||
| 133 | $this->addError($phpcsFile, $stackPtr); |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Add the error message. |
||
| 142 | * |
||
| 143 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 144 | * @param int $stackPtr The position of the current token in the |
||
| 145 | * stack passed in $tokens. |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | protected function addError($phpcsFile, $stackPtr) |
||
| 150 | { |
||
| 151 | $phpcsFile->addError( |
||
| 152 | 'Only variables can be passed to empty() prior to PHP 5.5.', |
||
| 153 | $stackPtr, |
||
| 154 | 'Found' |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | } |
||
| 158 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.