| Conditions | 17 |
| Paths | 13 |
| Total Lines | 66 |
| 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 |
||
| 52 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 53 | { |
||
| 54 | if ($this->supportsAbove('7.1') === false) { |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | $tokens = $phpcsFile->getTokens(); |
||
| 59 | |||
| 60 | // Verify this use statement is used with a closure - if so, it has to have parenthesis before it. |
||
| 61 | $previousNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
||
| 62 | if ($previousNonEmpty === false || $tokens[$previousNonEmpty]['code'] !== T_CLOSE_PARENTHESIS |
||
| 63 | || isset($tokens[$previousNonEmpty]['parenthesis_opener']) === false |
||
| 64 | ) { |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | // ... and (a variable within) parenthesis after it. |
||
| 69 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
||
| 70 | if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== T_OPEN_PARENTHESIS) { |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) { |
||
| 75 | // Live coding. |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | $closurePtr = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($tokens[$previousNonEmpty]['parenthesis_opener'] - 1), null, true); |
||
| 80 | if ($closurePtr === false || $tokens[$closurePtr]['code'] !== T_CLOSURE) { |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | // Get the parameters declared by the closure. |
||
| 85 | $closureParams = PHPCSHelper::getMethodParameters($phpcsFile, $closurePtr); |
||
| 86 | |||
| 87 | $errorMsg = 'Variables bound to a closure via the use construct cannot use the same name as superglobals, $this, or a declared parameter since PHP 7.1. Found: %s'; |
||
| 88 | |||
| 89 | for ($i = ($nextNonEmpty + 1); $i < $tokens[$nextNonEmpty]['parenthesis_closer']; $i++) { |
||
| 90 | if ($tokens[$i]['code'] !== T_VARIABLE) { |
||
| 91 | continue; |
||
| 92 | } |
||
| 93 | |||
| 94 | $variableName = $tokens[$i]['content']; |
||
| 95 | |||
| 96 | if ($variableName === '$this') { |
||
| 97 | $phpcsFile->addError($errorMsg, $i, 'FoundThis', array($variableName)); |
||
| 98 | continue; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (in_array($variableName, $this->superglobals, true) === true) { |
||
| 102 | $phpcsFile->addError($errorMsg, $i, 'FoundSuperglobal', array($variableName)); |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | |||
| 106 | // Check whether it is one of the parameters declared by the closure. |
||
| 107 | if (empty($closureParams) === false) { |
||
| 108 | foreach ($closureParams as $param) { |
||
| 109 | if ($param['name'] === $variableName) { |
||
| 110 | $phpcsFile->addError($errorMsg, $i, 'FoundShadowParam', array($variableName)); |
||
| 111 | continue 2; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | }//end process() |
||
| 118 | |||
| 120 |