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