|
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 PhpParser\NodeVisitor\NameResolver; |
|
18
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\FullyQualifiedNameResolver; |
|
19
|
|
|
use Humbug\PhpScoper\Whitelist; |
|
20
|
|
|
use PhpParser\Node; |
|
21
|
|
|
use PhpParser\Node\Arg; |
|
22
|
|
|
use PhpParser\Node\Expr\FuncCall; |
|
23
|
|
|
use PhpParser\Node\Name; |
|
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 const declaration by define when the constant is whitelisted. |
|
34
|
|
|
* |
|
35
|
|
|
* ``` |
|
36
|
|
|
* const DUMMY_CONST = 'foo'; |
|
37
|
|
|
* ``` |
|
38
|
|
|
* |
|
39
|
|
|
* => |
|
40
|
|
|
* |
|
41
|
|
|
* ``` |
|
42
|
|
|
* define('DUMMY_CONST', 'foo'); |
|
43
|
|
|
* ``` |
|
44
|
|
|
* |
|
45
|
|
|
* It does not do the prefixing. |
|
46
|
|
|
* |
|
47
|
|
|
* @private |
|
48
|
|
|
*/ |
|
49
|
|
|
final class ConstStmtReplacer extends NodeVisitorAbstract |
|
50
|
|
|
{ |
|
51
|
|
|
private Whitelist $whitelist; |
|
52
|
|
|
private FullyQualifiedNameResolver $nameResolver; |
|
53
|
549 |
|
private NameResolver $newNameResolver; |
|
54
|
|
|
|
|
55
|
549 |
|
public function __construct( |
|
56
|
549 |
|
Whitelist $whitelist, |
|
57
|
|
|
FullyQualifiedNameResolver $nameResolver, |
|
58
|
|
|
NameResolver $newNameResolver |
|
59
|
|
|
) { |
|
60
|
|
|
$this->whitelist = $whitelist; |
|
61
|
|
|
$this->nameResolver = $nameResolver; |
|
62
|
|
|
$this->newNameResolver = $newNameResolver; |
|
63
|
|
|
} |
|
64
|
548 |
|
|
|
65
|
|
|
/** |
|
66
|
548 |
|
* @param Const_ $node |
|
67
|
548 |
|
*/ |
|
68
|
|
|
public function enterNode(Node $node): Node |
|
69
|
|
|
{ |
|
70
|
28 |
|
if (!$node instanceof Const_) { |
|
71
|
|
|
return $node; |
|
72
|
28 |
|
} |
|
73
|
28 |
|
|
|
74
|
28 |
|
foreach ($node->consts as $constant) { |
|
75
|
28 |
|
/** @var Node\Const_ $constant */ |
|
76
|
|
|
$oldResolvedConstantName = $this->nameResolver |
|
77
|
28 |
|
->resolveName( |
|
78
|
|
|
new Name( |
|
79
|
28 |
|
(string) $constant->name, |
|
80
|
23 |
|
$node->getAttributes(), |
|
81
|
|
|
), |
|
82
|
|
|
) |
|
83
|
7 |
|
->getName(); |
|
84
|
1 |
|
$resolvedConstantName = $this->newNameResolver |
|
85
|
|
|
->getNameContext() |
|
86
|
|
|
->getResolvedName( |
|
87
|
1 |
|
new Name( |
|
88
|
|
|
(string) $constant->name, |
|
89
|
|
|
$node->getAttributes() |
|
90
|
|
|
), |
|
91
|
6 |
|
Node\Stmt\Use_::TYPE_CONSTANT |
|
92
|
6 |
|
); |
|
93
|
6 |
|
|
|
94
|
|
|
if ((string) $oldResolvedConstantName !== (string) $resolvedConstantName) { |
|
95
|
6 |
|
// TODO: check those cases if relevant |
|
96
|
6 |
|
$x = ''; |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
6 |
|
|
|
99
|
|
|
if (false === $this->whitelist->isSymbolWhitelisted((string) $resolvedConstantName, true)) { |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (count($node->consts) > 1) { |
|
104
|
23 |
|
throw new UnexpectedValueException( |
|
105
|
|
|
'Whitelisting a constant declared in a grouped constant statement (e.g. `const FOO = ' |
|
106
|
|
|
.'\'foo\', BAR = \'bar\'; is not supported. Consider breaking it down in multiple constant ' |
|
107
|
|
|
.'declaration statements' |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return new Expression( |
|
112
|
|
|
new FuncCall( |
|
113
|
|
|
new FullyQualified('define'), |
|
114
|
|
|
[ |
|
115
|
|
|
new Arg( |
|
116
|
|
|
new String_((string) $resolvedConstantName) |
|
117
|
|
|
), |
|
118
|
|
|
new Arg($constant->value), |
|
119
|
|
|
] |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $node; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|