Skip to content

Commit d696666

Browse files
Fix hasOffsetValueType for arrays
1 parent 4f321ba commit d696666

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

src/Type/ArrayType.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
99
use PHPStan\Reflection\ClassMemberAccessAnswerer;
1010
use PHPStan\Reflection\TrivialParametersAcceptor;
11+
use PHPStan\Rules\Arrays\AllowedArrayKeysTypes;
1112
use PHPStan\ShouldNotHappenException;
1213
use PHPStan\TrinaryLogic;
1314
use PHPStan\Type\Accessory\AccessoryArrayListType;
@@ -267,10 +268,8 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
267268

268269
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
269270
{
270-
$offsetType = $offsetType->toArrayKey();
271-
if ($offsetType instanceof ErrorType) {
272-
return TrinaryLogic::createNo();
273-
}
271+
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
272+
$offsetType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
274273

275274
if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
276275
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())

src/Type/Constant/ConstantArrayType.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Reflection\InitializerExprTypeResolver;
2020
use PHPStan\Reflection\PhpVersionStaticAccessor;
2121
use PHPStan\Reflection\TrivialParametersAcceptor;
22+
use PHPStan\Rules\Arrays\AllowedArrayKeysTypes;
2223
use PHPStan\ShouldNotHappenException;
2324
use PHPStan\TrinaryLogic;
2425
use PHPStan\Type\AcceptsResult;
@@ -581,17 +582,14 @@ public function findTypeAndMethodNames(): array
581582

582583
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
583584
{
584-
$offsetArrayKeyType = $offsetType->toArrayKey();
585+
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
586+
$offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
585587

586588
return $this->recursiveHasOffsetValueType($offsetArrayKeyType);
587589
}
588590

589591
private function recursiveHasOffsetValueType(Type $offsetType): TrinaryLogic
590592
{
591-
if ($offsetType instanceof ErrorType) {
592-
return TrinaryLogic::createNo();
593-
}
594-
595593
if ($offsetType instanceof UnionType) {
596594
$results = [];
597595
foreach ($offsetType->getTypes() as $innerType) {

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ public static function dataProperties(): array
18011801
'$this->resource',
18021802
],
18031803
[
1804-
'*ERROR*',
1804+
'mixed',
18051805
'$this->yetAnotherAnotherMixedParameter',
18061806
],
18071807
[

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ public function testStrings(): void
193193
'Offset 12.34 might not exist on array|string.',
194194
28,
195195
],
196-
[
197-
'Offset int|object might not exist on array|string.',
198-
32,
199-
],
200196
]);
201197
}
202198

@@ -932,10 +928,6 @@ public function testBugObject(): void
932928
'Offset int|object does not exist on array{baz: 21}|array{foo: 17, bar: 19}.',
933929
12,
934930
],
935-
[
936-
'Offset object does not exist on array<string, int>.',
937-
21,
938-
],
939931
]);
940932
}
941933

tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ public function testBug10610(): void
326326
$this->analyse([__DIR__ . '/data/bug-10610.php'], []);
327327
}
328328

329+
public function testBugDoctrine(): void
330+
{
331+
$this->analyse([__DIR__ . '/data/bug-doctrine.php'], []);
332+
}
333+
329334
#[RequiresPhp('>= 8.4')]
330335
public function testBug12553(): void
331336
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace BugDoctrine;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello(string|array $a, array $b): void
8+
{
9+
$b[$a] ?? 2;
10+
}
11+
}

0 commit comments

Comments
 (0)