Conditions | 9 |
Paths | 6 |
Total Lines | 60 |
Code Lines | 31 |
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 |
||
172 | protected function isCallTimePassByReferenceParam(\PHP_CodeSniffer_File $phpcsFile, $parameter, $nestingLevel) |
||
173 | { |
||
174 | $tokens = $phpcsFile->getTokens(); |
||
175 | |||
176 | $searchStartToken = $parameter['start'] - 1; |
||
177 | $searchEndToken = $parameter['end'] + 1; |
||
178 | $nextVariable = $searchStartToken; |
||
179 | do { |
||
180 | $nextVariable = $phpcsFile->findNext(T_VARIABLE, ($nextVariable + 1), $searchEndToken); |
||
181 | if ($nextVariable === false) { |
||
182 | return false; |
||
183 | } |
||
184 | |||
185 | // Make sure the variable belongs directly to this function call |
||
186 | // and is not inside a nested function call or array. |
||
187 | if (isset($tokens[$nextVariable]['nested_parenthesis']) === false |
||
188 | || (count($tokens[$nextVariable]['nested_parenthesis']) !== $nestingLevel) |
||
189 | ) { |
||
190 | continue; |
||
191 | } |
||
192 | |||
193 | // Checking this: $value = my_function(...[*]$arg...). |
||
|
|||
194 | $tokenBefore = $phpcsFile->findPrevious( |
||
195 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
196 | ($nextVariable - 1), |
||
197 | $searchStartToken, |
||
198 | true |
||
199 | ); |
||
200 | |||
201 | if ($tokenBefore === false || $tokens[$tokenBefore]['code'] !== T_BITWISE_AND) { |
||
202 | // Nothing before the token or no &. |
||
203 | continue; |
||
204 | } |
||
205 | |||
206 | if ($phpcsFile->isReference($tokenBefore) === false) { |
||
207 | continue; |
||
208 | } |
||
209 | |||
210 | // Checking this: $value = my_function(...[*]&$arg...). |
||
211 | $tokenBefore = $phpcsFile->findPrevious( |
||
212 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
213 | ($tokenBefore - 1), |
||
214 | $searchStartToken, |
||
215 | true |
||
216 | ); |
||
217 | |||
218 | // Prevent false positive on assign by reference and compare with reference |
||
219 | // within function call parameters. |
||
220 | if (isset($this->assignOrCompare[$tokens[$tokenBefore]['type']])) { |
||
221 | continue; |
||
222 | } |
||
223 | |||
224 | // The found T_BITWISE_AND represents a pass-by-reference. |
||
225 | return true; |
||
226 | |||
227 | } while ($nextVariable < $searchEndToken); |
||
228 | |||
229 | // This code should never be reached, but here in case of weird bugs ;-) |
||
230 | return false; |
||
231 | } |
||
232 | |||
234 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.