-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
FunctionIdentifierRecorder.php
172 lines (138 loc) · 5.34 KB
/
FunctionIdentifierRecorder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Humbug\PhpScoper\PhpParser\NodeVisitor;
use Humbug\PhpScoper\PhpParser\Node\FullyQualifiedFactory;
use Humbug\PhpScoper\PhpParser\NodeVisitor\AttributeAppender\ParentNodeAppender;
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
use Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario;
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeVisitorAbstract;
/**
* Records the functions that need to be aliased.
*
* @private
*/
final class FunctionIdentifierRecorder extends NodeVisitorAbstract
{
public function __construct(
private readonly string $prefix,
private readonly IdentifierResolver $identifierResolver,
private readonly SymbolsRegistry $symbolsRegistry,
private readonly EnrichedReflector $enrichedReflector,
) {
}
public function enterNode(Node $node): Node
{
if (!($node instanceof Identifier || $node instanceof Name || $node instanceof String_)
|| !ParentNodeAppender::hasParent($node)
) {
return $node;
}
$resolvedName = $this->retrieveResolvedName($node);
if (null !== $resolvedName && $this->shouldBeAliased($node, $resolvedName)) {
$this->symbolsRegistry->recordFunction(
$resolvedName,
FullyQualifiedFactory::concat($this->prefix, $resolvedName),
);
}
return $node;
}
private function shouldBeAliased(
Node $node,
FullyQualified $resolvedName
): bool {
if ($this->enrichedReflector->isExposedFunction($resolvedName->toString())) {
return true;
}
// If is a function declaration, excluded global functions need to be
// aliased since otherwise any usage without the FQCN in a namespace
// will break. Indeed, previously it would work thanks to the function
// PHP autoloading fallback mechanism, but now that the declaration is
// namespaced because of the prefix, an alias is needed.
return self::isFunctionDeclaration($node)
&& $this->enrichedReflector->belongsToGlobalNamespace($resolvedName->toString())
&& $this->enrichedReflector->isFunctionExcluded($resolvedName->toString());
}
private function retrieveResolvedName(Node $node): ?FullyQualified
{
if ($node instanceof Identifier) {
return $this->retrieveResolvedNameForIdentifier($node);
}
if ($node instanceof Name) {
return $this->retrieveResolvedNameForFuncCall($node);
}
if ($node instanceof String_) {
return $this->retrieveResolvedNameForString($node);
}
throw UnexpectedParsingScenario::create();
}
private function retrieveResolvedNameForIdentifier(Identifier $identifier): ?FullyQualified
{
$parent = ParentNodeAppender::getParent($identifier);
if (!($parent instanceof Function_)
|| $identifier === $parent->returnType
) {
return null;
}
$resolvedName = $this->identifierResolver->resolveIdentifier($identifier);
return $resolvedName instanceof FullyQualified ? $resolvedName : null;
}
private function retrieveResolvedNameForFuncCall(Name $name): ?FullyQualified
{
$parent = ParentNodeAppender::getParent($name);
if (!($parent instanceof FuncCall)) {
return null;
}
return $name instanceof FullyQualified ? $name : null;
}
private function retrieveResolvedNameForString(String_ $string): ?FullyQualified
{
$stringParent = ParentNodeAppender::getParent($string);
if (!($stringParent instanceof Arg)) {
return null;
}
$argParent = ParentNodeAppender::getParent($stringParent);
if (!($argParent instanceof FuncCall)) {
return null;
}
if (!self::isFunctionExistsCall($argParent)) {
return null;
}
$resolvedName = $this->identifierResolver->resolveString($string);
return $resolvedName instanceof FullyQualified ? $resolvedName : null;
}
private static function isFunctionExistsCall(FuncCall $node): bool
{
$name = $node->name;
return $name instanceof Name
// PHP-Scoper assumes that it is the PHP native function.
// See limitations.md#declaring-a-custom-namespaced-function-function_exists
&& $name->toString() === 'function_exists';
}
private static function isFunctionDeclaration(Node $node): bool
{
if (!($node instanceof Identifier)) {
return false;
}
$parentNode = ParentNodeAppender::getParent($node);
return $parentNode instanceof Function_;
}
}