| Conditions | 14 |
| Paths | 10 |
| Total Lines | 86 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 120 | protected function isCallTimePassByReferenceParam(PHP_CodeSniffer_File $phpcsFile, $parameter, $nestingLevel) |
||
| 121 | { |
||
| 122 | $tokens = $phpcsFile->getTokens(); |
||
| 123 | |||
| 124 | $searchStartToken = $parameter['start'] - 1; |
||
| 125 | $searchEndToken = $parameter['end'] + 1; |
||
| 126 | $nextVariable = $searchStartToken; |
||
| 127 | do { |
||
| 128 | $nextVariable = $phpcsFile->findNext(T_VARIABLE, ($nextVariable + 1), $searchEndToken); |
||
| 129 | if ($nextVariable === false) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | // Make sure the variable belongs directly to this function call |
||
| 134 | // and is not inside a nested function call or array. |
||
| 135 | if (isset($tokens[$nextVariable]['nested_parenthesis']) === false || |
||
| 136 | (count($tokens[$nextVariable]['nested_parenthesis']) !== $nestingLevel) |
||
| 137 | ) { |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | // Checking this: $value = my_function(...[*]$arg...). |
||
| 143 | $tokenBefore = $phpcsFile->findPrevious( |
||
| 144 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 145 | ($nextVariable - 1), |
||
| 146 | $searchStartToken, |
||
| 147 | true |
||
| 148 | ); |
||
| 149 | |||
| 150 | if ($tokenBefore === false || $tokens[$tokenBefore]['code'] !== T_BITWISE_AND) { |
||
| 151 | // Nothing before the token or no &. |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Checking this: $value = my_function(...[*]&$arg...). |
||
| 156 | $tokenBefore = $phpcsFile->findPrevious( |
||
| 157 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 158 | ($tokenBefore - 1), |
||
| 159 | $searchStartToken, |
||
| 160 | true |
||
| 161 | ); |
||
| 162 | |||
| 163 | // We have to exclude all uses of T_BITWISE_AND that are not |
||
| 164 | // references. We use a blacklist approach as we prefer false |
||
| 165 | // positives to not identifying a pass-by-reference call at all. |
||
| 166 | // The blacklist may not yet be complete. |
||
| 167 | switch ($tokens[$tokenBefore]['code']) { |
||
| 168 | // In these cases T_BITWISE_AND represents |
||
| 169 | // the bitwise and operator. |
||
| 170 | case T_LNUMBER: |
||
| 171 | case T_VARIABLE: |
||
| 172 | case T_CLOSE_SQUARE_BRACKET: |
||
| 173 | case T_CLOSE_PARENTHESIS: |
||
| 174 | break; |
||
| 175 | |||
| 176 | // Unfortunately the tokenizer fails to recognize global constants, |
||
| 177 | // class-constants and -attributes. Any of these are returned is |
||
| 178 | // treated as T_STRING. |
||
| 179 | // So we step back another token and check if it is a class |
||
| 180 | // operator (-> or ::), which means we have a false positive. |
||
| 181 | // Global constants still remain uncovered. |
||
| 182 | case T_STRING: |
||
| 183 | $tokenBeforePlus = $phpcsFile->findPrevious( |
||
| 184 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 185 | ($tokenBefore - 1), |
||
| 186 | $searchStartToken, |
||
| 187 | true |
||
| 188 | ); |
||
| 189 | if ($tokens[$tokenBeforePlus]['code'] === T_DOUBLE_COLON || |
||
| 190 | $tokens[$tokenBeforePlus]['code'] === T_OBJECT_OPERATOR |
||
| 191 | ) { |
||
| 192 | break; |
||
| 193 | } |
||
| 194 | // If not a class constant: fall through. |
||
| 195 | |||
| 196 | default: |
||
| 197 | // The found T_BITWISE_AND represents a pass-by-reference. |
||
| 198 | return true; |
||
| 199 | } |
||
| 200 | |||
| 201 | } while($nextVariable < $searchEndToken); |
||
| 202 | |||
| 203 | // This code should never be reached, but here in case of weird bugs ;-) |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | |||
| 208 |
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.