| Conditions | 17 |
| Paths | 14 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 49 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 50 | { |
||
| 51 | if ($this->supportsAbove('7.0') === false) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | $tokens = $phpcsFile->getTokens(); |
||
| 56 | |||
| 57 | // Verify that the next token is a square open bracket. If not, bow out. |
||
| 58 | $nextToken = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
||
| 59 | |||
| 60 | if ($nextToken === false || $tokens[$nextToken]['code'] !== T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) { |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | // The previous non-empty token has to be a $, -> or ::. |
||
| 65 | $prevToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
||
| 66 | if ($prevToken === false || in_array($tokens[$prevToken]['code'], array(T_DOLLAR, T_OBJECT_OPERATOR, T_DOUBLE_COLON), true) === false) { |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | // For static object calls, it only applies when this is a function call. |
||
| 71 | if ($tokens[$prevToken]['code'] === T_DOUBLE_COLON) { |
||
| 72 | $hasBrackets = $tokens[$nextToken]['bracket_closer']; |
||
| 73 | while (($hasBrackets = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false) { |
||
| 74 | if ($tokens[$hasBrackets]['code'] === T_OPEN_SQUARE_BRACKET) { |
||
| 75 | if (isset($tokens[$hasBrackets]['bracket_closer'])) { |
||
| 76 | $hasBrackets = $tokens[$hasBrackets]['bracket_closer']; |
||
| 77 | continue; |
||
| 78 | } else { |
||
| 79 | // Live coding. |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | } elseif ($tokens[$hasBrackets]['code'] === T_OPEN_PARENTHESIS) { |
||
| 84 | // Caught! |
||
| 85 | break; |
||
| 86 | |||
| 87 | } else { |
||
| 88 | // Not a function call, so bow out. |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | // Now let's also prevent false positives when used with self and static which still work fine. |
||
| 94 | $classToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($prevToken - 1), null, true, null, true); |
||
| 95 | if ($classToken !== false) { |
||
| 96 | if ($tokens[$classToken]['code'] === T_STATIC || $tokens[$classToken]['code'] === T_SELF) { |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | elseif ($tokens[$classToken]['code'] === T_STRING && $tokens[$classToken]['content'] === 'self') { |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | $phpcsFile->addError( |
||
| 106 | 'Indirect access to variables, properties and methods will be evaluated strictly in left-to-right order since PHP 7.0. Use curly braces to remove ambiguity.', |
||
| 107 | $stackPtr, |
||
| 108 | 'Found' |
||
| 109 | ); |
||
| 110 | |||
| 111 | }//end process() |
||
| 112 | |||
| 115 |