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\FullyQualified; |
22
|
|
|
use PhpParser\Node\Stmt\ClassLike; |
23
|
|
|
use PhpParser\Node\Stmt\Trait_; |
24
|
|
|
use PhpParser\NodeVisitorAbstract; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Records the user classes registered in the global namespace which have been whitelisted and whitelisted classes. |
28
|
|
|
* |
29
|
|
|
* @private |
30
|
|
|
*/ |
31
|
|
|
final class ClassIdentifierRecorder extends NodeVisitorAbstract |
32
|
|
|
{ |
33
|
|
|
private $prefix; |
34
|
|
|
private $nameResolver; |
35
|
|
|
private $whitelist; |
36
|
|
|
|
37
|
10 |
|
public function __construct( |
38
|
|
|
string $prefix, |
39
|
|
|
FullyQualifiedNameResolver $nameResolver, |
40
|
|
|
Whitelist $whitelist |
41
|
|
|
) { |
42
|
10 |
|
$this->prefix = $prefix; |
43
|
10 |
|
$this->nameResolver = $nameResolver; |
44
|
10 |
|
$this->whitelist = $whitelist; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
10 |
|
public function enterNode(Node $node): Node |
51
|
|
|
{ |
52
|
10 |
|
if (false === ($node instanceof Identifier) || false === ParentNodeAppender::hasParent($node)) { |
53
|
10 |
|
return $node; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
$parent = ParentNodeAppender::getParent($node); |
57
|
|
|
|
58
|
6 |
|
if (false === ($parent instanceof ClassLike) || $parent instanceof Trait_) { |
59
|
6 |
|
return $node; |
60
|
|
|
} |
61
|
|
|
/** @var ClassLike $parent */ |
62
|
|
|
if (null === $parent->name) { |
63
|
|
|
return $node; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @var Identifier $node */ |
67
|
|
|
$resolvedName = $this->nameResolver->resolveName($node)->getName(); |
68
|
|
|
|
69
|
|
|
if (false === ($resolvedName instanceof FullyQualified)) { |
70
|
|
|
return $node; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @var FullyQualified $resolvedName */ |
74
|
|
|
if ($this->whitelist->isGlobalWhitelistedClass((string) $resolvedName) |
75
|
|
|
|| $this->whitelist->isSymbolWhitelisted((string) $resolvedName) |
76
|
|
|
) { |
77
|
|
|
$this->whitelist->recordWhitelistedClass( |
78
|
|
|
$resolvedName, |
79
|
|
|
FullyQualified::concat($this->prefix, $resolvedName) |
|
|
|
|
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $node; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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: