forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensitiveConstantNameRector.php
More file actions
171 lines (154 loc) · 4.31 KB
/
SensitiveConstantNameRector.php
File metadata and controls
171 lines (154 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace Rector\Php73\Rector\ConstFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\DeprecatedAtVersionInterface;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php73\Rector\ConstFetch\SensitiveConstantNameRector\SensitiveConstantNameRectorTest
*/
final class SensitiveConstantNameRector extends AbstractRector implements MinPhpVersionInterface, DeprecatedAtVersionInterface
{
/**
* @see http://php.net/manual/en/reserved.constants.php
* @var string[]
*/
private const array PHP_RESERVED_CONSTANTS = [
'PHP_VERSION',
'PHP_MAJOR_VERSION',
'PHP_MINOR_VERSION',
'PHP_RELEASE_VERSION',
'PHP_VERSION_ID',
'PHP_EXTRA_VERSION',
'PHP_ZTS',
'PHP_DEBUG',
'PHP_MAXPATHLEN',
'PHP_OS',
'PHP_OS_FAMILY',
'PHP_SAPI',
'PHP_EOL',
'PHP_INT_MAX',
'PHP_INT_MIN',
'PHP_INT_SIZE',
'PHP_FLOAT_DIG',
'PHP_FLOAT_EPSILON',
'PHP_FLOAT_MIN',
'PHP_FLOAT_MAX',
'DEFAULT_INCLUDE_PATH',
'PEAR_INSTALL_DIR',
'PEAR_EXTENSION_DIR',
'PHP_EXTENSION_DIR',
'PHP_PREFIX',
'PHP_BINDIR',
'PHP_BINARY',
'PHP_MANDIR',
'PHP_LIBDIR',
'PHP_DATADIR',
'PHP_SYSCONFDIR',
'PHP_LOCALSTATEDIR',
'PHP_CONFIG_FILE_PATH',
'PHP_CONFIG_FILE_SCAN_DIR',
'PHP_SHLIB_SUFFIX',
'PHP_FD_SETSIZE',
'E_ERROR',
'E_WARNING',
'E_PARSE',
'E_NOTICE',
'E_CORE_ERROR',
'E_CORE_WARNING',
'E_COMPILE_ERROR',
'E_COMPILE_WARNING',
'E_USER_ERROR',
'E_USER_WARNING',
'E_USER_NOTICE',
'E_RECOVERABLE_ERROR',
'E_DEPRECATED',
'E_USER_DEPRECATED',
'E_ALL',
'E_STRICT',
'__COMPILER_HALT_OFFSET__',
'TRUE',
'FALSE',
'NULL',
];
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::NAMESPACES;
}
public function provideDeprecatedAtVersion(): int
{
return PhpVersionFeature::DEPRECATE_INSENSITIVE_CONSTANT_NAME;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change case insensitive constants to sensitive ones',
[
new CodeSample(
<<<'CODE_SAMPLE'
define('FOO', 42, true);
var_dump(FOO);
var_dump(foo);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
define('FOO', 42, true);
var_dump(FOO);
var_dump(FOO);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ConstFetch::class];
}
/**
* @param ConstFetch $node
*/
public function refactor(Node $node): ?Node
{
$constantName = $this->getName($node);
if ($constantName === null) {
return null;
}
$uppercasedConstantName = strtoupper($constantName);
// is system constant?
if (in_array($uppercasedConstantName, self::PHP_RESERVED_CONSTANTS, true)) {
return null;
}
// constant is defined in current lower/upper case
if ($this->reflectionProvider->hasConstant(new Name($constantName), null)) {
return null;
}
// is uppercase, all good
if ($constantName === $uppercasedConstantName) {
return null;
}
if (
str_contains($uppercasedConstantName, '\\')
|| str_contains($uppercasedConstantName, '(')
|| str_contains($uppercasedConstantName, "'")
) {
return null;
}
$node->name = new FullyQualified($uppercasedConstantName);
return $node;
}
}