Total Complexity | 41 |
Total Lines | 423 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like PreParser 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 PreParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | class PreParser |
||
57 | { |
||
58 | public const ERROR_CONTENT_ID_NOT_FOUND = 101401; |
||
59 | |||
60 | private string $subject; |
||
61 | private Mailcode_Collection $collection; |
||
62 | private static int $contentCounter = 0; |
||
63 | private bool $parsed = false; |
||
64 | |||
65 | /** |
||
66 | * @var CommandDef[] |
||
67 | */ |
||
68 | private array $commands = array(); |
||
69 | |||
70 | /** |
||
71 | * @var array<int,string> |
||
72 | */ |
||
73 | private static array $contents = array(); |
||
74 | |||
75 | private Debugger $debugger; |
||
76 | |||
77 | public function __construct(string $subject, Mailcode_Collection $collection) |
||
78 | { |
||
79 | $this->subject = $subject; |
||
80 | $this->collection = $collection; |
||
81 | $this->debugger = new Debugger(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return Mailcode_Collection |
||
86 | */ |
||
87 | public function getCollection() : Mailcode_Collection |
||
88 | { |
||
89 | return $this->collection; |
||
90 | } |
||
91 | |||
92 | public function isValid() : bool |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Resets the stored contents and content counter. |
||
99 | * |
||
100 | * NOTE: Used primarily for the unit tests. |
||
101 | */ |
||
102 | public static function reset() : void |
||
103 | { |
||
104 | self::$contentCounter = 0; |
||
105 | self::$contents = array(); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Fetches the content string stored under the specified ID. |
||
110 | * |
||
111 | * @param int $id |
||
112 | * @return string |
||
113 | * |
||
114 | * @throws Mailcode_Parser_Exception |
||
115 | * @see PreParser::ERROR_CONTENT_ID_NOT_FOUND |
||
116 | */ |
||
117 | public static function getContent(int $id) : string |
||
118 | { |
||
119 | if(isset(self::$contents[$id])) |
||
120 | { |
||
121 | return self::$contents[$id]; |
||
122 | } |
||
123 | |||
124 | throw new Mailcode_Parser_Exception( |
||
125 | 'Command content not found', |
||
126 | sprintf( |
||
127 | 'The content stored under ID [%s] does not exist.', |
||
128 | $id |
||
129 | ), |
||
130 | self::ERROR_CONTENT_ID_NOT_FOUND |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Removes the content string stored under the specified ID, if it exists. |
||
136 | * |
||
137 | * @param int $id |
||
138 | * @return void |
||
139 | */ |
||
140 | public static function clearContent(int $id) : void |
||
141 | { |
||
142 | if(isset(self::$contents[$id])) |
||
143 | { |
||
144 | unset(self::$contents[$id]); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | public static function getContentCounter() : int |
||
149 | { |
||
150 | return self::$contentCounter; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return $this |
||
155 | * @throws Mailcode_Exception |
||
156 | */ |
||
157 | public function parse() : self |
||
158 | { |
||
159 | if($this->parsed) |
||
160 | { |
||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | $this->parsed = true; |
||
165 | $this->subject = self::safeguardBrackets($this->subject); |
||
166 | |||
167 | $this->detectCommands(); |
||
168 | |||
169 | foreach($this->commands as $commandDef) |
||
170 | { |
||
171 | $this->processCommand($commandDef); |
||
172 | } |
||
173 | |||
174 | $this->validateCommandContents(); |
||
175 | |||
176 | $this->subject = self::restoreBrackets($this->subject); |
||
|
|||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return string[] |
||
183 | * @throws Mailcode_Exception |
||
184 | */ |
||
185 | private function getContentCommandNames() : array |
||
186 | { |
||
187 | $commands = Mailcode::create()->getCommands()->getContentCommands(); |
||
188 | $result = array(); |
||
189 | |||
190 | foreach($commands as $command) |
||
191 | { |
||
192 | $result[] = $command->getName(); |
||
193 | } |
||
194 | |||
195 | return $result; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @var array<string,string> |
||
200 | */ |
||
201 | private static array $escapeChars = array( |
||
202 | '\{' => '__BRACKET_OPEN__', |
||
203 | '\}' => '__BRACKET_CLOSE__' |
||
204 | ); |
||
205 | |||
206 | public static function safeguardBrackets(string $subject) : string |
||
207 | { |
||
208 | return str_replace( |
||
209 | array_keys(self::$escapeChars), |
||
210 | array_values(self::$escapeChars), |
||
211 | $subject |
||
212 | ); |
||
213 | } |
||
214 | |||
215 | public static function restoreBrackets(string $subject) : string |
||
216 | { |
||
217 | return str_replace( |
||
218 | array_values(self::$escapeChars), |
||
219 | array_keys(self::$escapeChars), |
||
220 | $subject |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | public static function unescapeBrackets(string $subject) : string |
||
225 | { |
||
226 | return str_replace( |
||
227 | array( |
||
228 | '\{', |
||
229 | '\}' |
||
230 | ), |
||
231 | array( |
||
232 | '{', |
||
233 | '}' |
||
234 | ), |
||
235 | $subject |
||
236 | ); |
||
237 | } |
||
238 | |||
239 | public function countCommands() : int |
||
240 | { |
||
241 | $this->parse(); |
||
242 | |||
243 | return count($this->commands); |
||
244 | } |
||
245 | |||
246 | public function getString() : string |
||
247 | { |
||
248 | return $this->subject; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return CommandDef[] |
||
253 | */ |
||
254 | public function getCommands() : array |
||
255 | { |
||
256 | $this->parse(); |
||
257 | |||
258 | return $this->commands; |
||
259 | } |
||
260 | |||
261 | private function validateCommandContents() : void |
||
262 | { |
||
263 | foreach($this->commands as $command) |
||
264 | { |
||
265 | if(!$command->validateContent($this, $this->collection)) |
||
266 | { |
||
267 | return; |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | |||
272 | private function detectCommands() : void |
||
273 | { |
||
274 | $openingCommands = $this->detectOpeningCommands(); |
||
275 | $closingCommands = $this->detectClosingCommands(); |
||
276 | |||
277 | if(!$this->validateCommandsList($openingCommands, $closingCommands)) |
||
278 | { |
||
279 | return; |
||
280 | } |
||
281 | |||
282 | foreach($openingCommands as $idx => $def) |
||
283 | { |
||
284 | $this->commands[] = new CommandDef( |
||
285 | $def['name'], |
||
286 | $def['matchedText'], |
||
287 | $def['parameters'], |
||
288 | $closingCommands[$idx]['matchedText'] |
||
289 | ); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param array<int,array{matchedText:string,name:string,parameters:string}> $openingCommands |
||
295 | * @param array<int,array{name:string,matchedText:string}> $closingCommands |
||
296 | * @return bool |
||
297 | */ |
||
298 | private function validateCommandsList(array $openingCommands, array $closingCommands) : bool |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @return array<int,array{matchedText:string,name:string,parameters:string}> |
||
340 | */ |
||
341 | private function detectOpeningCommands() : array |
||
342 | { |
||
343 | $regex = sprintf( |
||
344 | '/{\s*(%s)\s*:([^}]+)}/sixU', |
||
345 | implode('|', $this->getContentCommandNames()) |
||
346 | ); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @return array<int,array{name:string,matchedText:string}> |
||
368 | */ |
||
369 | private function detectClosingCommands() : array |
||
391 | } |
||
392 | |||
393 | private function processCommand(CommandDef $commandDef) : void |
||
394 | { |
||
395 | $commandDef->extractContent($this->subject); |
||
396 | |||
397 | $this->debugger->debugCommandDef($commandDef); |
||
398 | |||
399 | // Replace the original command and content with the replacement command |
||
400 | $this->subject = substr_replace( |
||
401 | $this->subject, |
||
402 | $commandDef->getReplacementCommand(), |
||
403 | $commandDef->getStartPos(), |
||
404 | $commandDef->getLength() |
||
405 | ); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Stores the content of the command. The command will retrieve |
||
410 | * it using {@see PreParser::getContent()} when |
||
411 | * it is created by the main parser. |
||
412 | * |
||
413 | * @param string $content |
||
414 | * @return int |
||
415 | */ |
||
416 | public static function storeContent(string $content) : int |
||
417 | { |
||
418 | self::$contentCounter++; |
||
419 | |||
420 | self::$contents[self::$contentCounter] = self::restoreBrackets($content); |
||
421 | |||
422 | return self::$contentCounter; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @param string $matchedText |
||
427 | * @return false |
||
428 | */ |
||
429 | private function addErrorClosedNeverOpened(string $matchedText) : bool |
||
430 | { |
||
431 | $this->collection->addErrorMessage( |
||
432 | $matchedText, |
||
433 | t('The closing command has no matching opening command.'), |
||
434 | Mailcode_Commands_CommonConstants::VALIDATION_MISSING_CONTENT_OPENING_TAG |
||
435 | ); |
||
436 | return false; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @param string $matchedText |
||
441 | * @param string $name |
||
442 | * @return false |
||
443 | */ |
||
444 | private function addErrorNeverClosed(string $matchedText, string $name) : bool |
||
445 | { |
||
446 | $this->collection->addErrorMessage( |
||
447 | $matchedText, |
||
448 | t( |
||
449 | 'The command is never closed with a matching %1$s command.', |
||
450 | sb()->code('{' . $name . '}') |
||
451 | ), |
||
452 | Mailcode_Commands_CommonConstants::VALIDATION_MISSING_CONTENT_CLOSING_TAG |
||
453 | ); |
||
454 | return false; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * @param string $name |
||
459 | * @param string $openingMatchedText |
||
460 | * @param string $closingMatchedText |
||
461 | * @return false |
||
462 | */ |
||
463 | private function addErrorClosingMismatch(string $name, string $openingMatchedText, string $closingMatchedText) : bool |
||
479 | } |
||
480 | } |
||
481 |