Skip to content

Commit f9c1e7e

Browse files
Try
1 parent 051e0af commit f9c1e7e

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use PHPStan\Rules\RuleLevelHelper;
1313
use PHPStan\Type\BenevolentUnionType;
1414
use PHPStan\Type\ErrorType;
15+
use PHPStan\Type\MixedType;
1516
use PHPStan\Type\Type;
17+
use PHPStan\Type\TypeCombinator;
1618
use PHPStan\Type\TypeUtils;
1719
use PHPStan\Type\VerbosityLevel;
1820
use function count;
@@ -44,11 +46,18 @@ public function check(
4446
Type $dimType,
4547
): array
4648
{
49+
if (!$dimType instanceof MixedType) {
50+
// Non allowed array keys will already be reported by InvalidKeyInArrayItemRule
51+
$dimTypeToCheck = TypeCombinator::intersect($dimType, AllowedArrayKeysTypes::getType());
52+
} else {
53+
$dimTypeToCheck = $dimType;
54+
}
55+
4756
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
4857
$scope,
4958
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $var),
5059
$unknownClassPattern,
51-
static fn (Type $type): bool => $type->hasOffsetValueType($dimType)->yes(),
60+
static fn (Type $type): bool => $type->hasOffsetValueType($dimTypeToCheck)->yes(),
5261
);
5362
$type = $typeResult->getType();
5463
if ($type instanceof ErrorType) {
@@ -61,7 +70,7 @@ public function check(
6170

6271
if ($type->hasOffsetValueType($dimType)->no()) {
6372
return [
64-
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(count($dimType->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
73+
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimTypeToCheck->describe(count($dimTypeToCheck->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
6574
->identifier('offsetAccess.notFound')
6675
->build(),
6776
];
@@ -76,7 +85,6 @@ public function check(
7685
$flattenedTypes = TypeUtils::flattenTypes($type);
7786
}
7887

79-
$allowedArrayKeysTypes = AllowedArrayKeysTypes::getType();
8088
foreach ($flattenedTypes as $innerType) {
8189
if (
8290
$this->reportPossiblyNonexistentGeneralArrayOffset
@@ -95,6 +103,7 @@ public function check(
95103
$report = true;
96104
break;
97105
}
106+
98107
if ($dimType instanceof BenevolentUnionType) {
99108
$flattenedInnerTypes = [$dimType];
100109
} else {
@@ -112,7 +121,7 @@ public function check(
112121

113122
if ($report) {
114123
return [
115-
RuleErrorBuilder::message(sprintf('Offset %s might not exist on %s.', $dimType->describe(count($dimType->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
124+
RuleErrorBuilder::message(sprintf('Offset %s might not exist on %s.', $dimTypeToCheck->describe(count($dimTypeToCheck->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
116125
->identifier('offsetAccess.notFound')
117126
->build(),
118127
];

0 commit comments

Comments
 (0)