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) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param \Exception $error |
||
| 148 | * @param string $templateIdentifier |
||
| 149 | * @throws \Exception |
||
| 150 | */ |
||
| 151 | public function createParsingRelatedExceptionWithContext(\Exception $error, $templateIdentifier) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $templateIdentifier |
||
| 172 | * @param \Closure $templateSourceClosure Closure which returns the template source if needed |
||
| 173 | * @return ParsedTemplateInterface |
||
| 174 | */ |
||
| 175 | public function getOrParseAndStoreTemplate($templateIdentifier, $templateSourceClosure) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Pre-process the template source, making all registered TemplateProcessors |
||
| 203 | * do what they need to do with the template source before it is parsed. |
||
| 204 | * |
||
| 205 | * @param string $templateSource |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | protected function preProcessTemplateSource($templateSource) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Resets the parser to its default values. |
||
| 218 | * |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | protected function reset() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Splits the template string on all dynamic tags found. |
||
| 230 | * |
||
| 231 | * @param string $templateString Template string to split. |
||
| 232 | * @return array Splitted template |
||
| 233 | */ |
||
| 234 | protected function splitTemplateAtDynamicTags($templateString) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Build object tree from the split template |
||
| 241 | * |
||
| 242 | * @param array $splitTemplate The split template, so that every tag with a namespace declaration is already a seperate array element. |
||
| 243 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
| 244 | * @return ParsingState |
||
| 245 | * @throws Exception |
||
| 246 | */ |
||
| 247 | protected function buildObjectTree(array $splitTemplate, $context) |
||
| 292 | /** |
||
| 293 | * Handles an opening or self-closing view helper tag. |
||
| 294 | * |
||
| 295 | * @param ParsingState $state Current parsing state |
||
| 296 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
| 297 | * @param string $methodIdentifier Method identifier |
||
| 298 | * @param string $arguments Arguments string, not yet parsed |
||
| 299 | * @param boolean $selfclosing true, if the tag is a self-closing tag. |
||
| 300 | * @param string $templateElement The template code containing the ViewHelper call |
||
| 301 | * @return NodeInterface|null |
||
| 302 | */ |
||
| 303 | protected function openingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $arguments, $selfclosing, $templateElement) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Initialize the given ViewHelper and adds it to the current node and to |
||
| 327 | * the stack. |
||
| 328 | * |
||
| 329 | * @param ParsingState $state Current parsing state |
||
| 330 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
| 331 | * @param string $methodIdentifier Method identifier |
||
| 332 | * @param array $argumentsObjectTree Arguments object tree |
||
| 333 | * @return null|NodeInterface An instance of ViewHelperNode if identity was valid - NULL if the namespace/identity was not registered |
||
| 334 | * @throws Exception |
||
| 335 | */ |
||
| 336 | protected function initializeViewHelperAndAddItToStack(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $argumentsObjectTree) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Handles a closing view helper tag |
||
| 360 | * |
||
| 361 | * @param ParsingState $state The current parsing state |
||
| 362 | * @param string $namespaceIdentifier Namespace identifier for the closing tag. |
||
| 363 | * @param string $methodIdentifier Method identifier. |
||
| 364 | * @return boolean whether the viewHelper was found and added to the stack or not |
||
| 365 | * @throws Exception |
||
| 366 | */ |
||
| 367 | protected function closingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Handles the appearance of an object accessor (like {posts.author.email}). |
||
| 394 | * Creates a new instance of \TYPO3Fluid\Fluid\ObjectAccessorNode. |
||
| 395 | * |
||
| 396 | * Handles ViewHelpers as well which are in the shorthand syntax. |
||
| 397 | * |
||
| 398 | * @param ParsingState $state The current parsing state |
||
| 399 | * @param string $objectAccessorString String which identifies which objects to fetch |
||
| 400 | * @param string $delimiter |
||
| 401 | * @param string $viewHelperString |
||
| 402 | * @param string $additionalViewHelpersString |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | protected function objectAccessorHandler(ParsingState $state, $objectAccessorString, $delimiter, $viewHelperString, $additionalViewHelpersString) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Call all interceptors registered for a given interception point. |
||
| 451 | * |
||
| 452 | * @param NodeInterface $node The syntax tree node which can be modified by the interceptors. |
||
| 453 | * @param integer $interceptionPoint the interception point. One of the \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_* constants. |
||
| 454 | * @param ParsingState $state the parsing state |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | protected function callInterceptor(NodeInterface & $node, $interceptionPoint, ParsingState $state) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Parse arguments of a given tag, and build up the Arguments Object Tree |
||
| 477 | * for each argument. |
||
| 478 | * Returns an associative array, where the key is the name of the argument, |
||
| 479 | * and the value is a single Argument Object Tree. |
||
| 480 | * |
||
| 481 | * @param string $argumentsString All arguments as string |
||
| 482 | * @return array An associative array of objects, where the key is the argument name. |
||
| 483 | */ |
||
| 484 | protected function parseArguments($argumentsString) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Build up an argument object tree for the string in $argumentString. |
||
| 503 | * This builds up the tree for a single argument value. |
||
| 504 | * |
||
| 505 | * This method also does some performance optimizations, so in case |
||
| 506 | * no { or < is found, then we just return a TextNode. |
||
| 507 | * |
||
| 508 | * @param string $argumentString |
||
| 509 | * @return SyntaxTree\NodeInterface the corresponding argument object tree. |
||
| 510 | */ |
||
| 511 | protected function buildArgumentObjectTree($argumentString) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Removes escapings from a given argument string and trims the outermost |
||
| 526 | * quotes. |
||
| 527 | * |
||
| 528 | * This method is meant as a helper for regular expression results. |
||
| 529 | * |
||
| 530 | * @param string $quotedValue Value to unquote |
||
| 531 | * @return string Unquoted value |
||
| 532 | */ |
||
| 533 | public function unquoteString($quotedValue) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Handler for everything which is not a ViewHelperNode. |
||
| 546 | * |
||
| 547 | * This includes Text, array syntax, and object accessor syntax. |
||
| 548 | * |
||
| 549 | * @param ParsingState $state Current parsing state |
||
| 550 | * @param string $text Text to process |
||
| 551 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
| 552 | * @return void |
||
| 553 | */ |
||
| 554 | protected function textAndShorthandSyntaxHandler(ParsingState $state, $text, $context) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Handler for array syntax. This creates the array object recursively and |
||
| 611 | * adds it to the current node. |
||
| 612 | * |
||
| 613 | * @param ParsingState $state The current parsing state |
||
| 614 | * @param NodeInterface[] $arrayText The array as string. |
||
| 615 | * @return void |
||
| 616 | */ |
||
| 617 | protected function arrayHandler(ParsingState $state, $arrayText) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Recursive function which takes the string representation of an array and |
||
| 625 | * builds an object tree from it. |
||
| 626 | * |
||
| 627 | * Deals with the following value types: |
||
| 628 | * - Numbers (Integers and Floats) |
||
| 629 | * - Strings |
||
| 630 | * - Variables |
||
| 631 | * - sub-arrays |
||
| 632 | * |
||
| 633 | * @param string $arrayText Array text |
||
| 634 | * @return NodeInterface[] the array node built up |
||
| 635 | * @throws Exception |
||
| 636 | */ |
||
| 637 | protected function recursiveArrayHandler($arrayText) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Text node handler |
||
| 661 | * |
||
| 662 | * @param ParsingState $state |
||
| 663 | * @param string $text |
||
| 664 | * @return void |
||
| 665 | */ |
||
| 666 | protected function textHandler(ParsingState $state, $text) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @return ParsingState |
||
| 675 | */ |
||
| 676 | protected function getParsingState() |
||
| 686 | } |
||
| 687 |
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.