| Conditions | 10 |
| Paths | 12 |
| Total Lines | 53 |
| 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 |
||
| 10 | public static function mergeIfs($tokens, $i) |
||
| 11 | { |
||
| 12 | $token = $tokens[$i]; |
||
| 13 | |||
| 14 | if ($token[0] !== T_IF) { |
||
| 15 | return null; |
||
| 16 | } |
||
| 17 | $condition1 = self::readCondition($tokens, $i); |
||
| 18 | |||
| 19 | [$char, $if1BlockStartIndex] = TokenManager::getNextToken($tokens, $condition1[2]); |
||
| 20 | // if with no curly brace. |
||
| 21 | if ($char[0] == T_IF) { |
||
| 22 | [$char,] = TokenManager::getNextToken($tokens, $if1BlockStartIndex); |
||
| 23 | $condition2 = self::readCondition($tokens, $if1BlockStartIndex); |
||
| 24 | |||
| 25 | $if2Body = self::readBody($tokens, $condition2[2]); |
||
| 26 | |||
| 27 | $afterSecondIf = TokenManager::getNextToken($tokens, $if2Body[2]); |
||
| 28 | |||
| 29 | if (T_ELSEIF !== $afterSecondIf[0][0] && T_ELSE !== $afterSecondIf[0][0]) { |
||
| 30 | return NestedIf::merge($tokens, $condition1[2], $condition2[0], -1); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | // if with no curly brace. |
||
| 35 | if ($char[0] !== '{') { |
||
| 36 | return null; |
||
| 37 | } |
||
| 38 | |||
| 39 | $if2index = self::forwardTo($tokens, $if1BlockStartIndex); |
||
| 40 | |||
| 41 | if ($tokens[$if2index][0] !== T_IF) { |
||
| 42 | return null; |
||
| 43 | } |
||
| 44 | |||
| 45 | $condition2 = self::readCondition($tokens, $if2index); |
||
| 46 | |||
| 47 | $if2Body = self::readBody($tokens, $condition2[2]); |
||
| 48 | [, $if1BodyCloseIndex] = TokenManager::readBody($tokens, $if1BlockStartIndex); |
||
| 49 | $if1closeIndexCandid = self::forwardTo($tokens, $if2Body[2]); |
||
| 50 | |||
| 51 | if ($if1closeIndexCandid !== $if1BodyCloseIndex) { |
||
| 52 | return null; |
||
| 53 | } |
||
| 54 | |||
| 55 | $afterFirstIf = TokenManager::getNextToken($tokens, $if1BodyCloseIndex); |
||
| 56 | |||
| 57 | if (T_ELSEIF == $afterFirstIf[0][0] || T_ELSE == $afterFirstIf[0][0]) { |
||
| 58 | return null; |
||
| 59 | } |
||
| 60 | |||
| 61 | return NestedIf::merge($tokens, $condition1[2], $condition2[0], $if2Body[2]); |
||
| 62 | } |
||
| 63 | |||
| 123 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.