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 | * Parses a given template string and returns a parsed template object. |
||
| 102 | * |
||
| 103 | * The resulting ParsedTemplate can then be rendered by calling evaluate() on it. |
||
| 104 | * |
||
| 105 | * Normally, you should use a subclass of AbstractTemplateView instead of calling the |
||
| 106 | * TemplateParser directly. |
||
| 107 | * |
||
| 108 | * @param string $templateString The template to parse as a string |
||
| 109 | * @param string|null $templateIdentifier If the template has an identifying string it can be passed here to improve error reporting. |
||
| 110 | * @return ParsingState Parsed template |
||
| 111 | * @throws Exception |
||
| 112 | */ |
||
| 113 | public function parse($templateString, $templateIdentifier = null) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param \Exception $error |
||
| 135 | * @param string $templateIdentifier |
||
| 136 | * @throws \Exception |
||
| 137 | */ |
||
| 138 | public function createParsingRelatedExceptionWithContext(\Exception $error, $templateIdentifier) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $templateIdentifier |
||
| 159 | * @param \Closure $templateSourceClosure Closure which returns the template source if needed |
||
| 160 | * @return ParsedTemplateInterface |
||
| 161 | */ |
||
| 162 | public function getOrParseAndStoreTemplate($templateIdentifier, $templateSourceClosure) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Pre-process the template source, making all registered TemplateProcessors |
||
| 190 | * do what they need to do with the template source before it is parsed. |
||
| 191 | * |
||
| 192 | * @param string $templateSource |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | protected function preProcessTemplateSource($templateSource) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Resets the parser to its default values. |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | protected function reset() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Extracts escaping modifiers ({escapingEnabled=true/false}) out of the given template and sets $this->escapingEnabled accordingly |
||
| 217 | * |
||
| 218 | * @param string $templateString Template string to extract the {escaping = ..} definitions from |
||
| 219 | * @return string The updated template string without escaping declarations inside |
||
| 220 | * @throws Exception if there is more than one modifier |
||
| 221 | */ |
||
| 222 | protected function extractEscapingModifier($templateString) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Splits the template string on all dynamic tags found. |
||
| 242 | * |
||
| 243 | * @param string $templateString Template string to split. |
||
| 244 | * @return array Splitted template |
||
| 245 | */ |
||
| 246 | protected function splitTemplateAtDynamicTags($templateString) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Build object tree from the split template |
||
| 253 | * |
||
| 254 | * @param array $splitTemplate The split template, so that every tag with a namespace declaration is already a seperate array element. |
||
| 255 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
| 256 | * @return ParsingState |
||
| 257 | * @throws Exception |
||
| 258 | */ |
||
| 259 | protected function buildObjectTree(array $splitTemplate, $context) |
||
| 304 | /** |
||
| 305 | * Handles an opening or self-closing view helper tag. |
||
| 306 | * |
||
| 307 | * @param ParsingState $state Current parsing state |
||
| 308 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
| 309 | * @param string $methodIdentifier Method identifier |
||
| 310 | * @param string $arguments Arguments string, not yet parsed |
||
| 311 | * @param boolean $selfclosing true, if the tag is a self-closing tag. |
||
| 312 | * @param string $templateElement The template code containing the ViewHelper call |
||
| 313 | * @return NodeInterface|null |
||
| 314 | */ |
||
| 315 | protected function openingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $arguments, $selfclosing, $templateElement) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Initialize the given ViewHelper and adds it to the current node and to |
||
| 339 | * the stack. |
||
| 340 | * |
||
| 341 | * @param ParsingState $state Current parsing state |
||
| 342 | * @param string $namespaceIdentifier Namespace identifier - being looked up in $this->namespaces |
||
| 343 | * @param string $methodIdentifier Method identifier |
||
| 344 | * @param array $argumentsObjectTree Arguments object tree |
||
| 345 | * @return null|NodeInterface An instance of ViewHelperNode if identity was valid - NULL if the namespace/identity was not registered |
||
| 346 | * @throws Exception |
||
| 347 | */ |
||
| 348 | protected function initializeViewHelperAndAddItToStack(ParsingState $state, $namespaceIdentifier, $methodIdentifier, $argumentsObjectTree) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Handles a closing view helper tag |
||
| 372 | * |
||
| 373 | * @param ParsingState $state The current parsing state |
||
| 374 | * @param string $namespaceIdentifier Namespace identifier for the closing tag. |
||
| 375 | * @param string $methodIdentifier Method identifier. |
||
| 376 | * @return boolean whether the viewHelper was found and added to the stack or not |
||
| 377 | * @throws Exception |
||
| 378 | */ |
||
| 379 | protected function closingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Handles the appearance of an object accessor (like {posts.author.email}). |
||
| 406 | * Creates a new instance of \TYPO3Fluid\Fluid\ObjectAccessorNode. |
||
| 407 | * |
||
| 408 | * Handles ViewHelpers as well which are in the shorthand syntax. |
||
| 409 | * |
||
| 410 | * @param ParsingState $state The current parsing state |
||
| 411 | * @param string $objectAccessorString String which identifies which objects to fetch |
||
| 412 | * @param string $delimiter |
||
| 413 | * @param string $viewHelperString |
||
| 414 | * @param string $additionalViewHelpersString |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | protected function objectAccessorHandler(ParsingState $state, $objectAccessorString, $delimiter, $viewHelperString, $additionalViewHelpersString) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Call all interceptors registered for a given interception point. |
||
| 463 | * |
||
| 464 | * @param NodeInterface $node The syntax tree node which can be modified by the interceptors. |
||
| 465 | * @param integer $interceptionPoint the interception point. One of the \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_* constants. |
||
| 466 | * @param ParsingState $state the parsing state |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | protected function callInterceptor(NodeInterface & $node, $interceptionPoint, ParsingState $state) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Parse arguments of a given tag, and build up the Arguments Object Tree |
||
| 489 | * for each argument. |
||
| 490 | * Returns an associative array, where the key is the name of the argument, |
||
| 491 | * and the value is a single Argument Object Tree. |
||
| 492 | * |
||
| 493 | * @param string $argumentsString All arguments as string |
||
| 494 | * @return array An associative array of objects, where the key is the argument name. |
||
| 495 | */ |
||
| 496 | protected function parseArguments($argumentsString) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Build up an argument object tree for the string in $argumentString. |
||
| 515 | * This builds up the tree for a single argument value. |
||
| 516 | * |
||
| 517 | * This method also does some performance optimizations, so in case |
||
| 518 | * no { or < is found, then we just return a TextNode. |
||
| 519 | * |
||
| 520 | * @param string $argumentString |
||
| 521 | * @return SyntaxTree\NodeInterface the corresponding argument object tree. |
||
| 522 | */ |
||
| 523 | protected function buildArgumentObjectTree($argumentString) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Removes escapings from a given argument string and trims the outermost |
||
| 538 | * quotes. |
||
| 539 | * |
||
| 540 | * This method is meant as a helper for regular expression results. |
||
| 541 | * |
||
| 542 | * @param string $quotedValue Value to unquote |
||
| 543 | * @return string Unquoted value |
||
| 544 | */ |
||
| 545 | public function unquoteString($quotedValue) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Handler for everything which is not a ViewHelperNode. |
||
| 558 | * |
||
| 559 | * This includes Text, array syntax, and object accessor syntax. |
||
| 560 | * |
||
| 561 | * @param ParsingState $state Current parsing state |
||
| 562 | * @param string $text Text to process |
||
| 563 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently. |
||
| 564 | * @return void |
||
| 565 | */ |
||
| 566 | protected function textAndShorthandSyntaxHandler(ParsingState $state, $text, $context) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Handler for array syntax. This creates the array object recursively and |
||
| 623 | * adds it to the current node. |
||
| 624 | * |
||
| 625 | * @param ParsingState $state The current parsing state |
||
| 626 | * @param NodeInterface[] $arrayText The array as string. |
||
| 627 | * @return void |
||
| 628 | */ |
||
| 629 | protected function arrayHandler(ParsingState $state, $arrayText) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Recursive function which takes the string representation of an array and |
||
| 637 | * builds an object tree from it. |
||
| 638 | * |
||
| 639 | * Deals with the following value types: |
||
| 640 | * - Numbers (Integers and Floats) |
||
| 641 | * - Strings |
||
| 642 | * - Variables |
||
| 643 | * - sub-arrays |
||
| 644 | * |
||
| 645 | * @param string $arrayText Array text |
||
| 646 | * @return NodeInterface[] the array node built up |
||
| 647 | * @throws Exception |
||
| 648 | */ |
||
| 649 | protected function recursiveArrayHandler($arrayText) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Text node handler |
||
| 673 | * |
||
| 674 | * @param ParsingState $state |
||
| 675 | * @param string $text |
||
| 676 | * @return void |
||
| 677 | */ |
||
| 678 | protected function textHandler(ParsingState $state, $text) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @return ParsingState |
||
| 687 | */ |
||
| 688 | protected function getParsingState() |
||
| 698 | } |
||
| 699 |
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.