| Total Complexity | 42 |
| Total Lines | 213 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like InlineGrammar 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 InlineGrammar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | final class InlineGrammar implements GrammarInterface |
||
| 24 | { |
||
| 25 | use TokenTrait; |
||
|
|
|||
| 26 | |||
| 27 | // inject grammar tokens |
||
| 28 | public const TYPE_OPEN_TAG = 1; |
||
| 29 | public const TYPE_CLOSE_TAG = 2; |
||
| 30 | public const TYPE_NAME = 3; |
||
| 31 | public const TYPE_SEPARATOR = 4; |
||
| 32 | public const TYPE_DEFAULT = 5; |
||
| 33 | |||
| 34 | // whitespace |
||
| 35 | private const REGEXP_WHITESPACE = '/\s/'; |
||
| 36 | |||
| 37 | // Allowed keyword characters. |
||
| 38 | private const REGEXP_KEYWORD = '/[a-z0-9_\-:\.]/ui'; |
||
| 39 | |||
| 40 | /** @var Byte[] */ |
||
| 41 | private $name = []; |
||
| 42 | |||
| 43 | /** @var Byte[]|null */ |
||
| 44 | private $default = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @inheritDoc |
||
| 48 | */ |
||
| 49 | public function parse(Buffer $src): \Generator |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @codeCoverageIgnore |
||
| 70 | * @inheritDoc |
||
| 71 | */ |
||
| 72 | public static function tokenName(int $token): string |
||
| 73 | { |
||
| 74 | switch ($token) { |
||
| 75 | case self::TYPE_OPEN_TAG: |
||
| 76 | return 'INLINE:OPEN_TAG'; |
||
| 77 | case self::TYPE_CLOSE_TAG: |
||
| 78 | return 'INLINE:CLOSE_TAG'; |
||
| 79 | case self::TYPE_NAME: |
||
| 80 | return 'INLINE:NAME'; |
||
| 81 | case self::TYPE_SEPARATOR: |
||
| 82 | return 'INLINE:SEPARATOR'; |
||
| 83 | case self::TYPE_DEFAULT: |
||
| 84 | return 'INLINE:DEFAULT'; |
||
| 85 | default: |
||
| 86 | return 'INLINE:UNDEFINED'; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param Buffer $src |
||
| 92 | * @param int $offset |
||
| 93 | * @return Token[]|null |
||
| 94 | */ |
||
| 95 | private function parseGrammar(Buffer $src, int $offset): ?array |
||
| 96 | { |
||
| 97 | $this->tokens = [ |
||
| 98 | new Token(self::TYPE_OPEN_TAG, $offset, '$' . $src->next()->char) |
||
| 99 | ]; |
||
| 100 | |||
| 101 | while ($n = $src->next()) { |
||
| 102 | if (!$n instanceof Byte) { |
||
| 103 | // no other grammars are allowed |
||
| 104 | return null; |
||
| 105 | } |
||
| 106 | |||
| 107 | switch ($n->char) { |
||
| 108 | case '"': |
||
| 109 | case "'": |
||
| 110 | if ($this->default === null) { |
||
| 111 | // " and ' not allowed in names |
||
| 112 | return null; |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->default[] = $n; |
||
| 116 | while ($nn = $src->next()) { |
||
| 117 | $this->default[] = $nn; |
||
| 118 | if ($nn instanceof Byte && $nn->char === $n->char) { |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | break; |
||
| 123 | |||
| 124 | case '}': |
||
| 125 | $this->flushName(); |
||
| 126 | $this->flushDefault(); |
||
| 127 | |||
| 128 | $this->tokens[] = new Token( |
||
| 129 | self::TYPE_CLOSE_TAG, |
||
| 130 | $n->offset, |
||
| 131 | $n->char |
||
| 132 | ); |
||
| 133 | |||
| 134 | break 2; |
||
| 135 | |||
| 136 | case '|': |
||
| 137 | $this->flushName(); |
||
| 138 | $this->flushDefault(); |
||
| 139 | |||
| 140 | $this->tokens[] = new Token( |
||
| 141 | self::TYPE_SEPARATOR, |
||
| 142 | $n->offset, |
||
| 143 | $n->char |
||
| 144 | ); |
||
| 145 | |||
| 146 | $this->default = []; |
||
| 147 | |||
| 148 | break; |
||
| 149 | |||
| 150 | default: |
||
| 151 | if ($this->default !== null) { |
||
| 152 | // default allows spaces |
||
| 153 | $this->default[] = $n; |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | |||
| 157 | if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
||
| 158 | break; |
||
| 159 | } |
||
| 160 | |||
| 161 | if (preg_match(self::REGEXP_KEYWORD, $n->char)) { |
||
| 162 | $this->name[] = $n; |
||
| 163 | break; |
||
| 164 | } |
||
| 165 | |||
| 166 | return null; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if (!$this->isValid()) { |
||
| 171 | return null; |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this->tokens; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | private function isValid(): bool |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Pack name token. |
||
| 214 | */ |
||
| 215 | private function flushName(): void |
||
| 216 | { |
||
| 217 | if ($this->name === []) { |
||
| 218 | return; |
||
| 219 | } |
||
| 220 | |||
| 221 | $this->tokens[] = $this->packToken($this->name, self::TYPE_NAME); |
||
| 222 | $this->name = []; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Pack default token. |
||
| 227 | */ |
||
| 228 | private function flushDefault(): void |
||
| 236 | } |
||
| 237 | } |
||
| 238 |