4
4
5
5
use PhpParser \Node ;
6
6
use PHPStan \Analyser \Scope ;
7
- use PHPStan \DependencyInjection \AutowiredParameter ;
8
7
use PHPStan \DependencyInjection \RegisteredRule ;
9
8
use PHPStan \Rules \Rule ;
10
9
use PHPStan \Rules \RuleErrorBuilder ;
11
- use PHPStan \Type \MixedType ;
10
+ use PHPStan \Rules \RuleLevelHelper ;
11
+ use PHPStan \Type \ErrorType ;
12
+ use PHPStan \Type \Type ;
12
13
use PHPStan \Type \VerbosityLevel ;
13
14
use function sprintf ;
14
15
@@ -20,8 +21,7 @@ final class InvalidKeyInArrayItemRule implements Rule
20
21
{
21
22
22
23
public function __construct (
23
- #[AutowiredParameter]
24
- private bool $ reportMaybes ,
24
+ private RuleLevelHelper $ ruleLevelHelper ,
25
25
)
26
26
{
27
27
}
@@ -37,23 +37,28 @@ public function processNode(Node $node, Scope $scope): array
37
37
return [];
38
38
}
39
39
40
- $ dimensionType = $ scope ->getType ($ node ->key );
40
+ $ dimensionType = $ this ->ruleLevelHelper ->findTypeToCheck (
41
+ $ scope ,
42
+ $ node ->key ,
43
+ '' ,
44
+ static fn (Type $ dimType ): bool => AllowedArrayKeysTypes::getType ()->isSuperTypeOf ($ dimType )->yes (),
45
+ )->getType ();
46
+ if ($ dimensionType instanceof ErrorType) {
47
+ return [];
48
+ }
49
+
41
50
$ isSuperType = AllowedArrayKeysTypes::getType ()->isSuperTypeOf ($ dimensionType );
42
- if ($ isSuperType ->no ()) {
43
- return [
44
- RuleErrorBuilder::message (
45
- sprintf ('Invalid array key type %s. ' , $ dimensionType ->describe (VerbosityLevel::typeOnly ())),
46
- )->identifier ('array.invalidKey ' )->build (),
47
- ];
48
- } elseif ($ this ->reportMaybes && $ isSuperType ->maybe () && !$ dimensionType instanceof MixedType) {
49
- return [
50
- RuleErrorBuilder::message (
51
- sprintf ('Possibly invalid array key type %s. ' , $ dimensionType ->describe (VerbosityLevel::typeOnly ())),
52
- )->identifier ('array.invalidKey ' )->build (),
53
- ];
51
+ if ($ isSuperType ->yes ()) {
52
+ return [];
54
53
}
55
54
56
- return [];
55
+ return [
56
+ RuleErrorBuilder::message (sprintf (
57
+ '%s array key type %s. ' ,
58
+ $ isSuperType ->no () ? 'Invalid ' : 'Possibly invalid ' ,
59
+ $ dimensionType ->describe (VerbosityLevel::typeOnly ())
60
+ ))->identifier ('array.invalidKey ' )->build (),
61
+ ];
57
62
}
58
63
59
64
}
0 commit comments