‪TYPO3CMS  10.4
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\Node;
21 use PhpParser\Node\Expr;
22 use PhpParser\Node\Expr\ClassConstFetch;
23 use PhpParser\Node\Expr\New_;
24 use PhpParser\Node\Expr\StaticCall;
25 use PhpParser\Node\Name\FullyQualified;
26 use PhpParser\Node\Scalar\String_;
27 use PhpParser\NodeVisitorAbstract;
30 
40 class ‪GeneratorClassesResolver extends NodeVisitorAbstract
41 {
48  public function ‪enterNode(Node $node)
49  {
50  if ($node instanceof StaticCall
51  && $node->class instanceof FullyQualified
52  && $node->class->toString() === GeneralUtility::class
53  && $node->name->name === 'makeInstance'
54  && isset($node->args[0]->value)
55  && $node->args[0]->value instanceof Expr
56  ) {
57  if (null === $className = $this->‪resolveClassName($node->args[0]->value)) {
58  return;
59  }
60  $node->args[0]->value = $className;
61  $node->setAttribute(‪AbstractCoreMatcher::NODE_RESOLVED_AS, new New_(
62  $className,
63  // remove first argument (class name)
64  array_slice($node->args, 1),
65  $node->getAttributes()
66  ));
67  }
68  }
69 
70  protected function ‪resolveClassName(Expr $value): ?FullyQualified
71  {
72  if ($value instanceof String_) {
73  // 'TYPO3\\CMS\\ClassName'
74  return new FullyQualified($value->value, $value->getAttributes());
75  }
76  if ($value instanceof ClassConstFetch && $value->class instanceof FullyQualified) {
77  // \TYPO3\CMS\ClassName::class
78  return $value->class;
79  }
80  return null;
81  }
82 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\resolveClassName
‪resolveClassName(Expr $value)
Definition: GeneratorClassesResolver.php:70
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver
Definition: GeneratorClassesResolver.php:41
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\NODE_RESOLVED_AS
‪const NODE_RESOLVED_AS
Definition: AbstractCoreMatcher.php:35
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher
Definition: AbstractCoreMatcher.php:34
‪TYPO3\CMS\Install\ExtensionScanner\Php
Definition: CodeStatistics.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\GeneratorClassesResolver\enterNode
‪enterNode(Node $node)
Definition: GeneratorClassesResolver.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46