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 |
||
| 23 | class TemplateParser |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The following two constants are used for tracking whether we are currently |
||
| 28 | * parsing ViewHelper arguments or not. This is used to parse arrays only as |
||
| 29 | * ViewHelper argument. |
||
| 30 | */ |
||
| 31 | const CONTEXT_INSIDE_VIEWHELPER_ARGUMENTS = 1; |
||
| 32 | const CONTEXT_OUTSIDE_VIEWHELPER_ARGUMENTS = 2; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Whether or not the escaping interceptors are active |
||
| 36 | * |
||
| 37 | * @var boolean |
||
| 38 | */ |
||
| 39 | protected $escapingEnabled = true; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Configuration |
||
| 43 | */ |
||
| 44 | protected $configuration; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $settings; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var RenderingContextInterface |
||
| 53 | */ |
||
| 54 | protected $renderingContext; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var integer |
||
| 58 | */ |
||
| 59 | protected $pointerLineNumber = 1; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var integer |
||
| 63 | */ |
||
| 64 | protected $pointerLineCharacter = 1; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $pointerTemplateCode = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var ParsedTemplateInterface[] |
||
| 73 | */ |
||
| 74 | protected $parsedTemplates = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param RenderingContextInterface $renderingContext |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function setRenderingContext(RenderingContextInterface $renderingContext) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns an array of current line number, character in line and reference template code; |
||
| 88 | * for extraction when catching parser-related Exceptions during parsing. |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function getCurrentParsingPointers() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return boolean |
||
| 99 | */ |
||
| 100 | public function isEscapingEnabled() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param boolean $escapingEnabled |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | public function setEscapingEnabled($escapingEnabled) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Parses a given template string and returns a parsed template object. |
||
| 116 | * |
||
| 117 | * The resulting ParsedTemplate can then be rendered by calling evaluate() on it. |
||
| 118 | * |
||
| 119 | * Normally, you should use a subclass of AbstractTemplateView instead of calling the |
||
| 120 | * TemplateParser directly. |
||
| 121 | * |
||
| 122 | * @param string $templateString The template to parse as a string |
||
| 123 | * @param string|null $templateIdentifier If the template has an identifying string it can be passed here to improve error reporting. |
||
| 124 | * @return ParsingState Parsed template |
||
| 125 | * @throws Exception |
||
| 126 | */ |
||
| 127 | public function parse($templateString, $templateIdentifier = null) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param \Exception $error |
||
| 149 | * @param string $templateIdentifier |
||
| 150 | * @throws \Exception |
||
| 151 | */ |
||
| 152 | public function createParsingRelatedExceptionWithContext(\Exception $error, $templateIdentifier) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $templateIdentifier |
||
| 173 | * @param \Closure $templateSourceClosure Closure which returns the template source if needed |
||
| 174 | * @return ParsedTemplateInterface |
||
| 175 | */ |
||
| 176 | public function getOrParseAndStoreTemplate($templateIdentifier, $templateSourceClosure) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Pre-process the template source, making all registered TemplateProcessors |
||
| 204 | * do what they need to do with the template source before it is parsed. |
||
| 205 | * |
||
| 206 | * @param string $templateSource |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | protected function preProcessTemplateSource($templateSource) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Resets the parser to its default values. |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | protected function reset() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Extracts escaping modifiers ({escapingEnabled=true/false}) out of the given template and sets $this->escapingEnabled accordingly |
||
| 231 | * |
||
| 232 | * @param string $templateString Template string to extract the {escaping = ..} definitions from |
||
| 233 | * @return string The updated template string without escaping declarations inside |
||
| 234 | * @throws Exception if there is more than one modifier |
||
| 235 | */ |
||
| 236 | protected function extractEscapingModifier($templateString) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Splits the template string on all dynamic tags found. |
||
| 256 | * |
||
| 257 | * @param string $templateString Template string to split. |
||
| 258 | * @return array Splitted template |
||
| 259 | */ |
||
| 260 | protected function splitTemplateAtDynamicTags($templateString) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Build object tree from the split template |
||
| 267 | * |
||
| 268 | * @param array $splitTemplate The split template, so that every tag with a namespace declaration is already a seperate array element. |
||
| 269 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
| 270 | * @return ParsingState |
||
| 271 | * @throws Exception |
||
| 272 | */ |
||
| 273 | protected function buildObjectTree(array $splitTemplate, $context) |
||
| 318 | /** |
||
| 319 | * Handles an opening or self-closing view helper tag. |
||
| 320 | * |
||
| 321 | * @param ParsingState $state Current parsing state |
||
| 322 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
| 323 | * @param string $methodIdentifier Method identifier |
||
| 324 | * @param string $arguments Arguments string, not yet parsed |
||
| 325 | * @param boolean $selfclosing true, if the tag is a self-closing tag. |
||
| 326 | * @param string $templateElement The template code containing the ViewHelper call |
||
| 327 | * @return NodeInterface|null |
||
| 328 | */ |
||
| 329 | protected function openingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $arguments, $selfclosing, $templateElement) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Initialize the given ViewHelper and adds it to the current node and to |
||
| 353 | * the stack. |
||
| 354 | * |
||
| 355 | * @param ParsingState $state Current parsing state |
||
| 356 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
| 357 | * @param string $methodIdentifier Method identifier |
||
| 358 | * @param array $argumentsObjectTree Arguments object tree |
||
| 359 | * @return null|NodeInterface An instance of ViewHelperNode if identity was valid - NULL if the namespace/identity was not registered |
||
| 360 | * @throws Exception |
||
| 361 | */ |
||
| 362 | protected function initializeViewHelperAndAddItToStack(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $argumentsObjectTree) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Handles a closing view helper tag |
||
| 386 | * |
||
| 387 | * @param ParsingState $state The current parsing state |
||
| 388 | * @param string $namespaceIdentifier Namespace identifier for the closing tag. |
||
| 389 | * @param string $methodIdentifier Method identifier. |
||
| 390 | * @return boolean whether the viewHelper was found and added to the stack or not |
||
| 391 | * @throws Exception |
||
| 392 | */ |
||
| 393 | protected function closingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Handles the appearance of an object accessor (like {posts.author.email}). |
||
| 420 | * Creates a new instance of \TYPO3Fluid\Fluid\ObjectAccessorNode. |
||
| 421 | * |
||
| 422 | * Handles ViewHelpers as well which are in the shorthand syntax. |
||
| 423 | * |
||
| 424 | * @param ParsingState $state The current parsing state |
||
| 425 | * @param string $objectAccessorString String which identifies which objects to fetch |
||
| 426 | * @param string $delimiter |
||
| 427 | * @param string $viewHelperString |
||
| 428 | * @param string $additionalViewHelpersString |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | protected function objectAccessorHandler(ParsingState $state, $objectAccessorString, $delimiter, $viewHelperString, $additionalViewHelpersString) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Call all interceptors registered for a given interception point. |
||
| 477 | * |
||
| 478 | * @param NodeInterface $node The syntax tree node which can be modified by the interceptors. |
||
| 479 | * @param integer $interceptionPoint the interception point. One of the \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_* constants. |
||
| 480 | * @param ParsingState $state the parsing state |
||
| 481 | * @return void |
||
| 482 | */ |
||
| 483 | protected function callInterceptor(NodeInterface & $node, $interceptionPoint, ParsingState $state) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Parse arguments of a given tag, and build up the Arguments Object Tree |
||
| 503 | * for each argument. |
||
| 504 | * Returns an associative array, where the key is the name of the argument, |
||
| 505 | * and the value is a single Argument Object Tree. |
||
| 506 | * |
||
| 507 | * @param string $argumentsString All arguments as string |
||
| 508 | * @return array An associative array of objects, where the key is the argument name. |
||
| 509 | */ |
||
| 510 | protected function parseArguments($argumentsString) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Build up an argument object tree for the string in $argumentString. |
||
| 529 | * This builds up the tree for a single argument value. |
||
| 530 | * |
||
| 531 | * This method also does some performance optimizations, so in case |
||
| 532 | * no { or < is found, then we just return a TextNode. |
||
| 533 | * |
||
| 534 | * @param string $argumentString |
||
| 535 | * @return SyntaxTree\NodeInterface the corresponding argument object tree. |
||
| 536 | */ |
||
| 537 | protected function buildArgumentObjectTree($argumentString) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Removes escapings from a given argument string and trims the outermost |
||
| 552 | * quotes. |
||
| 553 | * |
||
| 554 | * This method is meant as a helper for regular expression results. |
||
| 555 | * |
||
| 556 | * @param string $quotedValue Value to unquote |
||
| 557 | * @return string Unquoted value |
||
| 558 | */ |
||
| 559 | public function unquoteString($quotedValue) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Handler for everything which is not a ViewHelperNode. |
||
| 572 | * |
||
| 573 | * This includes Text, array syntax, and object accessor syntax. |
||
| 574 | * |
||
| 575 | * @param ParsingState $state Current parsing state |
||
| 576 | * @param string $text Text to process |
||
| 577 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
| 578 | * @return void |
||
| 579 | */ |
||
| 580 | protected function textAndShorthandSyntaxHandler(ParsingState $state, $text, $context) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Handler for array syntax. This creates the array object recursively and |
||
| 637 | * adds it to the current node. |
||
| 638 | * |
||
| 639 | * @param ParsingState $state The current parsing state |
||
| 640 | * @param NodeInterface[] $arrayText The array as string. |
||
| 641 | * @return void |
||
| 642 | */ |
||
| 643 | protected function arrayHandler(ParsingState $state, $arrayText) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Recursive function which takes the string representation of an array and |
||
| 651 | * builds an object tree from it. |
||
| 652 | * |
||
| 653 | * Deals with the following value types: |
||
| 654 | * - Numbers (Integers and Floats) |
||
| 655 | * - Strings |
||
| 656 | * - Variables |
||
| 657 | * - sub-arrays |
||
| 658 | * |
||
| 659 | * @param string $arrayText Array text |
||
| 660 | * @return NodeInterface[] the array node built up |
||
| 661 | * @throws Exception |
||
| 662 | */ |
||
| 663 | protected function recursiveArrayHandler($arrayText) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Text node handler |
||
| 687 | * |
||
| 688 | * @param ParsingState $state |
||
| 689 | * @param string $text |
||
| 690 | * @return void |
||
| 691 | */ |
||
| 692 | protected function textHandler(ParsingState $state, $text) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @return ParsingState |
||
| 701 | */ |
||
| 702 | protected function getParsingState() |
||
| 712 | } |
||
| 713 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.