Completed
Push — master ( e787ce...d6ad3a )
by
unknown
18:33
created

GeneratorClassesResolver::resolveClassName()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
declare(strict_types = 1);
3
namespace TYPO3\CMS\Install\ExtensionScanner\Php;
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;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher;
28
29
/**
30
 * Create a fully qualified class name object from first argument of
31
 * GeneralUtility::makeInstance('My\\Package\\Class\\Name') if given as string
32
 * and not as My\Package\Class\Name::class language construct.
33
 *
34
 * This resolver is to be called after generic NameResolver::class, but before
35
 * other search and find visitors that implement CodeScannerInterface::class
36
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
37
 */
38
class GeneratorClassesResolver extends NodeVisitorAbstract
39
{
40
    /**
41
     * Called by PhpParser.
42
     * Create an fqdn object from first makeInstance argument if it is a String
43
     *
44
     * @param Node $node Incoming node
45
     */
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $className of type PhpParser\Node\Name\FullyQualified is incompatible with the declared type PhpParser\Node\Expr of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
}
81