Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/Rules/Arrays/InvalidKeyInArrayItemRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\MixedType;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

Expand All @@ -20,8 +21,7 @@ final class InvalidKeyInArrayItemRule implements Rule
{

public function __construct(
#[AutowiredParameter]
private bool $reportMaybes,
private RuleLevelHelper $ruleLevelHelper,
)
{
}
Expand All @@ -37,23 +37,28 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$dimensionType = $scope->getType($node->key);
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->key,
'',
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimType)->yes(),
)->getType();
if ($dimensionType instanceof ErrorType) {
return [];
}

$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType);
if ($isSuperType->no()) {
return [
RuleErrorBuilder::message(
sprintf('Invalid array key type %s.', $dimensionType->describe(VerbosityLevel::typeOnly())),
)->identifier('array.invalidKey')->build(),
];
} elseif ($this->reportMaybes && $isSuperType->maybe() && !$dimensionType instanceof MixedType) {
return [
RuleErrorBuilder::message(
sprintf('Possibly invalid array key type %s.', $dimensionType->describe(VerbosityLevel::typeOnly())),
)->identifier('array.invalidKey')->build(),
];
if ($isSuperType->yes()) {
return [];
}

return [];
return [
RuleErrorBuilder::message(sprintf(
'%s array key type %s.',
$isSuperType->no() ? 'Invalid' : 'Possibly invalid',
$dimensionType->describe(VerbosityLevel::typeOnly()),
))->identifier('array.invalidKey')->build(),
];
}

}
34 changes: 33 additions & 1 deletion tests/PHPStan/Rules/Arrays/InvalidKeyInArrayItemRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Arrays;

use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

Expand All @@ -12,9 +13,15 @@
class InvalidKeyInArrayItemRuleTest extends RuleTestCase
{

private bool $checkExplicitMixed = false;

private bool $checkImplicitMixed = false;

protected function getRule(): Rule
{
return new InvalidKeyInArrayItemRule(true);
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true);

return new InvalidKeyInArrayItemRule($ruleLevelHelper);
}

public function testInvalidKey(): void
Expand All @@ -35,6 +42,31 @@ public function testInvalidKey(): void
]);
}

public function testInvalidMixedKey(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;

$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], [
[
'Invalid array key type DateTimeImmutable.',
13,
],
[
'Invalid array key type array.',
14,
],
[
'Possibly invalid array key type stdClass|string.',
15,
],
[
'Possibly invalid array key type mixed.',
22,
],
]);
}

public function testInvalidKeyInList(): void
{
$this->analyse([__DIR__ . '/data/invalid-key-list.php'], [
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/invalid-key-array-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
[] => 'bbb',
$stringOrObject => 'aaa',
];

/** @var mixed $mixed */
$mixed = doFoo();

$b = [
$mixed => 'foo',
];
Loading