Passed
Push — master ( 157651...e7eb4b )
by Théo
03:29
created

NameStmtPrefixer::enterNode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\PhpParser\NodeVisitor;
16
17
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\FullyQualifiedNameResolver;
18
use Humbug\PhpScoper\Reflector;
19
use Humbug\PhpScoper\Whitelist;
20
use PhpParser\Node;
21
use PhpParser\Node\Expr\ClassConstFetch;
22
use PhpParser\Node\Expr\ConstFetch;
23
use PhpParser\Node\Expr\FuncCall;
24
use PhpParser\Node\Expr\Instanceof_;
25
use PhpParser\Node\Expr\New_;
26
use PhpParser\Node\Expr\StaticCall;
27
use PhpParser\Node\Expr\StaticPropertyFetch;
28
use PhpParser\Node\Name;
29
use PhpParser\Node\Name\FullyQualified;
30
use PhpParser\Node\NullableType;
31
use PhpParser\Node\Param;
32
use PhpParser\Node\Stmt\Catch_;
33
use PhpParser\Node\Stmt\Class_;
34
use PhpParser\Node\Stmt\ClassMethod;
35
use PhpParser\Node\Stmt\Function_;
36
use PhpParser\Node\Stmt\Interface_;
37
use PhpParser\NodeVisitorAbstract;
38
use function in_array;
39
40
/**
41
 * Prefixes names when appropriate.
42
 *
43
 * ```
44
 * new Foo\Bar();
45
 * ```.
46
 *
47
 * =>
48
 *
49
 * ```
50
 * new \Humbug\Foo\Bar();
51
 * ```
52
 *
53
 * @private
54
 */
55
final class NameStmtPrefixer extends NodeVisitorAbstract
56
{
57
    public const PHP_FUNCTION_KEYWORDS = [
58
        'self',
59
        'static',
60
        'parent',
61
    ];
62
63
    private $prefix;
64
    private $whitelist;
65
    private $nameResolver;
66
    private $reflector;
67
68 466
    public function __construct(
69
        string $prefix,
70
        Whitelist $whitelist,
71
        FullyQualifiedNameResolver $nameResolver,
72
        Reflector $reflector
73
    ) {
74 466
        $this->prefix = $prefix;
75 466
        $this->whitelist = $whitelist;
76 466
        $this->nameResolver = $nameResolver;
77 466
        $this->reflector = $reflector;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 465
    public function enterNode(Node $node): Node
84
    {
85 465
        return ($node instanceof Name && ParentNodeAppender::hasParent($node))
86 463
            ? $this->prefixName($node)
87 465
            : $node
88
        ;
89
    }
90
91 463
    private function prefixName(Name $name): Node
92
    {
93 463
        $parentNode = ParentNodeAppender::getParent($name);
94
95 463
        if ($parentNode instanceof NullableType) {
96 4
            if (false === ParentNodeAppender::hasParent($parentNode)) {
97
                return $name;
98
            }
99
100 4
            $parentNode = ParentNodeAppender::getParent($parentNode);
101
        }
102
103
        if (false === (
104 463
                $parentNode instanceof ConstFetch
105 463
                || $parentNode instanceof ClassConstFetch
106 463
                || $parentNode instanceof StaticPropertyFetch
107 463
                || $parentNode instanceof Param
108 463
                || $parentNode instanceof FuncCall
109 463
                || $parentNode instanceof StaticCall
110 463
                || $parentNode instanceof Function_
111 463
                || $parentNode instanceof ClassMethod
112 463
                || $parentNode instanceof New_
113 463
                || $parentNode instanceof Class_
114 463
                || $parentNode instanceof Interface_
115 463
                || $parentNode instanceof Catch_
116 463
                || $parentNode instanceof Instanceof_
117
            )
118
        ) {
119 456
            return $name;
120
        }
121
122
        if (
123
            (
124 369
                $parentNode instanceof FuncCall
125 369
                || $parentNode instanceof StaticCall
126 369
                || $parentNode instanceof ClassConstFetch
127 369
                || $parentNode instanceof StaticPropertyFetch
128 369
                || $parentNode instanceof New_
129 369
                || $parentNode instanceof Param
130 369
                || $parentNode instanceof Catch_
131 369
                || $parentNode instanceof Instanceof_
132
            )
133 369
            && in_array((string) $name, self::PHP_FUNCTION_KEYWORDS, true)
134
        ) {
135 9
            return $name;
136
        }
137
138 367
        if ($parentNode instanceof ConstFetch && 'null' === (string) $name) {
139 4
            return $name;
140
        }
141
142 367
        $resolvedValue = $this->nameResolver->resolveName($name);
143
144 367
        $resolvedName = $resolvedValue->getName();
145
146 367
        if ($this->prefix === $resolvedName->getFirst() // Skip if is already prefixed
147 367
            || $this->whitelist->belongsToWhitelistedNamespace((string) $resolvedName)  // Skip if the namespace node is whitelisted
148
        ) {
149 22
            return $resolvedName;
150
        }
151
152
        // Check if the class can be prefixed
153 357
        if (false === ($parentNode instanceof ConstFetch || $parentNode instanceof FuncCall)
154 357
            && $this->reflector->isClassInternal($resolvedName->toString())
155
        ) {
156 49
            return $resolvedName;
157
        }
158
159 320
        if ($parentNode instanceof ConstFetch) {
160 80
            if ($this->whitelist->isSymbolWhitelisted($resolvedName->toString(), true)) {
161 21
                return $resolvedName;
162
            }
163
164 62
            if ($this->reflector->isConstantInternal($resolvedName->toString())) {
165 14
                return new FullyQualified($resolvedName->toString(), $resolvedName->getAttributes());
166
            }
167
168
            // Constants have an autoloading fallback so we cannot prefix them when the name is ambiguous
169
            // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
170 48
            if (false === ($resolvedName instanceof FullyQualified)) {
171 8
                return $resolvedName;
172
            }
173
174 46
            if ($this->whitelist->isGlobalWhitelistedConstant((string) $resolvedName)) {
175
                // Unlike classes & functions, whitelisted are not prefixed with aliases registered in scoper-autoload.php
176 11
                return new FullyQualified($resolvedName->toString(), $resolvedName->getAttributes());
177
            }
178
179
            // Continue
180
        }
181
182
        // Functions have a fallback autoloading so we cannot prefix them when the name is ambiguous
183
        // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
184 289
        if ($parentNode instanceof FuncCall) {
185 60
            if ($this->reflector->isFunctionInternal($resolvedName->toString())) {
186 30
                return new FullyQualified($resolvedName->toString(), $resolvedName->getAttributes());
187
            }
188
189 45
            if (false === ($resolvedName instanceof FullyQualified)) {
190 8
                return $resolvedName;
191
            }
192
        }
193
194 273
        if ('self' === (string) $resolvedName && $parentNode instanceof ClassMethod) {
195 3
            return $name;
196
        }
197
198 273
        return FullyQualified::concat(
199 273
            $this->prefix,
200 273
            $resolvedName->toString(),
201 273
            $resolvedName->getAttributes()
202
        );
203
    }
204
}
205