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