Complex classes like TemplateParser 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 TemplateParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class TemplateParser |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * The following two constants are used for tracking whether we are currently |
||
31 | * parsing ViewHelper arguments or not. This is used to parse arrays only as |
||
32 | * ViewHelper argument. |
||
33 | */ |
||
34 | const CONTEXT_INSIDE_VIEWHELPER_ARGUMENTS = 1; |
||
35 | const CONTEXT_OUTSIDE_VIEWHELPER_ARGUMENTS = 2; |
||
36 | |||
37 | /** |
||
38 | * Whether or not the escaping interceptors are active |
||
39 | * |
||
40 | * @var boolean |
||
41 | */ |
||
42 | protected $escapingEnabled = true; |
||
43 | |||
44 | /** |
||
45 | * @var Configuration |
||
46 | */ |
||
47 | protected $configuration; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $settings; |
||
53 | |||
54 | /** |
||
55 | * @var RenderingContextInterface |
||
56 | */ |
||
57 | protected $renderingContext; |
||
58 | |||
59 | /** |
||
60 | * @var integer |
||
61 | */ |
||
62 | protected $pointerLineNumber = 1; |
||
63 | |||
64 | /** |
||
65 | * @var integer |
||
66 | */ |
||
67 | protected $pointerLineCharacter = 1; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $pointerTemplateCode = null; |
||
73 | |||
74 | /** |
||
75 | * @var ParsedTemplateInterface[] |
||
76 | */ |
||
77 | protected $parsedTemplates = []; |
||
78 | |||
79 | /** |
||
80 | * @param RenderingContextInterface $renderingContext |
||
81 | * @return void |
||
82 | */ |
||
83 | public function setRenderingContext(RenderingContextInterface $renderingContext) |
||
88 | |||
89 | /** |
||
90 | * Returns an array of current line number, character in line and reference template code; |
||
91 | * for extraction when catching parser-related Exceptions during parsing. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getCurrentParsingPointers() |
||
99 | |||
100 | /** |
||
101 | * @return boolean |
||
102 | */ |
||
103 | public function isEscapingEnabled() |
||
107 | |||
108 | /** |
||
109 | * @param boolean $escapingEnabled |
||
110 | * @return void |
||
111 | */ |
||
112 | public function setEscapingEnabled($escapingEnabled) |
||
116 | |||
117 | /** |
||
118 | * Parses a given template string and returns a parsed template object. |
||
119 | * |
||
120 | * The resulting ParsedTemplate can then be rendered by calling evaluate() on it. |
||
121 | * |
||
122 | * Normally, you should use a subclass of AbstractTemplateView instead of calling the |
||
123 | * TemplateParser directly. |
||
124 | * |
||
125 | * @param string $templateString The template to parse as a string |
||
126 | * @param string|null $templateIdentifier If the template has an identifying string it can be passed here to improve error reporting. |
||
127 | * @return ParsingState Parsed template |
||
128 | * @throws Exception |
||
129 | */ |
||
130 | public function parse($templateString, $templateIdentifier = null) |
||
148 | |||
149 | /** |
||
150 | * @param \Exception $error |
||
151 | * @param string $templateIdentifier |
||
152 | * @throws \Exception |
||
153 | */ |
||
154 | public function createParsingRelatedExceptionWithContext(\Exception $error, $templateIdentifier) |
||
172 | |||
173 | /** |
||
174 | * @param string $templateIdentifier |
||
175 | * @param \Closure $templateSourceClosure Closure which returns the template source if needed |
||
176 | * @return ParsedTemplateInterface |
||
177 | */ |
||
178 | public function getOrParseAndStoreTemplate($templateIdentifier, $templateSourceClosure) |
||
200 | |||
201 | /** |
||
202 | * @param string $templateIdentifier |
||
203 | * @param \Closure $templateSourceClosure |
||
204 | * @return ParsedTemplateInterface |
||
205 | */ |
||
206 | protected function parseTemplateSource($templateIdentifier, $templateSourceClosure) |
||
216 | |||
217 | /** |
||
218 | * Pre-process the template source, making all registered TemplateProcessors |
||
219 | * do what they need to do with the template source before it is parsed. |
||
220 | * |
||
221 | * @param string $templateSource |
||
222 | * @return string |
||
223 | */ |
||
224 | protected function preProcessTemplateSource($templateSource) |
||
231 | |||
232 | /** |
||
233 | * Resets the parser to its default values. |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | protected function reset() |
||
243 | |||
244 | /** |
||
245 | * Splits the template string on all dynamic tags found. |
||
246 | * |
||
247 | * @param string $templateString Template string to split. |
||
248 | * @return array Splitted template |
||
249 | */ |
||
250 | protected function splitTemplateAtDynamicTags($templateString) |
||
254 | |||
255 | /** |
||
256 | * Build object tree from the split template |
||
257 | * |
||
258 | * @param array $splitTemplate The split template, so that every tag with a namespace declaration is already a seperate array element. |
||
259 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
260 | * @return ParsingState |
||
261 | * @throws Exception |
||
262 | */ |
||
263 | protected function buildObjectTree(array $splitTemplate, $context) |
||
311 | /** |
||
312 | * Handles an opening or self-closing view helper tag. |
||
313 | * |
||
314 | * @param ParsingState $state Current parsing state |
||
315 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
316 | * @param string $methodIdentifier Method identifier |
||
317 | * @param string $arguments Arguments string, not yet parsed |
||
318 | * @param boolean $selfclosing true, if the tag is a self-closing tag. |
||
319 | * @param string $templateElement The template code containing the ViewHelper call |
||
320 | * @return NodeInterface|null |
||
321 | */ |
||
322 | protected function openingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $arguments, $selfclosing, $templateElement) |
||
343 | |||
344 | /** |
||
345 | * Initialize the given ViewHelper and adds it to the current node and to |
||
346 | * the stack. |
||
347 | * |
||
348 | * @param ParsingState $state Current parsing state |
||
349 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
350 | * @param string $methodIdentifier Method identifier |
||
351 | * @param array $argumentsObjectTree Arguments object tree |
||
352 | * @return null|NodeInterface An instance of ViewHelperNode if identity was valid - NULL if the namespace/identity was not registered |
||
353 | * @throws Exception |
||
354 | */ |
||
355 | protected function initializeViewHelperAndAddItToStack(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $argumentsObjectTree) |
||
391 | |||
392 | /** |
||
393 | * Handles a closing view helper tag |
||
394 | * |
||
395 | * @param ParsingState $state The current parsing state |
||
396 | * @param string $namespaceIdentifier Namespace identifier for the closing tag. |
||
397 | * @param string $methodIdentifier Method identifier. |
||
398 | * @return boolean whether the viewHelper was found and added to the stack or not |
||
399 | * @throws Exception |
||
400 | */ |
||
401 | protected function closingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier) |
||
428 | |||
429 | /** |
||
430 | * Handles the appearance of an object accessor (like {posts.author.email}). |
||
431 | * Creates a new instance of \TYPO3Fluid\Fluid\ObjectAccessorNode. |
||
432 | * |
||
433 | * Handles ViewHelpers as well which are in the shorthand syntax. |
||
434 | * |
||
435 | * @param ParsingState $state The current parsing state |
||
436 | * @param string $objectAccessorString String which identifies which objects to fetch |
||
437 | * @param string $delimiter |
||
438 | * @param string $viewHelperString |
||
439 | * @param string $additionalViewHelpersString |
||
440 | * @return void |
||
441 | */ |
||
442 | protected function objectAccessorHandler(ParsingState $state, $objectAccessorString, $delimiter, $viewHelperString, $additionalViewHelpersString) |
||
485 | |||
486 | /** |
||
487 | * Call all interceptors registered for a given interception point. |
||
488 | * |
||
489 | * @param NodeInterface $node The syntax tree node which can be modified by the interceptors. |
||
490 | * @param integer $interceptionPoint the interception point. One of the \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_* constants. |
||
491 | * @param ParsingState $state the parsing state |
||
492 | * @return void |
||
493 | */ |
||
494 | protected function callInterceptor(NodeInterface & $node, $interceptionPoint, ParsingState $state) |
||
511 | |||
512 | /** |
||
513 | * Parse arguments of a given tag, and build up the Arguments Object Tree |
||
514 | * for each argument. |
||
515 | * Returns an associative array, where the key is the name of the argument, |
||
516 | * and the value is a single Argument Object Tree. |
||
517 | * |
||
518 | * @param string $argumentsString All arguments as string |
||
519 | * @return array An associative array of objects, where the key is the argument name. |
||
520 | */ |
||
521 | protected function parseArguments($argumentsString) |
||
537 | |||
538 | /** |
||
539 | * Build up an argument object tree for the string in $argumentString. |
||
540 | * This builds up the tree for a single argument value. |
||
541 | * |
||
542 | * This method also does some performance optimizations, so in case |
||
543 | * no { or < is found, then we just return a TextNode. |
||
544 | * |
||
545 | * @param string $argumentString |
||
546 | * @return SyntaxTree\NodeInterface the corresponding argument object tree. |
||
547 | */ |
||
548 | protected function buildArgumentObjectTree($argumentString) |
||
560 | |||
561 | /** |
||
562 | * Removes escapings from a given argument string and trims the outermost |
||
563 | * quotes. |
||
564 | * |
||
565 | * This method is meant as a helper for regular expression results. |
||
566 | * |
||
567 | * @param string $quotedValue Value to unquote |
||
568 | * @return string Unquoted value |
||
569 | */ |
||
570 | public function unquoteString($quotedValue) |
||
583 | |||
584 | /** |
||
585 | * Handler for everything which is not a ViewHelperNode. |
||
586 | * |
||
587 | * This includes Text, array syntax, and object accessor syntax. |
||
588 | * |
||
589 | * @param ParsingState $state Current parsing state |
||
590 | * @param string $text Text to process |
||
591 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
592 | * @return void |
||
593 | */ |
||
594 | protected function textAndShorthandSyntaxHandler(ParsingState $state, $text, $context) |
||
665 | |||
666 | /** |
||
667 | * Handler for array syntax. This creates the array object recursively and |
||
668 | * adds it to the current node. |
||
669 | * |
||
670 | * @param ParsingState $state The current parsing state |
||
671 | * @param NodeInterface[] $arrayText The array as string. |
||
672 | * @return void |
||
673 | */ |
||
674 | protected function arrayHandler(ParsingState $state, $arrayText) |
||
679 | |||
680 | /** |
||
681 | * Recursive function which takes the string representation of an array and |
||
682 | * builds an object tree from it. |
||
683 | * |
||
684 | * Deals with the following value types: |
||
685 | * - Numbers (Integers and Floats) |
||
686 | * - Strings |
||
687 | * - Variables |
||
688 | * - sub-arrays |
||
689 | * |
||
690 | * @param string $arrayText Array text |
||
691 | * @return NodeInterface[] the array node built up |
||
692 | * @throws Exception |
||
693 | */ |
||
694 | protected function recursiveArrayHandler($arrayText) |
||
695 | { |
||
696 | $matches = []; |
||
697 | $arrayToBuild = []; |
||
698 | if (preg_match_all(Patterns::$SPLIT_PATTERN_SHORTHANDSYNTAX_ARRAY_PARTS, $arrayText, $matches, PREG_SET_ORDER)) { |
||
699 | foreach ($matches as $singleMatch) { |
||
700 | $arrayKey = $this->unquoteString($singleMatch['Key']); |
||
701 | if (array_key_exists('Subarray', $singleMatch) && !empty($singleMatch['Subarray']) && is_array($singleMatch['Subarray'])){ |
||
702 | $arrayToBuild[$arrayKey] = new ArrayNode($this->recursiveArrayHandler($singleMatch['Subarray'])); |
||
703 | } elseif (!empty($singleMatch['VariableIdentifier'])) { |
||
704 | $arrayToBuild[$arrayKey] = new ObjectAccessorNode($singleMatch['VariableIdentifier']); |
||
705 | } elseif (array_key_exists('Number', $singleMatch) && (!empty($singleMatch['Number']) || $singleMatch['Number'] === '0')) { |
||
706 | // Note: this method of casting picks "int" when value is a natural number and "float" if any decimals are found. See also NumericNode. |
||
707 | $arrayToBuild[$arrayKey] = $singleMatch['Number'] + 0; |
||
708 | } elseif ((array_key_exists('QuotedString', $singleMatch) && !empty($singleMatch['QuotedString']))) { |
||
709 | $argumentString = $this->unquoteString($singleMatch['QuotedString']); |
||
710 | $arrayToBuild[$arrayKey] = $this->buildArgumentObjectTree($argumentString); |
||
711 | } |
||
712 | } |
||
713 | } |
||
714 | return $arrayToBuild; |
||
715 | } |
||
716 | |||
717 | /** |
||
718 | * Text node handler |
||
719 | * |
||
720 | * @param ParsingState $state |
||
721 | * @param string $text |
||
722 | * @return void |
||
723 | */ |
||
724 | protected function textHandler(ParsingState $state, $text) |
||
730 | |||
731 | /** |
||
732 | * @return ParsingState |
||
733 | */ |
||
734 | protected function getParsingState() |
||
744 | } |
||
745 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.