| Conditions | 13 |
| Paths | 19 |
| Total Lines | 75 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 69 | public function parse($tokens = null) |
||
| 70 | { |
||
| 71 | if (is_string($tokens)) { |
||
| 72 | $tokens = $this->tokenize($tokens); |
||
| 73 | } elseif(!$tokens instanceof TokenIterator) { |
||
| 74 | throw new \InvalidArgumentException('$tokens must be string or TokenIterator'); |
||
| 75 | } |
||
| 76 | |||
| 77 | $start = $tokens->current(); |
||
| 78 | |||
| 79 | /** @var Token[] $result */ |
||
| 80 | $result = [$start]; |
||
| 81 | |||
| 82 | $context = []; |
||
| 83 | $all = []; |
||
| 84 | |||
| 85 | /** @var Token $token */ |
||
| 86 | for($tokens->next(); $tokens->valid(); $tokens->next()) { |
||
| 87 | $token = $tokens->current(); |
||
| 88 | |||
| 89 | if (!$token->isValid($this, $context)) { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($token->isStart()) { |
||
| 94 | if ($token instanceof LanguageToken) { |
||
| 95 | /** @noinspection PhpUndefinedMethodInspection bug */ |
||
| 96 | $result = array_merge($result, $token->getLanguage()->parse($tokens)); |
||
| 97 | } else { |
||
| 98 | $all[spl_object_hash($token)] = $result[] = $token; |
||
| 99 | $context[spl_object_hash($token)] = $token->name; |
||
| 100 | } |
||
| 101 | } else { |
||
| 102 | $start = $token->getStart(); |
||
| 103 | |||
| 104 | if ($token instanceof LanguageToken && $token->getRule()->getLanguage() === $this) { |
||
| 105 | // todo: close unclosed tokens |
||
| 106 | $result[0]->setEnd($token); |
||
| 107 | |||
| 108 | if($result[0]->getRule()->postProcess) { |
||
| 109 | $embed = $this |
||
| 110 | ->parse(substr($tokens->getSource(), $result[0]->pos, $result[0]->getLength())) |
||
| 111 | ->getTokens(); |
||
| 112 | |||
| 113 | foreach($embed as $etoken) { |
||
| 114 | $etoken->pos += $result[0]->pos; |
||
| 115 | } |
||
| 116 | |||
| 117 | $result = array_merge($result, $embed); |
||
| 118 | } |
||
| 119 | |||
| 120 | $result[] = $token; |
||
| 121 | return $result; |
||
| 122 | } else { |
||
| 123 | if ($start !== null) { |
||
| 124 | unset($context[spl_object_hash($start)]); |
||
| 125 | } else { |
||
| 126 | /** @noinspection PhpUnusedParameterInspection */ |
||
| 127 | $start = ArrayHelper::find(array_reverse($context), function ($k, $v) use ($token) { |
||
| 128 | return $v === $token->name; |
||
| 129 | }); |
||
| 130 | |||
| 131 | if ($start !== false) { |
||
| 132 | $token->setStart($all[$start]); |
||
| 133 | unset($context[$start]); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | $result[] = $token; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return new TokenIterator($result, $tokens->getSource()); |
||
| 143 | } |
||
| 144 | |||
| 219 |