2
2
3
3
namespace PHPStan \Rules \Classes ;
4
4
5
+ use PHPStan \Php \PhpVersion ;
5
6
use PHPStan \Rules \ClassCaseSensitivityCheck ;
6
7
use PHPStan \Rules \Rule ;
7
8
use PHPStan \Rules \RuleLevelHelper ;
12
13
class ClassConstantRuleTest extends \PHPStan \Testing \RuleTestCase
13
14
{
14
15
16
+ /** @var int */
17
+ private $ phpVersion ;
18
+
15
19
protected function getRule (): Rule
16
20
{
17
21
$ broker = $ this ->createReflectionProvider ();
18
- return new ClassConstantRule ($ broker , new RuleLevelHelper ($ broker , true , false , true , false ), new ClassCaseSensitivityCheck ($ broker ));
22
+ return new ClassConstantRule ($ broker , new RuleLevelHelper ($ broker , true , false , true , false ), new ClassCaseSensitivityCheck ($ broker ), new PhpVersion ( $ this -> phpVersion ) );
19
23
}
20
24
21
25
public function testClassConstant (): void
22
26
{
27
+ $ this ->phpVersion = PHP_VERSION_ID ;
23
28
$ this ->analyse (
24
29
[
25
30
__DIR__ . '/data/class-constant.php ' ,
@@ -85,6 +90,8 @@ public function testClassConstantVisibility(): void
85
90
if (!self ::$ useStaticReflectionProvider && PHP_VERSION_ID >= 70400 ) {
86
91
$ this ->markTestSkipped ('Test does not run on PHP 7.4 because of referencing parent:: without parent class. ' );
87
92
}
93
+
94
+ $ this ->phpVersion = PHP_VERSION_ID ;
88
95
$ this ->analyse ([__DIR__ . '/data/class-constant-visibility.php ' ], [
89
96
[
90
97
'Access to private constant PRIVATE_BAR of class ClassConstantVisibility\Bar. ' ,
@@ -149,6 +156,7 @@ public function testClassConstantVisibility(): void
149
156
150
157
public function testClassExists (): void
151
158
{
159
+ $ this ->phpVersion = PHP_VERSION_ID ;
152
160
$ this ->analyse ([__DIR__ . '/data/class-exists.php ' ], [
153
161
[
154
162
'Class UnknownClass\Bar not found. ' ,
@@ -168,4 +176,62 @@ public function testClassExists(): void
168
176
]);
169
177
}
170
178
179
+ public function dataClassConstantOnExpression (): array
180
+ {
181
+ return [
182
+ [
183
+ 70400 ,
184
+ [
185
+ [
186
+ 'Accessing ::class constant on an expression is supported only on PHP 8.0 and later. ' ,
187
+ 15 ,
188
+ ],
189
+ [
190
+ 'Accessing ::class constant on an expression is supported only on PHP 8.0 and later. ' ,
191
+ 16 ,
192
+ ],
193
+ [
194
+ 'Accessing ::class constant on an expression is supported only on PHP 8.0 and later. ' ,
195
+ 17 ,
196
+ ],
197
+ [
198
+ 'Accessing ::class constant on an expression is supported only on PHP 8.0 and later. ' ,
199
+ 18 ,
200
+ ],
201
+ ],
202
+ ],
203
+ [
204
+ 80000 ,
205
+ [
206
+ [
207
+ 'Accessing ::class constant on a dynamic string is not supported in PHP. ' ,
208
+ 16 ,
209
+ ],
210
+ [
211
+ 'Cannot access constant class on stdClass|null. ' ,
212
+ 17 ,
213
+ ],
214
+ [
215
+ 'Cannot access constant class on string|null. ' ,
216
+ 18 ,
217
+ ],
218
+ ],
219
+ ],
220
+ ];
221
+ }
222
+
223
+ /**
224
+ * @dataProvider dataClassConstantOnExpression
225
+ * @param int $phpVersion
226
+ * @param mixed[] $errors
227
+ */
228
+ public function testClassConstantOnExpression (int $ phpVersion , array $ errors ): void
229
+ {
230
+ if (!self ::$ useStaticReflectionProvider ) {
231
+ $ this ->markTestSkipped ('Test requires static reflection ' );
232
+ }
233
+ $ this ->phpVersion = $ phpVersion ;
234
+ $ this ->analyse ([__DIR__ . '/data/class-constant-on-expr.php ' ], $ errors );
235
+ }
236
+
171
237
}
0 commit comments