‪TYPO3CMS  ‪main
GeneratorClassesResolver.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PhpParser\BuilderFactory;
21 use PhpParser\Node;
22 use PhpParser\Node\Expr;
23 use PhpParser\Node\Expr\ClassConstFetch;
24 use PhpParser\Node\Expr\New_;
25 use PhpParser\Node\Expr\StaticCall;
26 use PhpParser\Node\Name\FullyQualified;
27 use PhpParser\Node\Scalar\String_;
28 use PhpParser\NodeVisitorAbstract;
31 
41 class ‪GeneratorClassesResolver extends NodeVisitorAbstract
42 {
46  protected ‪$builderFactory;
47 
48  public function ‪__construct(BuilderFactory ‪$builderFactory = null)
49  {
50  $this->builderFactory = ‪$builderFactory ?? new BuilderFactory();
51  }
52 
59  public function ‪enterNode(Node $node)
60  {
61  if ($node instanceof StaticCall
62  && $node->class instanceof FullyQualified
63  && $node->class->toString() === GeneralUtility::class
64  && $node->name->name === 'makeInstance'
65  && isset($node->args[0]->value)
66  && $node->args[0]->value instanceof Expr
67  ) {
68  $argValue = $node->args[0]->value;
69  $argAlternative = $this->‪substituteClassString($argValue);
70  if ($argAlternative !== null) {
71  $node->args[0]->value = $argAlternative;
72  $argValue = $argAlternative;
73  }
74 
75  $nodeAlternative = $this->‪substituteMakeInstance($node, $argValue);
76  if ($nodeAlternative !== null) {
77  $node->setAttribute(‪AbstractCoreMatcher::NODE_RESOLVED_AS, $nodeAlternative);
78  }
79  }
80  return null;
81  }
82 
87  protected function ‪substituteClassString(Expr $argValue): ?ClassConstFetch
88  {
89  // skip non-strings, and those starting with (invalid) namespace separator
90  if (!$argValue instanceof String_ || $argValue->value[0] === '\\') {
91  return null;
92  }
93 
94  $classString = ltrim($argValue->value, '\\');
95  $className = new FullyQualified($classString);
96  $classArg = $this->builderFactory->classConstFetch($className, 'class');
97  $this->‪duplicateNodeAttributes($argValue, $className, $classArg);
98  return $classArg;
99  }
100 
105  protected function ‪substituteMakeInstance(StaticCall $node, Expr $argValue): ?New_
106  {
107  if (!$argValue instanceof ClassConstFetch
108  || !$argValue->class instanceof FullyQualified
109  ) {
110  return null;
111  }
112 
113  $newExpr = $this->builderFactory->new(
114  $argValue->class,
115  array_slice($node->args, 1),
116  );
117  $this->‪duplicateNodeAttributes($node, $newExpr);
118  return $newExpr;
119  }
120 
126  protected function ‪duplicateNodeAttributes(Node $source, Node ...$targets): void
127  {
128  foreach ($targets as $target) {
129  $target->setAttributes([
130  'startLine' => $source->getStartLine(),
131  'endLine' => $source->getEndLine(),
132  ]);
133  }
134  }
135 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\duplicateNodeAttributes
‪duplicateNodeAttributes(Node $source, Node ... $targets)
Definition: GeneratorClassesResolver.php:125
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver
Definition: GeneratorClassesResolver.php:42
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\NODE_RESOLVED_AS
‪const NODE_RESOLVED_AS
Definition: AbstractCoreMatcher.php:35
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\__construct
‪__construct(BuilderFactory $builderFactory=null)
Definition: GeneratorClassesResolver.php:47
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher
Definition: AbstractCoreMatcher.php:34
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\$builderFactory
‪BuilderFactory $builderFactory
Definition: GeneratorClassesResolver.php:45
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\substituteMakeInstance
‪substituteMakeInstance(StaticCall $node, Expr $argValue)
Definition: GeneratorClassesResolver.php:104
‪TYPO3\CMS\Install\ExtensionScanner\Php
Definition: CodeStatistics.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\substituteClassString
‪substituteClassString(Expr $argValue)
Definition: GeneratorClassesResolver.php:86
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\enterNode
‪enterNode(Node $node)
Definition: GeneratorClassesResolver.php:58
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52