| Conditions | 12 |
| Paths | 16 |
| Total Lines | 62 |
| Code Lines | 31 |
| Lines | 3 |
| Ratio | 4.84 % |
| 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 |
||
| 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) { |
|
|
|
|||
| 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 | |||
| 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.