Skip to content

Respect phpstan level in InvalidKeyInArrayDimFetchRule #4166

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 4 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
18 changes: 11 additions & 7 deletions src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
Expand Down Expand Up @@ -41,22 +40,27 @@ 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 [];
}

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

$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType);
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) {
return [];
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/RuleLevelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ private function findTypeToCheckImplementation(
bool $isTopLevel = false,
): FoundTypeResult
{
if (!$this->checkNullables && !$type->isNull()->yes()) {
if (
!$this->checkNullables
&& !$type->isNull()->yes()
&& !$unionTypeCriteriaCallback(new NullType())
) {
$type = TypeCombinator::removeNull($type);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Levels/LevelsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function dataTopics(): array
['arrayDestructuring'],
['listType'],
['missingTypes'],
['arrayOffsetAccess'],
];
if (PHP_VERSION_ID >= 80300) {
$topics[] = ['constantAccesses83'];
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Possibly invalid array key type mixed.",
"line": 22,
"ignorable": true
}
]
7 changes: 7 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Invalid array key type DateTimeImmutable.",
"line": 17,
"ignorable": true
}
]
37 changes: 37 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"message": "Expression \"$a[42]\" on a separate line does not do anything.",
"line": 15,
"ignorable": true
},
{
"message": "Expression \"$a[null]\" on a separate line does not do anything.",
"line": 16,
"ignorable": true
},
{
"message": "Expression \"$a[$intOrNull]\" on a separate line does not do anything.",
"line": 18,
"ignorable": true
},
{
"message": "Expression \"$a[$objectOrInt]\" on a separate line does not do anything.",
"line": 19,
"ignorable": true
},
{
"message": "Expression \"$a[$objectOrNull]\" on a separate line does not do anything.",
"line": 20,
"ignorable": true
},
{
"message": "Expression \"$a[$explicitlyMixed]\" on a separate line does not do anything.",
"line": 21,
"ignorable": true
},
{
"message": "Expression \"$a[$implicitlyMixed]\" on a separate line does not do anything.",
"line": 22,
"ignorable": true
}
]
12 changes: 12 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Method Levels\\ArrayOffsetAccess\\Foo::test() has parameter $a with no value type specified in iterable type array.",
"line": 13,
"ignorable": true
},
{
"message": "Method Levels\\ArrayOffsetAccess\\Foo::test() has parameter $implicitlyMixed with no type specified.",
"line": 13,
"ignorable": true
}
]
12 changes: 12 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Possibly invalid array key type int|object.",
"line": 19,
"ignorable": true
},
{
"message": "Possibly invalid array key type object|null.",
"line": 20,
"ignorable": true
}
]
7 changes: 7 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Possibly invalid array key type mixed.",
"line": 21,
"ignorable": true
}
]
24 changes: 24 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Levels\ArrayOffsetAccess;

class Foo {
/**
* @param int|null $intOrNull
* @param object|int $objectOrInt
* @param object|null $objectOrNull
* @param mixed $explicitlyMixed
* @return void
*/
public function test(array $a, $intOrNull, $objectOrInt, $objectOrNull, $explicitlyMixed, $implicitlyMixed)
{
$a[42];
$a[null];
$a[new \DateTimeImmutable()];
$a[$intOrNull];
$a[$objectOrInt];
$a[$objectOrNull];
$a[$explicitlyMixed];
$a[$implicitlyMixed];
}
}
Loading