Total Complexity | 42 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like DynamicGrammar 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 DynamicGrammar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | final class DynamicGrammar implements GrammarInterface |
||
33 | { |
||
34 | use TokenTrait; |
||
|
|||
35 | |||
36 | // inject grammar tokens |
||
37 | public const TYPE_OPEN_TAG = 1; |
||
38 | public const TYPE_CLOSE_TAG = 2; |
||
39 | public const TYPE_OPEN_RAW_TAG = 3; |
||
40 | public const TYPE_CLOSE_RAW_TAG = 4; |
||
41 | public const TYPE_BODY_OPEN = 5; |
||
42 | public const TYPE_BODY_CLOSE = 6; |
||
43 | public const TYPE_BODY = 7; |
||
44 | public const TYPE_DIRECTIVE = 8; |
||
45 | public const TYPE_KEYWORD = 9; |
||
46 | public const TYPE_WHITESPACE = 10; |
||
47 | |||
48 | // grammar control directive |
||
49 | public const DECLARE_DIRECTIVE = 'declare'; |
||
50 | |||
51 | /** @var DirectiveRendererInterface */ |
||
52 | private $directiveRenderer; |
||
53 | |||
54 | /** @var BracesGrammar */ |
||
55 | private $echo; |
||
56 | |||
57 | /** @var BracesGrammar */ |
||
58 | private $raw; |
||
59 | |||
60 | /** |
||
61 | * @param DirectiveRendererInterface|null $directiveRenderer |
||
62 | */ |
||
63 | public function __construct(DirectiveRendererInterface $directiveRenderer = null) |
||
64 | { |
||
65 | $this->directiveRenderer = $directiveRenderer; |
||
66 | |||
67 | $this->echo = new BracesGrammar( |
||
68 | '{{', |
||
69 | '}}', |
||
70 | self::TYPE_OPEN_TAG, |
||
71 | self::TYPE_CLOSE_TAG |
||
72 | ); |
||
73 | |||
74 | $this->raw = new BracesGrammar( |
||
75 | '{!!', |
||
76 | '!!}', |
||
77 | self::TYPE_OPEN_RAW_TAG, |
||
78 | self::TYPE_CLOSE_RAW_TAG |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @inheritDoc |
||
84 | */ |
||
85 | public function parse(Buffer $src): \Generator |
||
86 | { |
||
87 | while ($n = $src->next()) { |
||
88 | if (!$n instanceof Byte) { |
||
89 | yield $n; |
||
90 | continue; |
||
91 | } |
||
92 | |||
93 | if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR) { |
||
94 | if ( |
||
95 | $this->echo->nextToken($src) || |
||
96 | $this->raw->nextToken($src) || |
||
97 | $src->lookaheadByte() === DirectiveGrammar::DIRECTIVE_CHAR |
||
98 | ) { |
||
99 | // escaped echo sequence, hide directive byte |
||
100 | yield $src->next(); |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | $directive = new DirectiveGrammar(); |
||
105 | if ($directive->parse($src, $n->offset)) { |
||
106 | if (strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE) { |
||
107 | // configure braces syntax |
||
108 | $this->declare($directive->getBody()); |
||
109 | } else { |
||
110 | if ( |
||
111 | $this->directiveRenderer !== null |
||
112 | && !$this->directiveRenderer->hasDirective($directive->getKeyword()) |
||
113 | ) { |
||
114 | // directive opening char |
||
115 | yield $n; |
||
116 | |||
117 | // unknown directive, treat as plain test |
||
118 | $src->replay($n->offset); |
||
119 | continue; |
||
120 | } |
||
121 | |||
122 | yield from $directive; |
||
123 | } |
||
124 | |||
125 | $src->replay($directive->getLastOffset()); |
||
126 | continue; |
||
127 | } |
||
128 | |||
129 | $src->replay($n->offset); |
||
130 | } |
||
131 | |||
132 | /** @var BracesGrammar|null $braces */ |
||
133 | $braces = null; |
||
134 | if ($this->echo->starts($src, $n)) { |
||
135 | $braces = clone $this->echo; |
||
136 | } elseif ($this->raw->starts($src, $n)) { |
||
137 | $braces = clone $this->raw; |
||
138 | } |
||
139 | |||
140 | if ($braces !== null) { |
||
141 | $echo = $braces->parse($src, $n); |
||
142 | if ($echo !== null) { |
||
143 | yield from $echo; |
||
144 | continue; |
||
145 | } |
||
146 | |||
147 | $src->replay($n->offset); |
||
148 | } |
||
149 | |||
150 | yield $n; |
||
151 | } |
||
152 | |||
153 | yield from $src; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @codeCoverageIgnore |
||
158 | * @inheritDoc |
||
159 | */ |
||
160 | public static function tokenName(int $token): string |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param string $body |
||
186 | */ |
||
187 | private function declare(?string $body): void |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param string $body |
||
229 | * @return array |
||
230 | */ |
||
231 | private function fetchOptions(string $body): array |
||
270 | } |
||
271 | } |
||
272 |