| Conditions | 23 |
| Paths | 138 |
| Total Lines | 98 |
| 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 |
||
| 12 | public static function normalizeSyntax($tokens, $replace = false) |
||
| 13 | { |
||
| 14 | $ends = [T_ENDFOR, T_ENDIF, T_ENDFOREACH, T_ENDWHILE]; |
||
| 15 | $start = [T_FOR, T_IF, T_FOREACH, T_WHILE, T_ELSEIF]; |
||
| 16 | $i = 0; |
||
| 17 | $refactoredTokens = []; |
||
| 18 | $tCount = \count($tokens); |
||
| 19 | $ifIf = []; |
||
| 20 | $closing = $replace ? '}' : ['}', '']; |
||
| 21 | |||
| 22 | $opening = function ($or = '') use ($replace) { |
||
| 23 | return $replace ? '{' : ['{', $or]; |
||
| 24 | }; |
||
| 25 | |||
| 26 | self::$hasChange = false; |
||
| 27 | |||
| 28 | while ($tCount > $i) { |
||
| 29 | $t = $tokens[$i]; |
||
| 30 | if (\in_array($t[0], $ends)) { |
||
| 31 | self::$hasChange = true; |
||
| 32 | // replace the ruby-style syntax with C-style |
||
| 33 | $refactoredTokens[] = $replace ? '}' : ['}', $t[1]]; |
||
| 34 | $replace && self::removeSemi($tokens, $i); |
||
| 35 | $i++; |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | |||
| 39 | if (\in_array($t[0], $start)) { |
||
| 40 | // forward to end of parenthesis |
||
| 41 | [, , $u] = Ifs::readCondition($tokens, $i); |
||
|
|
|||
| 42 | // read first char after the parenthesis |
||
| 43 | [$next, $u] = TokenManager::getNextToken($tokens, $u); |
||
| 44 | if ($next == ':') { |
||
| 45 | $tokens[$u] = $opening(':'); |
||
| 46 | // Adds a closing curly brace "}" before elseif. |
||
| 47 | $t[0] == T_ELSEIF && $refactoredTokens[] = $closing; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($t[0] == T_ELSE || $t[0] == T_IF) { |
||
| 52 | if ($t[0] == T_ELSE) { |
||
| 53 | [$next_T, $next_I] = TokenManager::getNextToken($tokens, $i); |
||
| 54 | } else { |
||
| 55 | [, , $u] = Ifs::readCondition($tokens, $i); |
||
| 56 | [$next_T, $next_I] = TokenManager::getNextToken($tokens, $u); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (\in_array($next_T[0], [T_FOR, T_FOREACH, T_WHILE])) { |
||
| 60 | array_splice($tokens, $next_I, 0, [$opening()]); |
||
| 61 | $refactoredTokens[] = $t; |
||
| 62 | $i++; |
||
| 63 | [, , $u] = Ifs::readCondition($tokens, $next_I + 1); |
||
| 64 | [, $u] = TokenManager::getNextToken($tokens, $u); |
||
| 65 | [, $u] = TokenManager::readBody($tokens, $u); |
||
| 66 | array_splice($tokens, $u, 0, [$closing]); |
||
| 67 | |||
| 68 | // we update the count since the number of elements is changed. |
||
| 69 | $tCount = \count($tokens); |
||
| 70 | continue; |
||
| 71 | } elseif ($next_T[0] !== T_IF && $next_T !== '{' && $next_T !== ':') { |
||
| 72 | /** |
||
| 73 | * in case if or else block is like this: |
||
| 74 | * if ($v) { |
||
| 75 | * ... |
||
| 76 | * } else |
||
| 77 | * $var = 0;. |
||
| 78 | */ |
||
| 79 | $refactoredTokens[] = $t; |
||
| 80 | array_splice($tokens, $next_I - 1, 0, [$opening()]); |
||
| 81 | [, $endIndex] = TokenManager::forwardTo($tokens, $i, [';']); |
||
| 82 | $NEXT = TokenManager::getNextToken($tokens, $endIndex); |
||
| 83 | if ($NEXT[0][0] == T_ELSE && $t[0] == T_ELSE) { |
||
| 84 | $ia = array_pop($ifIf); |
||
| 85 | array_splice($refactoredTokens, $ia, 0, [$opening()]); |
||
| 86 | array_splice($tokens, $endIndex + 2, 0, [$closing]); |
||
| 87 | } |
||
| 88 | array_splice($tokens, $endIndex + 2, 0, [$closing]); |
||
| 89 | $tCount = \count($tokens); |
||
| 90 | $i++; |
||
| 91 | continue; |
||
| 92 | } elseif ($t[0] == T_IF && $next_T[0] === T_IF) { |
||
| 93 | $ifIf[] = $next_I; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | [$next, $u] = TokenManager::getNextToken($tokens, $i); |
||
| 98 | |||
| 99 | if ($next == ':' && $t[0] == T_ELSE) { |
||
| 100 | $tokens[$u] = $opening(':'); |
||
| 101 | $refactoredTokens[] = $closing; |
||
| 102 | } |
||
| 103 | |||
| 104 | $refactoredTokens[] = $t; |
||
| 105 | $i++; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $refactoredTokens; |
||
| 109 | } |
||
| 110 | |||
| 118 |
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.