| Conditions | 11 |
| Paths | 9 |
| Total Lines | 73 |
| Code Lines | 30 |
| 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 |
||
| 25 | public function collect($tokens) |
||
| 26 | { |
||
| 27 | $result = []; |
||
| 28 | foreach ($tokens as $index => $token) |
||
| 29 | { |
||
| 30 | // Skip all except object operator |
||
| 31 | if($token->not(T_OBJECT_OPERATOR)) |
||
| 32 | { |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | $prev = $token->prev(); |
||
| 36 | $next = $token->next(); |
||
| 37 | |||
| 38 | // Should start with variable OR closing parenthesis |
||
| 39 | // eg: |
||
| 40 | // $object->call() |
||
| 41 | // (new Object)->call() |
||
| 42 | // $objects[1]->call() |
||
| 43 | if($prev->not(T_VARIABLE) xor $prev->value !== ')' xor $prev->value !== ']') |
||
| 44 | { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Should have method name |
||
| 49 | if($next->not(T_STRING)) |
||
| 50 | { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | $finishing = $next->next(); |
||
| 55 | |||
| 56 | // Should have opening bracket after method name |
||
| 57 | if($finishing->value !== '(') |
||
| 58 | { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | |||
| 62 | // TODO Resolve use statements |
||
| 63 | $className = []; |
||
| 64 | $className[] = $prev->value; |
||
| 65 | |||
| 66 | // Case of (new Type)->method() call |
||
| 67 | $this->resolve($className, $prev, '(', ')'); |
||
| 68 | |||
| 69 | // Case of [Something]->call() |
||
| 70 | $this->resolve($className, $prev, '[', ']'); |
||
| 71 | |||
| 72 | // Method call result, eg: |
||
| 73 | // myFunc()->method() |
||
| 74 | // This does not (yet?) support nested calls like: |
||
| 75 | // EG.: Yii::app()->getClientScript()->registerScript() |
||
| 76 | if(implode('', $className) === '()') |
||
| 77 | { |
||
| 78 | $function = $prev->prev()->prev(); |
||
| 79 | array_unshift($className, $function->value); |
||
| 80 | $calledFrom = $function->prev(); |
||
| 81 | if($calledFrom->is(T_DOUBLE_COLON) || $calledFrom->is(T_OBJECT_OPERATOR)) |
||
| 82 | { |
||
| 83 | array_unshift($className, $calledFrom->value); |
||
| 84 | $parent = $calledFrom->prev(); |
||
| 85 | if($parent->is(T_STRING) || $parent->is(T_VARIABLE)) |
||
| 86 | { |
||
| 87 | array_unshift($className,$parent->value); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $methodName = $next->value; |
||
| 93 | $name = sprintf('%s->%s', implode($className), $methodName); |
||
| 94 | |||
| 95 | $result[] = new StaticMethod($name,$tokens, $index); |
||
| 96 | } |
||
| 97 | return $result; |
||
| 98 | } |
||
| 134 | } |