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\Expr\FuncCall; |
21
|
|
|
use PhpParser\Node\Name; |
22
|
|
|
use PhpParser\Node\Name\FullyQualified; |
23
|
|
|
use PhpParser\Node\Param; |
24
|
|
|
use PhpParser\Node\Scalar\String_; |
25
|
|
|
use PhpParser\Node\Stmt\Expression; |
26
|
|
|
use PhpParser\NodeVisitorAbstract; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Replaces const declaration by define. |
30
|
|
|
* |
31
|
|
|
* ``` |
32
|
|
|
* const DUMMY_CONST = 'foo'; |
33
|
|
|
* ``` |
34
|
|
|
* |
35
|
|
|
* => |
36
|
|
|
* |
37
|
|
|
* ``` |
38
|
|
|
* define('DUMMY_CONST', 'foo'); |
39
|
|
|
* ``` |
40
|
|
|
* |
41
|
|
|
* @private |
42
|
|
|
*/ |
43
|
|
|
final class ConstStmtReplacer extends NodeVisitorAbstract |
44
|
|
|
{ |
45
|
|
|
private $whitelist; |
46
|
|
|
private $nameResolver; |
47
|
|
|
|
48
|
|
|
public function __construct(Whitelist $whitelist, FullyQualifiedNameResolver $nameResolver) |
49
|
|
|
{ |
50
|
|
|
$this->whitelist = $whitelist; |
51
|
|
|
$this->nameResolver = $nameResolver; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
* |
57
|
|
|
* @param Node\Stmt\Const_ $node |
58
|
|
|
*/ |
59
|
|
|
public function enterNode(Node $node): Node |
60
|
|
|
{ |
61
|
|
|
if (false === ($node instanceof Node\Stmt\Const_)) { |
62
|
|
|
return $node; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// TODO: check this: when can Node\Stmt\Const_ be empty or have more than one constant |
66
|
|
|
/** @var Node\Const_ $constant */ |
67
|
|
|
$constant = current($node->consts); |
68
|
|
|
|
69
|
|
|
$resolvedConstantName = $this->nameResolver->resolveName( |
70
|
|
|
new Name( |
71
|
|
|
(string) $constant->name, |
72
|
|
|
$node->getAttributes() // Take the parent node attribute since no "parent" attribute is recorded in |
73
|
|
|
// Node\Const_ |
74
|
|
|
// TODO: check with nikic if this is expected |
75
|
|
|
) |
76
|
|
|
)->getName(); |
77
|
|
|
|
78
|
|
|
if (false === $this->whitelist->isClassWhitelisted((string) $resolvedConstantName)) { |
79
|
|
|
return $node; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new Expression( |
83
|
|
|
new FuncCall( |
84
|
|
|
new FullyQualified('define'), |
85
|
|
|
[ |
86
|
|
|
new String_((string) $resolvedConstantName), |
87
|
|
|
$constant->value, |
88
|
|
|
] |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|