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