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