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\Whitelist; |
19
|
|
|
use PhpParser\Node; |
20
|
|
|
use PhpParser\Node\Identifier; |
21
|
|
|
use PhpParser\Node\Name; |
22
|
|
|
use PhpParser\Node\Name\FullyQualified; |
23
|
|
|
use PhpParser\Node\Stmt\Function_; |
24
|
|
|
use PhpParser\NodeVisitorAbstract; |
25
|
|
|
use function count; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Records the user functions registered in the global namespace. |
29
|
|
|
* |
30
|
|
|
* @private |
31
|
|
|
*/ |
32
|
|
|
final class FunctionIdentifierRecorder extends NodeVisitorAbstract |
33
|
|
|
{ |
34
|
|
|
private $prefix; |
35
|
|
|
private $nameResolver; |
36
|
|
|
private $whitelist; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
string $prefix, |
40
|
|
|
FullyQualifiedNameResolver $nameResolver, |
41
|
|
|
Whitelist $whitelist |
42
|
|
|
) { |
43
|
|
|
$this->prefix = $prefix; |
44
|
|
|
$this->nameResolver = $nameResolver; |
45
|
|
|
$this->whitelist = $whitelist; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function enterNode(Node $node): Node |
52
|
|
|
{ |
53
|
|
|
if (false === ($node instanceof Identifier) || false === AppendParentNode::hasParent($node)) { |
54
|
|
|
return $node; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$parent = AppendParentNode::getParent($node); |
58
|
|
|
|
59
|
|
|
if (false === ($parent instanceof Function_)) { |
60
|
|
|
return $node; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @var Identifier $node */ |
64
|
|
|
$resolvedValue = $this->nameResolver->resolveName( |
65
|
|
|
new Name( |
66
|
|
|
$node->name, |
67
|
|
|
$node->getAttributes() |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
$resolvedName = $resolvedValue->getName(); |
71
|
|
|
|
72
|
|
|
if (null !== $resolvedValue->getNamespace() |
73
|
|
|
|| false === ($resolvedName instanceof FullyQualified) |
74
|
|
|
|| count($resolvedName->parts) > 1 |
75
|
|
|
) { |
76
|
|
|
return $node; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* @var FullyQualified $resolvedName */ |
80
|
|
|
|
81
|
|
|
$this->whitelist->recordUserGlobalFunction( |
82
|
|
|
$resolvedName, |
83
|
|
|
FullyQualified::concat($this->prefix, $resolvedName) |
|
|
|
|
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
return $node; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: