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\Symbol\EnrichedReflector; |
19
|
|
|
use PhpParser\Node; |
20
|
|
|
use PhpParser\Node\Arg; |
21
|
|
|
use PhpParser\Node\Expr; |
22
|
|
|
use PhpParser\Node\Expr\FuncCall; |
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 constants `const` declarations by `define` for exposed constants. |
33
|
|
|
* |
34
|
|
|
* ``` |
35
|
|
|
* const DUMMY_CONST = 'foo'; |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* => |
39
|
|
|
* |
40
|
|
|
* ``` |
41
|
|
|
* define('DUMMY_CONST', 'foo'); |
42
|
|
|
* ``` |
43
|
|
|
* |
44
|
|
|
* @private |
45
|
|
|
*/ |
46
|
|
|
final class ConstStmtReplacer extends NodeVisitorAbstract |
47
|
|
|
{ |
48
|
|
|
private IdentifierResolver $identifierResolver; |
49
|
|
|
private EnrichedReflector $enrichedReflector; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
IdentifierResolver $identifierResolver, |
53
|
549 |
|
EnrichedReflector $enrichedReflector |
54
|
|
|
) { |
55
|
549 |
|
$this->identifierResolver = $identifierResolver; |
56
|
549 |
|
$this->enrichedReflector = $enrichedReflector; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function enterNode(Node $node): Node |
60
|
|
|
{ |
61
|
|
|
if (!$node instanceof Const_) { |
62
|
|
|
return $node; |
63
|
|
|
} |
64
|
548 |
|
|
65
|
|
|
foreach ($node->consts as $constant) { |
66
|
548 |
|
$replacement = $this->replaceConst($node, $constant); |
67
|
548 |
|
|
68
|
|
|
if (null !== $replacement) { |
69
|
|
|
// If there is more than one constant declare in the node we |
70
|
28 |
|
// will not have a replacement (this case is not supported) |
71
|
|
|
// hence the return statement is safe here. |
72
|
28 |
|
return $replacement; |
73
|
28 |
|
} |
74
|
28 |
|
} |
75
|
28 |
|
|
76
|
|
|
return $node; |
77
|
28 |
|
} |
78
|
|
|
|
79
|
28 |
|
private function replaceConst(Const_ $const, Node\Const_ $constant): ?Node |
80
|
23 |
|
{ |
81
|
|
|
$resolvedConstantName = $this->identifierResolver->resolveIdentifier( |
82
|
|
|
$constant->name, |
83
|
7 |
|
); |
84
|
1 |
|
|
85
|
|
|
if (!$this->enrichedReflector->isExposedConstant((string) $resolvedConstantName)) { |
86
|
|
|
// No replacement |
87
|
1 |
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (count($const->consts) > 1) { |
91
|
6 |
|
throw new UnexpectedValueException( |
92
|
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', |
93
|
6 |
|
); |
94
|
|
|
} |
95
|
6 |
|
|
96
|
6 |
|
return self::createConstDefineNode( |
97
|
|
|
(string) $resolvedConstantName, |
98
|
6 |
|
$constant->value, |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private static function createConstDefineNode(string $name, Expr $value): Node |
103
|
|
|
{ |
104
|
23 |
|
return new Expression( |
105
|
|
|
new FuncCall( |
106
|
|
|
new FullyQualified('define'), |
107
|
|
|
[ |
108
|
|
|
new Arg( |
109
|
|
|
new String_($name) |
110
|
|
|
), |
111
|
|
|
new Arg($value), |
112
|
|
|
], |
113
|
|
|
), |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|