Conditions | 16 |
Paths | 12 |
Total Lines | 57 |
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 |
||
28 | private static function flipElseIf($tokens, $condition, $ifBody, $elseBody) |
||
29 | { |
||
30 | [$ifBlockStartIndex, $ifBody, $ifBlockEndIndex] = $ifBody; |
||
31 | [$elseBodyStartIndex, $elseBody, $elseBodyEndIndex] = $elseBody; |
||
32 | [$conditionStartIndex, $condition, $conditionCloseIndex] = $condition; |
||
33 | |||
34 | $refactoredTokens = []; |
||
35 | foreach ($tokens as $i => $oldToken) { |
||
36 | // negate the condition |
||
37 | if ($conditionStartIndex == $i) { |
||
38 | $refactoredTokens[] = '('; |
||
39 | $negatedConditionTokens = Condition::negate($condition); |
||
40 | foreach ($negatedConditionTokens as $t) { |
||
41 | $refactoredTokens[] = $t; |
||
42 | } |
||
43 | continue; |
||
44 | } |
||
45 | |||
46 | if ($i >= $conditionStartIndex && $i < $conditionCloseIndex) { |
||
47 | continue; |
||
48 | } |
||
49 | |||
50 | if ($i == $ifBlockStartIndex) { |
||
51 | $refactoredTokens[] = '{'; |
||
52 | foreach ($elseBody as $t) { |
||
53 | $refactoredTokens[] = $t; |
||
54 | } |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | if ($i > $ifBlockStartIndex && $i < $ifBlockEndIndex) { |
||
59 | continue; |
||
60 | } |
||
61 | |||
62 | // removes: } else { |
||
63 | if ($i >= $ifBlockEndIndex && $i < $elseBodyStartIndex) { |
||
64 | continue; |
||
65 | } |
||
66 | |||
67 | // close the body and what was in the else block after it. |
||
68 | if ($i == $elseBodyStartIndex) { |
||
69 | $refactoredTokens[] = '}'; |
||
70 | foreach ($ifBody as $t) { |
||
71 | $refactoredTokens[] = $t; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // ignore the else body. |
||
76 | if ($i >= $elseBodyStartIndex && $i <= $elseBodyEndIndex) { |
||
77 | continue; |
||
78 | } |
||
79 | |||
80 | $refactoredTokens[] = $oldToken; |
||
81 | } |
||
82 | |||
83 | return $refactoredTokens; |
||
84 | } |
||
85 | } |
||
86 |
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.