Skip to content

Commit b55656f

Browse files
b1rdexondrejmirtes
authored andcommitted
Non-ArrayAccess object might be offset-accessible
1 parent a8b7cea commit b55656f

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

src/Type/ObjectType.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,15 @@ private function isExtraOffsetAccessibleClass(): TrinaryLogic
589589
}
590590
}
591591

592-
return TrinaryLogic::createNo();
592+
if ($classReflection->isInterface()) {
593+
return TrinaryLogic::createMaybe();
594+
}
595+
596+
if ($classReflection->isFinal()) {
597+
return TrinaryLogic::createNo();
598+
}
599+
600+
return TrinaryLogic::createMaybe();
593601
}
594602

595603
public function isOffsetAccessible(): TrinaryLogic

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ public function dataArrayDestructuring(): array
11741174
'$intArrayForRewritingFirstElement[1]',
11751175
],
11761176
[
1177-
'*ERROR*',
1177+
'stdClass',
11781178
'$obj',
11791179
],
11801180
[

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,9 @@ public function testCoalesceAssign(): void
230230
$this->analyse([__DIR__ . '/data/nonexistent-offset-coalesce-assign.php'], []);
231231
}
232232

233+
public function testIntersection(): void
234+
{
235+
$this->analyse([__DIR__ . '/data/nonexistent-offset-intersection.php'], []);
236+
}
237+
233238
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
class Foo
4+
{
5+
/** @var \ArrayAccess<string, mixed> */
6+
private $fooA;
7+
/** @var \ArrayAccess<string, mixed>&iterable<mixed> */
8+
private $fooB;
9+
/** @var \ArrayAccess<string, mixed>&\Countable */
10+
private $fooC;
11+
/** @var \ArrayAccess<string, mixed>&\stdClass */
12+
private $fooD;
13+
14+
public function test(): void
15+
{
16+
$a = $this->fooA['bar'];
17+
$b = $this->fooB['bar'];
18+
$c = $this->fooC['bar'];
19+
$d = $this->fooD['bar'];
20+
}
21+
}

tests/PHPStan/Type/ObjectTypeTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,23 @@ public function dataHasOffsetValueType(): array
437437
[
438438
new ObjectType(\stdClass::class),
439439
new IntegerType(),
440+
TrinaryLogic::createMaybe(),
441+
],
442+
[
443+
new ObjectType(\Generator::class),
444+
new IntegerType(),
440445
TrinaryLogic::createNo(),
441446
],
442447
[
443448
new ObjectType(\ArrayAccess::class),
444449
new IntegerType(),
445450
TrinaryLogic::createMaybe(),
446451
],
452+
[
453+
new ObjectType(\Countable::class),
454+
new IntegerType(),
455+
TrinaryLogic::createMaybe(),
456+
],
447457
[
448458
new GenericObjectType(\ArrayAccess::class, [new IntegerType(), new MixedType()]),
449459
new IntegerType(),

0 commit comments

Comments
 (0)