| Conditions | 16 |
| Paths | 14 |
| Total Lines | 63 |
| Code Lines | 32 |
| 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 |
||
| 45 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 46 | { |
||
| 47 | if ($this->supportsAbove('7.0') === false) { |
||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | $tokens = $phpcsFile->getTokens(); |
||
| 52 | |||
| 53 | // Verify that the next token is a square open bracket. If not, bow out. |
||
| 54 | $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
||
| 55 | |||
| 56 | if ($nextToken === false || $tokens[$nextToken]['code'] !== T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) { |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | // The previous token has to be a $, -> or ::. |
||
| 61 | if (isset($tokens[($stackPtr - 1)]) === false || in_array($tokens[($stackPtr - 1)]['code'], array(T_DOLLAR, T_OBJECT_OPERATOR, T_DOUBLE_COLON), true) === false) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | // For static object calls, it only applies when this is a function call. |
||
| 66 | if ($tokens[($stackPtr - 1)]['code'] === T_DOUBLE_COLON) { |
||
| 67 | $hasBrackets = $tokens[$nextToken]['bracket_closer']; |
||
| 68 | while (($hasBrackets = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false ) { |
||
| 69 | if ($tokens[$hasBrackets]['code'] === T_OPEN_SQUARE_BRACKET) { |
||
| 70 | if (isset($tokens[$hasBrackets]['bracket_closer'])) { |
||
| 71 | $hasBrackets = $tokens[$hasBrackets]['bracket_closer']; |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | else { |
||
| 75 | // Live coding. |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | elseif ($tokens[$hasBrackets]['code'] === T_OPEN_PARENTHESIS) { |
||
| 80 | // Caught! |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | else { |
||
| 84 | // Not a function call, so bow out. |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // Now let's also prevent false positives when used with self and static which still work fine. |
||
| 90 | $classToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true, null, true); |
||
| 91 | if ($classToken !== false) { |
||
| 92 | if ($tokens[$classToken]['code'] === T_STATIC) { |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | elseif ($tokens[$classToken]['code'] === T_STRING && $tokens[$classToken]['content'] === 'self') { |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | $phpcsFile->addError( |
||
| 102 | '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.', |
||
| 103 | $stackPtr, |
||
| 104 | 'Found' |
||
| 105 | ); |
||
| 106 | |||
| 107 | }//end process() |
||
| 108 | |||
| 111 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.