2
2
3
3
namespace PHPStan \Rules \Arrays ;
4
4
5
+ use PHPStan \Php \PhpVersion ;
5
6
use PHPStan \Rules \Rule ;
6
7
use PHPStan \Rules \RuleLevelHelper ;
7
8
use PHPStan \Testing \RuleTestCase ;
8
9
use PHPUnit \Framework \Attributes \RequiresPhp ;
10
+ use const PHP_VERSION_ID ;
9
11
10
12
/**
11
13
* @extends RuleTestCase<InvalidKeyInArrayItemRule>
@@ -21,50 +23,83 @@ protected function getRule(): Rule
21
23
{
22
24
$ ruleLevelHelper = new RuleLevelHelper (self ::createReflectionProvider (), true , false , true , $ this ->checkExplicitMixed , $ this ->checkImplicitMixed , false , true );
23
25
24
- return new InvalidKeyInArrayItemRule ($ ruleLevelHelper );
26
+ return new InvalidKeyInArrayItemRule (
27
+ $ ruleLevelHelper ,
28
+ self ::getContainer ()->getByType (PhpVersion::class),
29
+ );
25
30
}
26
31
27
32
public function testInvalidKey (): void
28
33
{
29
- $ this -> analyse ([ __DIR__ . ' /data/invalid-key-array-item.php ' ], [
34
+ $ errors = [
30
35
[
31
36
'Invalid array key type DateTimeImmutable. ' ,
32
- 13 ,
37
+ 12 ,
33
38
],
34
39
[
35
40
'Invalid array key type array. ' ,
36
- 14 ,
41
+ 13 ,
37
42
],
38
43
[
39
44
'Possibly invalid array key type stdClass|string. ' ,
40
- 15 ,
45
+ 14 ,
41
46
],
42
- ]);
47
+ ];
48
+
49
+ if (PHP_VERSION_ID >= 80100 ) {
50
+ $ errors [] = [
51
+ 'Invalid array key type float. ' ,
52
+ 26 ,
53
+ ];
54
+ }
55
+ if (PHP_VERSION_ID >= 80500 ) {
56
+ $ errors [] = [
57
+ 'Invalid array key type null. ' ,
58
+ 27 ,
59
+ ];
60
+ }
61
+
62
+ $ this ->analyse ([__DIR__ . '/data/invalid-key-array-item.php ' ], $ errors );
43
63
}
44
64
45
65
public function testInvalidMixedKey (): void
46
66
{
47
67
$ this ->checkExplicitMixed = true ;
48
68
$ this ->checkImplicitMixed = true ;
49
69
50
- $ this -> analyse ([ __DIR__ . ' /data/invalid-key-array-item.php ' ], [
70
+ $ errors = [
51
71
[
52
72
'Invalid array key type DateTimeImmutable. ' ,
53
- 13 ,
73
+ 12 ,
54
74
],
55
75
[
56
76
'Invalid array key type array. ' ,
57
- 14 ,
77
+ 13 ,
58
78
],
59
79
[
60
80
'Possibly invalid array key type stdClass|string. ' ,
61
- 15 ,
81
+ 14 ,
62
82
],
63
83
[
64
84
'Possibly invalid array key type mixed. ' ,
65
- 22 ,
85
+ 21 ,
66
86
],
67
- ]);
87
+ ];
88
+
89
+ if (PHP_VERSION_ID >= 80100 ) {
90
+ $ errors [] = [
91
+ 'Invalid array key type float. ' ,
92
+ 26 ,
93
+ ];
94
+ }
95
+ if (PHP_VERSION_ID >= 80500 ) {
96
+ $ errors [] = [
97
+ 'Invalid array key type null. ' ,
98
+ 27 ,
99
+ ];
100
+ }
101
+
102
+ $ this ->analyse ([__DIR__ . '/data/invalid-key-array-item.php ' ], $ errors );
68
103
}
69
104
70
105
public function testInvalidKeyInList (): void
0 commit comments