Skip to content

Commit aa9e2e8

Browse files
committed
AppendedArrayKeyTypeRule - more precise type description
1 parent 6a33de9 commit aa9e2e8

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/Rules/Arrays/AppendedArrayKeyTypeRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
7676
}
7777

7878
if (!$arrayType->getIterableKeyType()->isSuperTypeOf($keyType)->yes()) {
79+
$verbosity = VerbosityLevel::getRecommendedLevelByType($arrayType->getIterableKeyType(), $keyType);
7980
return [
8081
RuleErrorBuilder::message(sprintf(
8182
'Array (%s) does not accept key %s.',
82-
$arrayType->describe(VerbosityLevel::typeOnly()),
83+
$arrayType->describe($verbosity),
8384
$keyType->describe(VerbosityLevel::value())
8485
))->build(),
8586
];

tests/PHPStan/Rules/Arrays/AppendedArrayKeyTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public function testRule(): void
4545
'Array (array<string, mixed>) does not accept key 1.',
4646
46,
4747
],
48+
[
49+
'Array (array<1|2|3, string>) does not accept key int.',
50+
80,
51+
],
52+
[
53+
'Array (array<1|2|3, string>) does not accept key 4.',
54+
85,
55+
],
4856
]);
4957
}
5058

tests/PHPStan/Rules/Arrays/data/appended-array-key.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,21 @@ public function doBar()
6868
}
6969

7070
}
71+
72+
class MorePreciseKey
73+
{
74+
75+
/** @var array<1|2|3, string> */
76+
private $test;
77+
78+
public function doFoo(int $i): void
79+
{
80+
$this->test[$i] = 'foo';
81+
}
82+
83+
public function doBar(): void
84+
{
85+
$this->test[4] = 'foo';
86+
}
87+
88+
}

0 commit comments

Comments
 (0)