Completed
Push — master ( 5e59d9...c7bab4 )
by Théo
06:51 queued 10s
created

NameStmtPrefixer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
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\NodeVisitor;
16
17
use Humbug\PhpScoper\NodeVisitor\Resolver\FullyQualifiedNameResolver;
18
use Humbug\PhpScoper\Reflector;
19
use PhpParser\Node;
20
use PhpParser\Node\Expr\ClassConstFetch;
21
use PhpParser\Node\Expr\ConstFetch;
22
use PhpParser\Node\Expr\FuncCall;
23
use PhpParser\Node\Expr\Instanceof_;
24
use PhpParser\Node\Expr\New_;
25
use PhpParser\Node\Expr\StaticCall;
26
use PhpParser\Node\Name;
27
use PhpParser\Node\Name\FullyQualified;
28
use PhpParser\Node\NullableType;
29
use PhpParser\Node\Param;
30
use PhpParser\Node\Stmt\Catch_;
31
use PhpParser\Node\Stmt\Class_;
32
use PhpParser\Node\Stmt\ClassMethod;
33
use PhpParser\Node\Stmt\Function_;
34
use PhpParser\Node\Stmt\Interface_;
35
use PhpParser\NodeVisitorAbstract;
36
use function in_array;
37
38
/**
39
 * ```
40
 * new Foo\Bar();
41
 * ```.
42
 *
43
 * =>
44
 *
45
 * ```
46
 * new \Humbug\Foo\Bar();
47
 * ```
48
 *
49
 * @private
50
 */
51
final class NameStmtPrefixer extends NodeVisitorAbstract
52
{
53
    public const PHP_FUNCTION_KEYWORDS = [
54
        'self',
55
        'static',
56
        'parent',
57
    ];
58
59
    private $prefix;
60
    private $nameResolver;
61
    private $reflector;
62
63
    /**
64
     * @param string                     $prefix
65
     * @param FullyQualifiedNameResolver $nameResolver
66
     * @param Reflector                  $reflector
67
     */
68 367
    public function __construct(
69
        string $prefix,
70
        FullyQualifiedNameResolver $nameResolver,
71
        Reflector $reflector
72
    ) {
73 367
        $this->prefix = $prefix;
74 367
        $this->nameResolver = $nameResolver;
75 367
        $this->reflector = $reflector;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 366
    public function enterNode(Node $node): Node
82
    {
83 366
        return ($node instanceof Name && AppendParentNode::hasParent($node))
84 364
            ? $this->prefixName($node)
85 366
            : $node
86
        ;
87
    }
88
89 364
    private function prefixName(Name $name): Node
90
    {
91 364
        $parentNode = AppendParentNode::getParent($name);
92
93 364
        if ($parentNode instanceof NullableType) {
94 4
            if (false === AppendParentNode::hasParent($parentNode)) {
95
                return $name;
96
            }
97
98 4
            $parentNode = AppendParentNode::getParent($parentNode);
99
        }
100
101
        if (false === (
102 364
                $parentNode instanceof ConstFetch
103 364
                || $parentNode instanceof ClassConstFetch
104 364
                || $parentNode instanceof Param
105 364
                || $parentNode instanceof FuncCall
106 364
                || $parentNode instanceof StaticCall
107 364
                || $parentNode instanceof Function_
108 364
                || $parentNode instanceof ClassMethod
109 364
                || $parentNode instanceof New_
110 364
                || $parentNode instanceof Class_
111 364
                || $parentNode instanceof Interface_
112 364
                || $parentNode instanceof Catch_
113 364
                || $parentNode instanceof Instanceof_
114
            )
115
        ) {
116 364
            return $name;
117
        }
118
119
        if (
120
            (
121 273
                $parentNode instanceof FuncCall
122 273
                || $parentNode instanceof StaticCall
123 273
                || $parentNode instanceof ClassConstFetch
124 273
                || $parentNode instanceof New_
125 273
                || $parentNode instanceof Param
126 273
                || $parentNode instanceof Catch_
127 273
                || $parentNode instanceof Instanceof_
128
            )
129 273
            && in_array((string) $name, self::PHP_FUNCTION_KEYWORDS, true)
130
        ) {
131 8
            return $name;
132
        }
133
134 271
        if ($parentNode instanceof ConstFetch && 'null' === (string) $name) {
135 2
            return $name;
136
        }
137
138 271
        $resolvedValue = $this->nameResolver->resolveName($name);
139
140 271
        $resolvedName = $resolvedValue->getName();
141
142
        // Skip if is already prefixed
143 271
        if ($this->prefix === $resolvedName->getFirst()) {
144 4
            return $resolvedName;
145
        }
146
147
        // Check if the class can be prefixed
148 271
        if (false === ($parentNode instanceof ConstFetch || $parentNode instanceof FuncCall)) {
149 188
            if ($this->reflector->isClassInternal($resolvedName->toString())) {
150 45
                return $resolvedName;
151
            }
152
        }
153
154 243
        if ($parentNode instanceof ConstFetch
155
            && (
156 97
                $this->reflector->isConstantInternal($resolvedName->toString())
157
                // Constants have a fallback autoloading so we cannot prefix them when the name is ambiguous
158
                // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
159 243
                || false === ($resolvedName instanceof FullyQualified)
160
            )
161
        ) {
162 65
            return $resolvedName;
163
        }
164
165
        if (
166 234
            $parentNode instanceof FuncCall
167
            && (
168 88
                $this->reflector->isFunctionInternal($resolvedName->toString())
169
                // Functions have a fallback autoloading so we cannot prefix them when the name is ambiguous
170
                // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
171 234
                || false === ($resolvedName instanceof FullyQualified)
172
            )
173
        ) {
174 58
            return $resolvedName;
175
        }
176
177 221
        if ('self' === (string) $resolvedName && $parentNode instanceof ClassMethod) {
178 3
            return $name;
179
        }
180
181 221
        return FullyQualified::concat(
182 221
            $this->prefix,
183 221
            $resolvedName->toString(),
184 221
            $resolvedName->getAttributes()
185
        );
186
    }
187
}
188