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)  | 
            ||
| 388 | |||
| 389 | /**  | 
            ||
| 390 | * Handles a closing view helper tag  | 
            ||
| 391 | *  | 
            ||
| 392 | * @param ParsingState $state The current parsing state  | 
            ||
| 393 | * @param string $namespaceIdentifier Namespace identifier for the closing tag.  | 
            ||
| 394 | * @param string $methodIdentifier Method identifier.  | 
            ||
| 395 | * @return boolean whether the viewHelper was found and added to the stack or not  | 
            ||
| 396 | * @throws Exception  | 
            ||
| 397 | */  | 
            ||
| 398 | protected function closingViewHelperTagHandler(ParsingState $state, $namespaceIdentifier, $methodIdentifier)  | 
            ||
| 422 | |||
| 423 | /**  | 
            ||
| 424 |      * Handles the appearance of an object accessor (like {posts.author.email}). | 
            ||
| 425 | * Creates a new instance of \TYPO3Fluid\Fluid\ObjectAccessorNode.  | 
            ||
| 426 | *  | 
            ||
| 427 | * Handles ViewHelpers as well which are in the shorthand syntax.  | 
            ||
| 428 | *  | 
            ||
| 429 | * @param ParsingState $state The current parsing state  | 
            ||
| 430 | * @param string $objectAccessorString String which identifies which objects to fetch  | 
            ||
| 431 | * @param string $delimiter  | 
            ||
| 432 | * @param string $viewHelperString  | 
            ||
| 433 | * @param string $additionalViewHelpersString  | 
            ||
| 434 | * @return void  | 
            ||
| 435 | */  | 
            ||
| 436 | protected function objectAccessorHandler(ParsingState $state, $objectAccessorString, $delimiter, $viewHelperString, $additionalViewHelpersString)  | 
            ||
| 479 | |||
| 480 | /**  | 
            ||
| 481 | * Call all interceptors registered for a given interception point.  | 
            ||
| 482 | *  | 
            ||
| 483 | * @param NodeInterface $node The syntax tree node which can be modified by the interceptors.  | 
            ||
| 484 | * @param integer $interceptionPoint the interception point. One of the \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_* constants.  | 
            ||
| 485 | * @param ParsingState $state the parsing state  | 
            ||
| 486 | * @return void  | 
            ||
| 487 | */  | 
            ||
| 488 | protected function callInterceptor(NodeInterface & $node, $interceptionPoint, ParsingState $state)  | 
            ||
| 505 | |||
| 506 | /**  | 
            ||
| 507 | * Parse arguments of a given tag, and build up the Arguments Object Tree  | 
            ||
| 508 | * for each argument.  | 
            ||
| 509 | * Returns an associative array, where the key is the name of the argument,  | 
            ||
| 510 | * and the value is a single Argument Object Tree.  | 
            ||
| 511 | *  | 
            ||
| 512 | * @param string $argumentsString All arguments as string  | 
            ||
| 513 | * @return array An associative array of objects, where the key is the argument name.  | 
            ||
| 514 | */  | 
            ||
| 515 | protected function parseArguments($argumentsString)  | 
            ||
| 531 | |||
| 532 | /**  | 
            ||
| 533 | * Build up an argument object tree for the string in $argumentString.  | 
            ||
| 534 | * This builds up the tree for a single argument value.  | 
            ||
| 535 | *  | 
            ||
| 536 | * This method also does some performance optimizations, so in case  | 
            ||
| 537 |      * no { or < is found, then we just return a TextNode. | 
            ||
| 538 | *  | 
            ||
| 539 | * @param string $argumentString  | 
            ||
| 540 | * @return SyntaxTree\NodeInterface the corresponding argument object tree.  | 
            ||
| 541 | */  | 
            ||
| 542 | protected function buildArgumentObjectTree($argumentString)  | 
            ||
| 554 | |||
| 555 | /**  | 
            ||
| 556 | * Removes escapings from a given argument string and trims the outermost  | 
            ||
| 557 | * quotes.  | 
            ||
| 558 | *  | 
            ||
| 559 | * This method is meant as a helper for regular expression results.  | 
            ||
| 560 | *  | 
            ||
| 561 | * @param string $quotedValue Value to unquote  | 
            ||
| 562 | * @return string Unquoted value  | 
            ||
| 563 | */  | 
            ||
| 564 | public function unquoteString($quotedValue)  | 
            ||
| 577 | |||
| 578 | /**  | 
            ||
| 579 | * Handler for everything which is not a ViewHelperNode.  | 
            ||
| 580 | *  | 
            ||
| 581 | * This includes Text, array syntax, and object accessor syntax.  | 
            ||
| 582 | *  | 
            ||
| 583 | * @param ParsingState $state Current parsing state  | 
            ||
| 584 | * @param string $text Text to process  | 
            ||
| 585 | * @param integer $context one of the CONTEXT_* constants, defining whether we are inside or outside of ViewHelper arguments currently.  | 
            ||
| 586 | * @return void  | 
            ||
| 587 | */  | 
            ||
| 588 | protected function textAndShorthandSyntaxHandler(ParsingState $state, $text, $context)  | 
            ||
| 652 | |||
| 653 | /**  | 
            ||
| 654 | * Handler for array syntax. This creates the array object recursively and  | 
            ||
| 655 | * adds it to the current node.  | 
            ||
| 656 | *  | 
            ||
| 657 | * @param ParsingState $state The current parsing state  | 
            ||
| 658 | * @param NodeInterface[] $arrayText The array as string.  | 
            ||
| 659 | * @return void  | 
            ||
| 660 | */  | 
            ||
| 661 | protected function arrayHandler(ParsingState $state, $arrayText)  | 
            ||
| 666 | |||
| 667 | /**  | 
            ||
| 668 | * Recursive function which takes the string representation of an array and  | 
            ||
| 669 | * builds an object tree from it.  | 
            ||
| 670 | *  | 
            ||
| 671 | * Deals with the following value types:  | 
            ||
| 672 | * - Numbers (Integers and Floats)  | 
            ||
| 673 | * - Strings  | 
            ||
| 674 | * - Variables  | 
            ||
| 675 | * - sub-arrays  | 
            ||
| 676 | *  | 
            ||
| 677 | * @param string $arrayText Array text  | 
            ||
| 678 | * @return NodeInterface[] the array node built up  | 
            ||
| 679 | * @throws Exception  | 
            ||
| 680 | */  | 
            ||
| 681 | protected function recursiveArrayHandler($arrayText)  | 
            ||
| 704 | |||
| 705 | /**  | 
            ||
| 706 | * Text node handler  | 
            ||
| 707 | *  | 
            ||
| 708 | * @param ParsingState $state  | 
            ||
| 709 | * @param string $text  | 
            ||
| 710 | * @return void  | 
            ||
| 711 | */  | 
            ||
| 712 | protected function textHandler(ParsingState $state, $text)  | 
            ||
| 718 | |||
| 719 | /**  | 
            ||
| 720 | * @param NodeInterface $parent  | 
            ||
| 721 | * @param NodeInterface $child  | 
            ||
| 722 | */  | 
            ||
| 723 | protected function addChildToNode(NodeInterface $parent, NodeInterface $child)  | 
            ||
| 729 | |||
| 730 | /**  | 
            ||
| 731 | * @return ParsingState  | 
            ||
| 732 | */  | 
            ||
| 733 | protected function getParsingState()  | 
            ||
| 743 | }  | 
            ||
| 744 | 
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.