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\IdentifierResolver; |
18
|
|
|
use Humbug\PhpScoper\Reflector; |
19
|
|
|
use Humbug\PhpScoper\Whitelist; |
20
|
|
|
use PhpParser\Node; |
21
|
|
|
use PhpParser\Node\Arg; |
22
|
|
|
use PhpParser\Node\Expr; |
23
|
|
|
use PhpParser\Node\Expr\FuncCall; |
24
|
|
|
use PhpParser\Node\Name\FullyQualified; |
25
|
|
|
use PhpParser\Node\Scalar\String_; |
26
|
|
|
use PhpParser\Node\Stmt\Const_; |
27
|
|
|
use PhpParser\Node\Stmt\Expression; |
28
|
|
|
use PhpParser\NodeVisitorAbstract; |
29
|
|
|
use UnexpectedValueException; |
30
|
|
|
use function count; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Replaces constants `const` declarations by `define` for exposed constants. |
34
|
|
|
* |
35
|
|
|
* ``` |
36
|
|
|
* const DUMMY_CONST = 'foo'; |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* => |
40
|
|
|
* |
41
|
|
|
* ``` |
42
|
|
|
* define('DUMMY_CONST', 'foo'); |
43
|
|
|
* ``` |
44
|
|
|
* |
45
|
|
|
* @private |
46
|
|
|
*/ |
47
|
|
|
final class ConstStmtReplacer extends NodeVisitorAbstract |
48
|
|
|
{ |
49
|
|
|
private Whitelist $whitelist; |
50
|
|
|
private IdentifierResolver $identifierResolver; |
51
|
|
|
private Reflector $reflector; |
52
|
|
|
|
53
|
549 |
|
public function __construct( |
54
|
|
|
Whitelist $whitelist, |
55
|
549 |
|
IdentifierResolver $identifierResolver, |
56
|
549 |
|
Reflector $reflector |
57
|
|
|
) { |
58
|
|
|
$this->whitelist = $whitelist; |
59
|
|
|
$this->identifierResolver = $identifierResolver; |
60
|
|
|
$this->reflector = $reflector; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function enterNode(Node $node): Node |
64
|
548 |
|
{ |
65
|
|
|
if (!$node instanceof Const_) { |
66
|
548 |
|
return $node; |
67
|
548 |
|
} |
68
|
|
|
|
69
|
|
|
foreach ($node->consts as $constant) { |
70
|
28 |
|
$replacement = $this->replaceConst($node, $constant); |
71
|
|
|
|
72
|
28 |
|
if (null !== $replacement) { |
73
|
28 |
|
// If there is more than one constant declare in the node we |
74
|
28 |
|
// will not have a replacement (this case is not supported) |
75
|
28 |
|
// hence the return statement is safe here. |
76
|
|
|
return $replacement; |
77
|
28 |
|
} |
78
|
|
|
} |
79
|
28 |
|
|
80
|
23 |
|
return $node; |
81
|
|
|
} |
82
|
|
|
|
83
|
7 |
|
private function replaceConst(Const_ $const, Node\Const_ $constant): ?Node |
84
|
1 |
|
{ |
85
|
|
|
$resolvedConstantName = $this->identifierResolver->resolveIdentifier( |
86
|
|
|
$constant->name, |
87
|
1 |
|
); |
88
|
|
|
|
89
|
|
|
if (!$this->isExposedConstant((string) $resolvedConstantName)) { |
90
|
|
|
// No replacement |
91
|
6 |
|
return null; |
92
|
6 |
|
} |
93
|
6 |
|
|
94
|
|
|
if (count($const->consts) > 1) { |
95
|
6 |
|
throw new UnexpectedValueException( |
96
|
6 |
|
'Exposing a constant declared in a grouped constant statement (e.g. `const FOO = \'foo\', BAR = \'bar\'; is not supported. Consider breaking it down in multiple constant declaration statements', |
97
|
|
|
); |
98
|
6 |
|
} |
99
|
|
|
|
100
|
|
|
return self::createConstDefineNode( |
101
|
|
|
(string) $resolvedConstantName, |
102
|
|
|
$constant->value, |
103
|
|
|
); |
104
|
23 |
|
} |
105
|
|
|
|
106
|
|
|
private static function createConstDefineNode(string $name, Expr $value): Node |
107
|
|
|
{ |
108
|
|
|
return new Expression( |
109
|
|
|
new FuncCall( |
110
|
|
|
new FullyQualified('define'), |
111
|
|
|
[ |
112
|
|
|
new Arg( |
113
|
|
|
new String_($name) |
114
|
|
|
), |
115
|
|
|
new Arg($value), |
116
|
|
|
], |
117
|
|
|
), |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function isExposedConstant(string $name): bool |
122
|
|
|
{ |
123
|
|
|
// Special case: internal constants must be treated as exposed symbols. |
124
|
|
|
// |
125
|
|
|
// Example: when declaring a new internal constant for compatibility |
126
|
|
|
// reasons, it must remain un-prefixed. |
127
|
|
|
return $this->reflector->isConstantInternal($name) |
128
|
|
|
|| $this->whitelist->isExposedConstantFromGlobalNamespace($name) |
129
|
|
|
|| $this->whitelist->isSymbolExposed($name, true); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|