Skip to content

Introduce reportCastedArrayKey parameter #4012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ parameters:
reportStaticMethodSignatures: false
reportWrongPhpDocTypeInVarTag: false
reportAnyTypeWideningInVarTag: false
reportArrayKeyCast: false
reportPossiblyNonexistentGeneralArrayOffset: false
reportPossiblyNonexistentConstantArrayOffset: false
checkMissingOverrideMethodAttribute: false
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ parametersSchema:
reportStaticMethodSignatures: bool()
reportWrongPhpDocTypeInVarTag: bool()
reportAnyTypeWideningInVarTag: bool()
reportArrayKeyCast: bool()
reportPossiblyNonexistentGeneralArrayOffset: bool()
reportPossiblyNonexistentConstantArrayOffset: bool()
checkMissingOverrideMethodAttribute: bool()
Expand Down
9 changes: 8 additions & 1 deletion src/Rules/Arrays/AllowedArrayKeysTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@
final class AllowedArrayKeysTypes
{

public static function getType(): Type
public static function getType(bool $strict = false): Type
{
if ($strict) {
return new UnionType([
new IntegerType(),
new StringType(),
]);
}

return new UnionType([
new IntegerType(),
new StringType(),
Expand Down
23 changes: 15 additions & 8 deletions src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function __construct(
private RuleLevelHelper $ruleLevelHelper,
#[AutowiredParameter]
private bool $reportMaybes,
#[AutowiredParameter]
private bool $reportArrayKeyCast,
)
{
}
Expand All @@ -41,23 +43,28 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$dimensionType = $scope->getType($node->dim);
if ($dimensionType instanceof MixedType) {
return [];
}

$varType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->var,
'',
static fn (Type $varType): bool => $varType->isArray()->no() || AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType)->yes(),
static fn (Type $varType): bool => $varType->isArray()->no(),
)->getType();

if ($varType instanceof ErrorType || $varType->isArray()->no()) {
return [];
}

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

$isSuperType = AllowedArrayKeysTypes::getType($this->reportArrayKeyCast)->isSuperTypeOf($dimensionType);
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) {
return [];
}
Expand Down
16 changes: 14 additions & 2 deletions src/Rules/Arrays/InvalidKeyInArrayItemRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

Expand All @@ -20,8 +22,11 @@ final class InvalidKeyInArrayItemRule implements Rule
{

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

$dimensionType = $scope->getType($node->key);
$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType);
$reportArrayKeyCast = $this->reportArrayKeyCast;
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->key,
'',
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType($reportArrayKeyCast)->isSuperTypeOf($dimType)->yes(),
)->getType();

$isSuperType = AllowedArrayKeysTypes::getType($reportArrayKeyCast)->isSuperTypeOf($dimensionType);
if ($isSuperType->no()) {
return [
RuleErrorBuilder::message(
Expand Down
186 changes: 184 additions & 2 deletions tests/PHPStan/Rules/Arrays/InvalidKeyInArrayDimFetchRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
class InvalidKeyInArrayDimFetchRuleTest extends RuleTestCase
{

private bool $reportCastedArrayKey = false;

private bool $checkUnionType = true;

private bool $checkNullable = true;

protected function getRule(): Rule
{
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, false, false, false, true);
return new InvalidKeyInArrayDimFetchRule($ruleLevelHelper, true);
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), $this->checkNullable, false, $this->checkUnionType, false, false, false, true);
return new InvalidKeyInArrayDimFetchRule($ruleLevelHelper, true, $this->reportCastedArrayKey);
}

public function testInvalidKey(): void
Expand Down Expand Up @@ -61,6 +67,166 @@ public function testInvalidKey(): void
]);
}

public function testInvalidKeyOnLevel6(): void
{
$this->checkNullable = false;
$this->checkUnionType = false;
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], [
[
'Invalid array key type DateTimeImmutable.',
7,
],
[
'Invalid array key type array.',
8,
],
[
'Invalid array key type DateTimeImmutable.',
31,
],
[
'Invalid array key type DateTimeImmutable.',
45,
],
[
'Invalid array key type DateTimeImmutable.',
46,
],
[
'Invalid array key type DateTimeImmutable.',
47,
],
[
'Invalid array key type stdClass.',
47,
],
[
'Invalid array key type DateTimeImmutable.',
48,
],
]);
}

public function testInvalidKeyReportingCastedArrayKey(): void
{
$this->reportCastedArrayKey = true;
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], [
[
'Invalid array key type null.',
6,
],
[
'Invalid array key type DateTimeImmutable.',
7,
],
[
'Invalid array key type array.',
8,
],
[
'Invalid array key type float.',
10,
],
[
'Invalid array key type true.',
12,
],
[
'Invalid array key type false.',
13,
],
[
'Possibly invalid array key type string|null.',
17,
],
[
'Possibly invalid array key type stdClass|string.',
24,
],
[
'Invalid array key type DateTimeImmutable.',
31,
],
[
'Invalid array key type DateTimeImmutable.',
45,
],
[
'Invalid array key type DateTimeImmutable.',
46,
],
[
'Invalid array key type DateTimeImmutable.',
47,
],
[
'Invalid array key type stdClass.',
47,
],
[
'Invalid array key type DateTimeImmutable.',
48,
],
]);
}

public function testInvalidKeyReportingCastedArrayKeyOnLevel6(): void
{
$this->checkNullable = false;
$this->checkUnionType = false;
$this->reportCastedArrayKey = true;
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], [
[
'Invalid array key type null.',
6,
],
[
'Invalid array key type DateTimeImmutable.',
7,
],
[
'Invalid array key type array.',
8,
],
[
'Invalid array key type float.',
10,
],
[
'Invalid array key type true.',
12,
],
[
'Invalid array key type false.',
13,
],
[
'Invalid array key type DateTimeImmutable.',
31,
],
[
'Invalid array key type DateTimeImmutable.',
45,
],
[
'Invalid array key type DateTimeImmutable.',
46,
],
[
'Invalid array key type DateTimeImmutable.',
47,
],
[
'Invalid array key type stdClass.',
47,
],
[
'Invalid array key type DateTimeImmutable.',
48,
],
]);
}

#[RequiresPhp('>= 8.1')]
public function testBug6315(): void
{
Expand Down Expand Up @@ -92,4 +258,20 @@ public function testBug6315(): void
]);
}

public function testUnsetFalseKey(): void
{
$this->reportCastedArrayKey = true;

$this->analyse([__DIR__ . '/data/unset-false-key.php'], [
[
'Invalid array key type false.',
6,
],
[
'Invalid array key type false.',
13,
],
]);
}

}
Loading
Loading