| Conditions | 20 |
| Paths | 32 |
| Total Lines | 118 |
| Code Lines | 51 |
| 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 |
||
| 30 | public function collect($tokens) |
||
| 31 | { |
||
| 32 | $result = []; |
||
| 33 | foreach ($tokens as $index => $token) |
||
| 34 | { |
||
| 35 | // Check for exists/returns, eval's, echo and other constructs |
||
| 36 | $toCheck = [ |
||
| 37 | // Eval |
||
| 38 | T_EVAL, |
||
| 39 | |||
| 40 | // Echo constructs |
||
| 41 | T_ECHO, |
||
| 42 | T_PRINT, |
||
| 43 | |||
| 44 | // Finishing instructions |
||
| 45 | T_RETURN, |
||
| 46 | T_EXIT, |
||
| 47 | T_HALT_COMPILER, |
||
| 48 | |||
| 49 | // Loops |
||
| 50 | T_FOR, |
||
| 51 | T_FOREACH, |
||
| 52 | T_WHILE, |
||
| 53 | T_DO |
||
| 54 | ]; |
||
| 55 | |||
| 56 | foreach($toCheck as $type) |
||
| 57 | { |
||
| 58 | if($token->is($type)) |
||
| 59 | { |
||
| 60 | $result[] = $token; |
||
| 61 | continue 2; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | // Looks not looks like a function then skip |
||
| 66 | if (!$token->valIs('(')) |
||
| 67 | { |
||
| 68 | continue; |
||
| 69 | } |
||
| 70 | |||
| 71 | // Get actual token rather than `(` |
||
| 72 | $token = $token->prev(); |
||
| 73 | |||
| 74 | // If not string or variable, might be control operator, skip |
||
| 75 | // Variable might be used as variable function name, ie $name() |
||
| 76 | // This could be also `]` when it's array member |
||
| 77 | if ($token->not(T_STRING) && $token->not(T_VARIABLE) && !$token->valIs(']')) |
||
| 78 | { |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | // Get token before function name |
||
| 83 | $prev = $token->prev(); |
||
| 84 | |||
| 85 | // Method call |
||
| 86 | if($prev->is(T_OBJECT_OPERATOR)) |
||
| 87 | { |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Static method call |
||
| 92 | if($prev->is(T_DOUBLE_COLON)) |
||
| 93 | { |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | |||
| 97 | // Check if is simple function |
||
| 98 | // By checking what's before name |
||
| 99 | // Exclude array members calls here, as it is special case |
||
| 100 | if ( |
||
| 101 | $prev->not(T_DOUBLE_COLON) && |
||
| 102 | $prev->not(T_NEW) && |
||
| 103 | $prev->not(T_OBJECT_OPERATOR) && |
||
| 104 | !$token->valIs(']') |
||
| 105 | ) |
||
| 106 | { |
||
| 107 | $result[] = $token; |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Check for array member call |
||
| 112 | if ($token->valIs(']')) |
||
| 113 | { |
||
| 114 | $arr = $token; |
||
| 115 | |||
| 116 | $isClosed = false; |
||
| 117 | |||
| 118 | $name = []; |
||
| 119 | // Search back to find variable name of this array |
||
| 120 | // And whole expression value |
||
| 121 | // Need to check for `[$variable]` too |
||
| 122 | // |
||
| 123 | // If could not find variable, bogus code? Break loop. |
||
| 124 | // This would be code like only `]()` |
||
| 125 | while ($arr->not(TokenInterface::TypeEmpty)) |
||
| 126 | { |
||
| 127 | $name[] = $arr->value; |
||
| 128 | if ($arr->valIs(']')) |
||
| 129 | { |
||
| 130 | $isClosed = false; |
||
| 131 | } |
||
| 132 | if ($arr->valIs('[')) |
||
| 133 | { |
||
| 134 | $isClosed = true; |
||
| 135 | } |
||
| 136 | $arr = $arr->prev(); |
||
| 137 | if ($arr->is(T_VARIABLE) && $isClosed) |
||
| 138 | { |
||
| 139 | $name[] = $arr->value; |
||
| 140 | break; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | $tmp = implode('', array_reverse($name)); |
||
| 144 | $result[] = new FunctionCall($tmp, $tokens, $index); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | return $result; |
||
| 148 | } |
||
| 150 | } |