Skip to content

Commit 493e2de

Browse files
Fix objectType::getOffsetValueType
1 parent 5878035 commit 493e2de

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/Type/ObjectType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,14 +1228,14 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
12281228

12291229
public function getOffsetValueType(Type $offsetType): Type
12301230
{
1231-
if (!$this->isExtraOffsetAccessibleClass()->no()) {
1232-
return new MixedType();
1233-
}
1234-
12351231
if ($this->isInstanceOf(ArrayAccess::class)->yes()) {
12361232
return RecursionGuard::run($this, fn (): Type => $this->getMethod('offsetGet', new OutOfClassScope())->getOnlyVariant()->getReturnType());
12371233
}
12381234

1235+
if (!$this->isExtraOffsetAccessibleClass()->no()) {
1236+
return new MixedType();
1237+
}
1238+
12391239
return new ErrorType();
12401240
}
12411241

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12125;
4+
5+
use ArrayAccess;
6+
use Exception;
7+
use stdClass;
8+
9+
use function PHPStan\Testing\assertType;
10+
11+
/** @var ArrayAccess<array-key, stdClass>|array<array-key, stdClass> $bug */
12+
$bug = [];
13+
14+
assertType('stdClass|null', $bug['key'] ?? null);
15+
16+
interface MyInterface {
17+
/** @return array<string, string> | ArrayAccess<string, string> */
18+
public function getStrings(): array | ArrayAccess;
19+
}
20+
21+
function myFunction(MyInterface $container): string {
22+
$strings = $container->getStrings();
23+
assertType('array<string, string>|ArrayAccess<string, string>', $strings);
24+
assertType('string|null', $strings['test']);
25+
return $strings['test'];
26+
}
27+
28+
function myOtherFunction(MyInterface $container): string {
29+
$strings = $container->getStrings();
30+
assertType('array<string, string>|ArrayAccess<string, string>', $strings);
31+
if (isset($strings['test'])) {
32+
assertType('string', $strings['test']);
33+
return $strings['test'];
34+
} else {
35+
throw new Exception();
36+
}
37+
}

0 commit comments

Comments
 (0)