Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4972fd9

Browse files
VincentLangletondrejmirtes
authored andcommitted
Rely on RuleLevelHelper in InvalidKeyInArrayItemRule
1 parent 022e859 commit 4972fd9

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

‎src/Rules/Arrays/InvalidKeyInArrayItemRule.php‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7-
use PHPStan\DependencyInjection\AutowiredParameter;
87
use PHPStan\DependencyInjection\RegisteredRule;
98
use PHPStan\Rules\Rule;
109
use PHPStan\Rules\RuleErrorBuilder;
11-
use PHPStan\Type\MixedType;
10+
use PHPStan\Rules\RuleLevelHelper;
11+
use PHPStan\Type\ErrorType;
12+
use PHPStan\Type\Type;
1213
use PHPStan\Type\VerbosityLevel;
1314
use function sprintf;
1415

@@ -20,8 +21,7 @@ final class InvalidKeyInArrayItemRule implements Rule
2021
{
2122

2223
public function __construct(
23-
#[AutowiredParameter]
24-
private bool $reportMaybes,
24+
private RuleLevelHelper $ruleLevelHelper,
2525
)
2626
{
2727
}
@@ -37,23 +37,28 @@ public function processNode(Node $node, Scope $scope): array
3737
return [];
3838
}
3939

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+
4150
$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 [];
5453
}
5554

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+
];
5762
}
5863

5964
}

‎tests/PHPStan/Rules/Arrays/InvalidKeyInArrayItemRuleTest.php‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Rules\Arrays;
44

55
use PHPStan\Rules\Rule;
6+
use PHPStan\Rules\RuleLevelHelper;
67
use PHPStan\Testing\RuleTestCase;
78
use PHPUnit\Framework\Attributes\RequiresPhp;
89

@@ -12,9 +13,15 @@
1213
class InvalidKeyInArrayItemRuleTest extends RuleTestCase
1314
{
1415

16+
private bool $checkExplicitMixed = false;
17+
18+
private bool $checkImplicitMixed = false;
19+
1520
protected function getRule(): Rule
1621
{
17-
return new InvalidKeyInArrayItemRule(true);
22+
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true);
23+
24+
return new InvalidKeyInArrayItemRule($ruleLevelHelper);
1825
}
1926

2027
public function testInvalidKey(): void
@@ -35,6 +42,31 @@ public function testInvalidKey(): void
3542
]);
3643
}
3744

45+
public function testInvalidMixedKey(): void
46+
{
47+
$this->checkExplicitMixed = true;
48+
$this->checkImplicitMixed = true;
49+
50+
$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], [
51+
[
52+
'Invalid array key type DateTimeImmutable.',
53+
13,
54+
],
55+
[
56+
'Invalid array key type array.',
57+
14,
58+
],
59+
[
60+
'Possibly invalid array key type stdClass|string.',
61+
15,
62+
],
63+
[
64+
'Possibly invalid array key type mixed.',
65+
22,
66+
],
67+
]);
68+
}
69+
3870
public function testInvalidKeyInList(): void
3971
{
4072
$this->analyse([__DIR__ . '/data/invalid-key-list.php'], [

‎tests/PHPStan/Rules/Arrays/data/invalid-key-array-item.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@
1414
[] => 'bbb',
1515
$stringOrObject => 'aaa',
1616
];
17+
18+
/** @var mixed $mixed */
19+
$mixed = doFoo();
20+
21+
$b = [
22+
$mixed => 'foo',
23+
];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /