| Total Complexity | 70 |
| Total Lines | 346 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 0 |
Complex classes like TemplateCommentsParser 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.
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 TemplateCommentsParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 75 | class TemplateCommentsParser extends Parser |
||
| 76 | { |
||
| 77 | private $stack = []; |
||
| 78 | private $stream; |
||
| 79 | private $parent; |
||
| 80 | private $visitors; |
||
| 81 | private $expressionParser; |
||
| 82 | private $blocks; |
||
| 83 | private $blockStack; |
||
| 84 | private $macros; |
||
| 85 | private $env; |
||
| 86 | private $importedSymbols; |
||
| 87 | private $traits; |
||
| 88 | private $embeddedTemplates = []; |
||
| 89 | private $varNameSalt = 0; |
||
| 90 | private $expressionParserClass; |
||
| 91 | |||
| 92 | public function __construct(Environment $env) |
||
| 93 | { |
||
| 94 | $this->env = $env; |
||
| 95 | $this->expressionParserClass = ExpressionParser::class; |
||
| 96 | // Get the existing parser object used by the Twig $env |
||
| 97 | try { |
||
| 98 | $parserReflection = ReflectionHelper::getReflectionProperty($env, 'parser'); |
||
| 99 | } catch (ReflectionException $e) { |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | $parserReflection->setAccessible(true); |
||
| 103 | $parser = $parserReflection->getValue($env); |
||
| 104 | if ($parser === null) { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | // Get the expression parser used by the current parser |
||
| 108 | try { |
||
| 109 | $expressionParserReflection = ReflectionHelper::getReflectionProperty($parser, 'expressionParser'); |
||
| 110 | } catch (ReflectionException $e) { |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | // Preserve the existing expression parser and use it |
||
| 114 | $expressionParserReflection->setAccessible(true); |
||
| 115 | $expressionParser = $expressionParserReflection->getValue($parser); |
||
| 116 | $this->expressionParserClass = get_class($expressionParser); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getVarName(): string |
||
| 120 | { |
||
| 121 | return sprintf('__internal_parse_%d', $this->varNameSalt++); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function parse(TokenStream $stream, $test = null, bool $dropNeedle = false): ModuleNode |
||
| 125 | { |
||
| 126 | $vars = get_object_vars($this); |
||
| 127 | unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames'], $vars['varNameSalt']); |
||
| 128 | $this->stack[] = $vars; |
||
| 129 | |||
| 130 | // node visitors |
||
| 131 | if (null === $this->visitors) { |
||
| 132 | $this->visitors = $this->env->getNodeVisitors(); |
||
| 133 | } |
||
| 134 | |||
| 135 | if (null === $this->expressionParser) { |
||
| 136 | $this->expressionParser = new $this->expressionParserClass($this, $this->env); |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->stream = $stream; |
||
| 140 | $this->parent = null; |
||
| 141 | $this->blocks = []; |
||
| 142 | $this->macros = []; |
||
| 143 | $this->traits = []; |
||
| 144 | $this->blockStack = []; |
||
| 145 | $this->importedSymbols = [[]]; |
||
| 146 | $this->embeddedTemplates = []; |
||
| 147 | |||
| 148 | try { |
||
| 149 | $body = $this->subparse($test, $dropNeedle); |
||
| 150 | |||
| 151 | if (null !== $this->parent && null === $body = $this->filterBodyNodes($body)) { |
||
| 152 | $body = new Node(); |
||
| 153 | } |
||
| 154 | } catch (SyntaxError $e) { |
||
| 155 | if (!$e->getSourceContext()) { |
||
| 156 | $e->setSourceContext($this->stream->getSourceContext()); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (!$e->getTemplateLine()) { |
||
| 160 | $e->setTemplateLine($this->stream->getCurrent()->getLine()); |
||
| 161 | } |
||
| 162 | |||
| 163 | throw $e; |
||
| 164 | } |
||
| 165 | |||
| 166 | $node = new ModuleNode(new BodyNode([$body]), $this->parent, new Node($this->blocks), new Node($this->macros), new Node($this->traits), $this->embeddedTemplates, $stream->getSourceContext()); |
||
| 167 | |||
| 168 | $traverser = new NodeTraverser($this->env, $this->visitors); |
||
| 169 | |||
| 170 | /** @var ModuleNode $node */ |
||
| 171 | $node = $traverser->traverse($node); |
||
| 172 | |||
| 173 | // restore previous stack so previous parse() call can resume working |
||
| 174 | foreach (array_pop($this->stack) as $key => $val) { |
||
| 175 | $this->$key = $val; |
||
| 176 | } |
||
| 177 | |||
| 178 | return $node; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function subparse($test, bool $dropNeedle = false): Node |
||
| 253 | } |
||
| 254 | |||
| 255 | public function getBlockStack(): array |
||
| 256 | { |
||
| 257 | return $this->blockStack; |
||
| 258 | } |
||
| 259 | |||
| 260 | public function peekBlockStack() |
||
| 261 | { |
||
| 262 | return $this->blockStack[count($this->blockStack) - 1] ?? null; |
||
| 263 | } |
||
| 264 | |||
| 265 | public function popBlockStack(): void |
||
| 266 | { |
||
| 267 | array_pop($this->blockStack); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function pushBlockStack($name): void |
||
| 271 | { |
||
| 272 | $this->blockStack[] = $name; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function hasBlock(string $name): bool |
||
| 276 | { |
||
| 277 | return isset($this->blocks[$name]); |
||
| 278 | } |
||
| 279 | |||
| 280 | public function getBlock(string $name): Node |
||
| 281 | { |
||
| 282 | return $this->blocks[$name]; |
||
| 283 | } |
||
| 284 | |||
| 285 | public function setBlock(string $name, BlockNode $value): void |
||
| 286 | { |
||
| 287 | $this->blocks[$name] = new BodyNode([$value], [], $value->getTemplateLine()); |
||
| 288 | } |
||
| 289 | |||
| 290 | public function hasMacro(string $name): bool |
||
| 291 | { |
||
| 292 | return isset($this->macros[$name]); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function setMacro(string $name, MacroNode $node): void |
||
| 296 | { |
||
| 297 | $this->macros[$name] = $node; |
||
| 298 | } |
||
| 299 | |||
| 300 | public function addTrait($trait): void |
||
| 301 | { |
||
| 302 | $this->traits[] = $trait; |
||
| 303 | } |
||
| 304 | |||
| 305 | public function hasTraits(): bool |
||
| 306 | { |
||
| 307 | return count($this->traits) > 0; |
||
| 308 | } |
||
| 309 | |||
| 310 | public function embedTemplate(ModuleNode $template) |
||
| 311 | { |
||
| 312 | $template->setIndex(mt_rand()); |
||
| 313 | |||
| 314 | $this->embeddedTemplates[] = $template; |
||
| 315 | } |
||
| 316 | |||
| 317 | public function addImportedSymbol(string $type, string $alias, string $name = null, AbstractExpression $node = null): void |
||
| 318 | { |
||
| 319 | $this->importedSymbols[0][$type][$alias] = ['name' => $name, 'node' => $node]; |
||
| 320 | } |
||
| 321 | |||
| 322 | public function getImportedSymbol(string $type, string $alias) |
||
| 323 | { |
||
| 324 | // if the symbol does not exist in the current scope (0), try in the main/global scope (last index) |
||
| 325 | return $this->importedSymbols[0][$type][$alias] ?? ($this->importedSymbols[count($this->importedSymbols) - 1][$type][$alias] ?? null); |
||
| 326 | } |
||
| 327 | |||
| 328 | public function isMainScope(): bool |
||
| 329 | { |
||
| 330 | return 1 === count($this->importedSymbols); |
||
| 331 | } |
||
| 332 | |||
| 333 | public function pushLocalScope(): void |
||
| 334 | { |
||
| 335 | array_unshift($this->importedSymbols, []); |
||
| 336 | } |
||
| 337 | |||
| 338 | public function popLocalScope(): void |
||
| 339 | { |
||
| 340 | array_shift($this->importedSymbols); |
||
| 341 | } |
||
| 342 | |||
| 343 | public function getExpressionParser(): ExpressionParser |
||
| 344 | { |
||
| 345 | return $this->expressionParser; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function getParent(): ?Node |
||
| 349 | { |
||
| 350 | return $this->parent; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function setParent(?Node $parent): void |
||
| 356 | } |
||
| 357 | |||
| 358 | public function hasInheritance() |
||
| 359 | { |
||
| 360 | return $this->parent || 0 < count($this->traits); |
||
| 361 | } |
||
| 362 | |||
| 363 | public function getStream(): TokenStream |
||
| 364 | { |
||
| 365 | return $this->stream; |
||
| 366 | } |
||
| 367 | |||
| 368 | public function getCurrentToken(): Token |
||
| 369 | { |
||
| 370 | return $this->stream->getCurrent(); |
||
| 371 | } |
||
| 372 | |||
| 373 | private function filterBodyNodes(Node $node, bool $nested = false): ?Node |
||
| 374 | { |
||
| 375 | // check that the body does not contain non-empty output nodes |
||
| 376 | if ( |
||
| 377 | ($node instanceof TextNode && !ctype_space($node->getAttribute('data'))) |
||
| 378 | || (!$node instanceof TextNode && !$node instanceof BlockReferenceNode && $node instanceof NodeOutputInterface) |
||
| 379 | ) { |
||
| 380 | if (str_contains((string)$node, chr(0xEF) . chr(0xBB) . chr(0xBF))) { |
||
| 421 | } |
||
| 422 | } |
||
| 423 |