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\Arg; |
21
|
|
|
use PhpParser\Node\Expr\FuncCall; |
22
|
|
|
use PhpParser\Node\Name; |
23
|
|
|
use PhpParser\Node\Name\FullyQualified; |
24
|
|
|
use PhpParser\Node\Scalar\String_; |
25
|
|
|
use PhpParser\Node\Stmt\Const_; |
26
|
|
|
use PhpParser\Node\Stmt\Expression; |
27
|
|
|
use PhpParser\NodeVisitorAbstract; |
28
|
|
|
use UnexpectedValueException; |
29
|
|
|
use function count; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Replaces const declaration by define when the constant is whitelisted. |
33
|
|
|
* |
34
|
|
|
* ``` |
35
|
|
|
* const DUMMY_CONST = 'foo'; |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* => |
39
|
|
|
* |
40
|
|
|
* ``` |
41
|
|
|
* define('DUMMY_CONST', 'foo'); |
42
|
|
|
* ``` |
43
|
|
|
* |
44
|
|
|
* It does not do the prefixing. |
45
|
|
|
* |
46
|
|
|
* @private |
47
|
|
|
*/ |
48
|
|
|
final class ConstStmtReplacer extends NodeVisitorAbstract |
49
|
|
|
{ |
50
|
|
|
private $whitelist; |
51
|
|
|
private $nameResolver; |
52
|
|
|
|
53
|
549 |
|
public function __construct(Whitelist $whitelist, FullyQualifiedNameResolver $nameResolver) |
54
|
|
|
{ |
55
|
549 |
|
$this->whitelist = $whitelist; |
56
|
549 |
|
$this->nameResolver = $nameResolver; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
* |
62
|
|
|
* @param Const_ $node |
63
|
|
|
*/ |
64
|
548 |
|
public function enterNode(Node $node): Node |
65
|
|
|
{ |
66
|
548 |
|
if (false === ($node instanceof Const_)) { |
67
|
548 |
|
return $node; |
68
|
|
|
} |
69
|
|
|
|
70
|
28 |
|
foreach ($node->consts as $constant) { |
71
|
|
|
/** @var Node\Const_ $constant */ |
72
|
28 |
|
$resolvedConstantName = $this->nameResolver->resolveName( |
73
|
28 |
|
new Name( |
74
|
28 |
|
(string) $constant->name, |
75
|
28 |
|
$node->getAttributes() |
76
|
|
|
) |
77
|
28 |
|
)->getName(); |
78
|
|
|
|
79
|
28 |
|
if (false === $this->whitelist->isSymbolWhitelisted((string) $resolvedConstantName, true)) { |
80
|
23 |
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
7 |
|
if (count($node->consts) > 1) { |
84
|
1 |
|
throw new UnexpectedValueException( |
85
|
|
|
'Whitelisting a constant declared in a grouped constant statement (e.g. `const FOO = ' |
86
|
|
|
.'\'foo\', BAR = \'bar\'; is not supported. Consider breaking it down in multiple constant ' |
87
|
1 |
|
.'declaration statements' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
return new Expression( |
92
|
6 |
|
new FuncCall( |
93
|
6 |
|
new FullyQualified('define'), |
94
|
|
|
[ |
95
|
6 |
|
new Arg( |
96
|
6 |
|
new String_((string) $resolvedConstantName) |
97
|
|
|
), |
98
|
6 |
|
new Arg($constant->value), |
99
|
|
|
] |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
23 |
|
return $node; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|