Skip to content

Commit f2a1729

Browse files
committed
If an object implements \ArrayAccess, check both array value and property
1 parent ac44145 commit f2a1729

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Utils/Utils.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,16 @@ public static function suggestionList(string $input, array $options): array
267267
*/
268268
public static function extractKey($objectLikeValue, string $key)
269269
{
270-
if (\is_array($objectLikeValue) || $objectLikeValue instanceof \ArrayAccess) {
270+
if (\is_array($objectLikeValue)) {
271271
return $objectLikeValue[$key] ?? null;
272272
}
273273

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

tests/Executor/ExecutorTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo
11171117
'name' => 'ArrayAccess',
11181118
'fields' => [
11191119
'set' => Type::int(),
1120+
'setProperty' => Type::int(),
11201121
'unsetNull' => Type::int(),
11211122
'unsetThrow' => Type::int(),
11221123
],
@@ -1151,6 +1152,8 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo
11511152
'arrayAccess' => [
11521153
'type' => $ArrayAccess,
11531154
'resolve' => static fn (): \ArrayAccess => new class() implements \ArrayAccess {
1155+
public ?int $setProperty = 1;
1156+
11541157
/** @param mixed $offset */
11551158
#[\ReturnTypeWillChange]
11561159
public function offsetExists($offset): bool
@@ -1244,6 +1247,7 @@ public function __get(string $name): ?int
12441247
}
12451248
arrayAccess {
12461249
set
1250+
setProperty
12471251
unsetNull
12481252
unsetThrow
12491253
}
@@ -1271,6 +1275,7 @@ public function __get(string $name): ?int
12711275
],
12721276
'arrayAccess' => [
12731277
'set' => 1,
1278+
'setProperty' => 1,
12741279
'unsetNull' => null,
12751280
'unsetThrow' => null,
12761281
],

0 commit comments

Comments
 (0)