| Total Complexity | 41 |
| Total Lines | 263 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DirectiveGrammar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DirectiveGrammar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | final class DirectiveGrammar implements \IteratorAggregate |
||
| 22 | { |
||
| 23 | use TokenTrait; |
||
|
|
|||
| 24 | |||
| 25 | // start directive |
||
| 26 | public const DIRECTIVE_CHAR = '@'; |
||
| 27 | |||
| 28 | // whitespace |
||
| 29 | private const REGEXP_WHITESPACE = '/\s/'; |
||
| 30 | |||
| 31 | // Allowed keyword characters. |
||
| 32 | private const REGEXP_KEYWORD = '/[a-z0-9_\-:\.]/ui'; |
||
| 33 | |||
| 34 | /** @var array */ |
||
| 35 | private $name = []; |
||
| 36 | |||
| 37 | /** @var array */ |
||
| 38 | private $body = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param Buffer $src |
||
| 42 | * @param int $offset |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | public function parse(Buffer $src, int $offset): bool |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Directive tokens. |
||
| 103 | * |
||
| 104 | * @return \Generator|\Traversable |
||
| 105 | */ |
||
| 106 | public function getIterator() |
||
| 107 | { |
||
| 108 | if ($this->tokens === []) { |
||
| 109 | throw new \LogicException('Directive not parsed'); |
||
| 110 | } |
||
| 111 | |||
| 112 | yield from $this->tokens; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Return offset after last directive token. |
||
| 117 | * |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | public function getLastOffset(): int |
||
| 121 | { |
||
| 122 | return $this->getLastToken()->offset + strlen($this->getLastToken()->content) - 1; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get directive keyword. |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getKeyword(): string |
||
| 131 | { |
||
| 132 | foreach ($this->tokens as $token) { |
||
| 133 | if ($token->type === DynamicGrammar::TYPE_KEYWORD) { |
||
| 134 | return $token->content; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | throw new SyntaxException('Directive not parsed', $this->tokens[0]); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get directive body. |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getBody(): ?string |
||
| 147 | { |
||
| 148 | foreach ($this->tokens as $token) { |
||
| 149 | if ($token->type === DynamicGrammar::TYPE_BODY) { |
||
| 150 | return $token->content; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return null; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Pack keyword token. |
||
| 159 | */ |
||
| 160 | private function flushName(): void |
||
| 161 | { |
||
| 162 | if ($this->name === []) { |
||
| 163 | return; |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->tokens[] = $this->packToken($this->name, DynamicGrammar::TYPE_KEYWORD); |
||
| 167 | $this->name = []; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param Buffer $src |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | private function parseBody(Buffer $src) |
||
| 175 | { |
||
| 176 | $this->body = []; |
||
| 177 | $level = 1; |
||
| 178 | |||
| 179 | while ($nn = $src->next()) { |
||
| 180 | if (!$nn instanceof Byte) { |
||
| 181 | $this->flushBody(); |
||
| 182 | return $this->finalize(); |
||
| 183 | } |
||
| 184 | |||
| 185 | if (in_array($nn->char, ['"', '"'])) { |
||
| 186 | $this->body[] = $nn; |
||
| 187 | while ($nnn = $src->next()) { |
||
| 188 | $this->body[] = $nnn; |
||
| 189 | if ($nnn instanceof Byte && $nnn->char === $nn->char) { |
||
| 190 | break; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | continue; |
||
| 194 | } |
||
| 195 | |||
| 196 | $this->body[] = $nn; |
||
| 197 | |||
| 198 | if ($nn->char === '(') { |
||
| 199 | $level++; |
||
| 200 | continue; |
||
| 201 | } |
||
| 202 | |||
| 203 | if ($nn->char === ')') { |
||
| 204 | $level--; |
||
| 205 | |||
| 206 | if ($level === 0) { |
||
| 207 | $n = array_pop($this->body); |
||
| 208 | |||
| 209 | $this->flushBody(); |
||
| 210 | $this->tokens[] = new Token(DynamicGrammar::TYPE_BODY_CLOSE, $n->offset, $n->char); |
||
| 211 | |||
| 212 | return $this->finalize(); |
||
| 213 | } |
||
| 214 | |||
| 215 | continue; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | return $this->finalize(); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Pack name token. |
||
| 224 | */ |
||
| 225 | private function flushBody(): void |
||
| 226 | { |
||
| 227 | if ($this->body === []) { |
||
| 228 | return; |
||
| 229 | } |
||
| 230 | |||
| 231 | $this->tokens[] = $this->packToken($this->body, DynamicGrammar::TYPE_BODY); |
||
| 232 | $this->body = []; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return Token |
||
| 237 | */ |
||
| 238 | private function getLastToken(): Token |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Flush directive and seek buffer before last non WHITESPACE token. |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | private function finalize(): bool |
||
| 284 | } |
||
| 285 | } |
||
| 286 |