Skip to content

Commit 7a27887

Browse files
committed
If an object implements \ArrayAccess, check both array value and property
1 parent 61f7cf1 commit 7a27887

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Utils/Utils.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,16 @@ public static function suggestionList(string $input, array $options): array
268268
*/
269269
public static function extractKey($objectLikeValue, string $key)
270270
{
271-
if (is_array($objectLikeValue) || $objectLikeValue instanceof \ArrayAccess) {
271+
if (is_array($objectLikeValue)) {
272272
return $objectLikeValue[$key] ?? null;
273273
}
274274

275+
if ($objectLikeValue instanceof \ArrayAccess) {
276+
return $objectLikeValue[$key]
277+
?? $objectLikeValue->{$key} // @phpstan-ignore-line Variable property access on ArrayAccess is fine here, we do the same for arbitrary objects
278+
?? null;
279+
}
280+
275281
if (is_object($objectLikeValue)) {
276282
return $objectLikeValue->{$key} ?? null;
277283
}

tests/Executor/ExecutorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,7 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo
11901190
'name' => 'ArrayAccess',
11911191
'fields' => [
11921192
'set' => Type::int(),
1193+
'setProperty' => Type::int(),
11931194
'unsetNull' => Type::int(),
11941195
'unsetThrow' => Type::int(),
11951196
],
@@ -1224,6 +1225,8 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo
12241225
'arrayAccess' => [
12251226
'type' => $ArrayAccess,
12261227
'resolve' => static fn (): \ArrayAccess => new class() implements \ArrayAccess {
1228+
public ?int $setProperty = 1;
1229+
12271230
/** @param mixed $offset */
12281231
#[\ReturnTypeWillChange]
12291232
public function offsetExists($offset): bool
@@ -1317,6 +1320,7 @@ public function __get(string $name): ?int
13171320
}
13181321
arrayAccess {
13191322
set
1323+
setProperty
13201324
unsetNull
13211325
unsetThrow
13221326
}
@@ -1344,6 +1348,7 @@ public function __get(string $name): ?int
13441348
],
13451349
'arrayAccess' => [
13461350
'set' => 1,
1351+
'setProperty' => 1,
13471352
'unsetNull' => null,
13481353
'unsetThrow' => null,
13491354
],

0 commit comments

Comments
 (0)