22
33namespace PHPStan \Rules \Arrays ;
44
5+ use PHPStan \Php \PhpVersion ;
56use PHPStan \Rules \Rule ;
67use PHPStan \Rules \RuleLevelHelper ;
78use PHPStan \Testing \RuleTestCase ;
89use PHPUnit \Framework \Attributes \RequiresPhp ;
10+ use const PHP_VERSION_ID ;
911
1012/**
1113 * @extends RuleTestCase<InvalidKeyInArrayItemRule>
@@ -21,50 +23,83 @@ protected function getRule(): Rule
2123 {
2224 $ ruleLevelHelper = new RuleLevelHelper (self ::createReflectionProvider (), true , false , true , $ this ->checkExplicitMixed , $ this ->checkImplicitMixed , false , true );
2325
24- return new InvalidKeyInArrayItemRule ($ ruleLevelHelper );
26+ return new InvalidKeyInArrayItemRule (
27+ $ ruleLevelHelper ,
28+ self ::getContainer ()->getByType (PhpVersion::class),
29+ );
2530 }
2631
2732 public function testInvalidKey (): void
2833 {
29- $ this -> analyse ([ __DIR__ . ' /data/invalid-key-array-item.php ' ], [
34+ $ errors = [
3035 [
3136 'Invalid array key type DateTimeImmutable. ' ,
32- 13 ,
37+ 12 ,
3338 ],
3439 [
3540 'Invalid array key type array. ' ,
36- 14 ,
41+ 13 ,
3742 ],
3843 [
3944 'Possibly invalid array key type stdClass|string. ' ,
40- 15 ,
45+ 14 ,
4146 ],
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 );
4363 }
4464
4565 public function testInvalidMixedKey (): void
4666 {
4767 $ this ->checkExplicitMixed = true ;
4868 $ this ->checkImplicitMixed = true ;
4969
50- $ this -> analyse ([ __DIR__ . ' /data/invalid-key-array-item.php ' ], [
70+ $ errors = [
5171 [
5272 'Invalid array key type DateTimeImmutable. ' ,
53- 13 ,
73+ 12 ,
5474 ],
5575 [
5676 'Invalid array key type array. ' ,
57- 14 ,
77+ 13 ,
5878 ],
5979 [
6080 'Possibly invalid array key type stdClass|string. ' ,
61- 15 ,
81+ 14 ,
6282 ],
6383 [
6484 'Possibly invalid array key type mixed. ' ,
65- 22 ,
85+ 21 ,
6686 ],
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 );
68103 }
69104
70105 public function testInvalidKeyInList (): void
0 commit comments