From f2a17290e84a36cd45bba4388d66bdc50edffc38 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 26 Feb 2024 15:53:42 +0000 Subject: [PATCH] If an object implements \ArrayAccess, check both array value and property --- src/Utils/Utils.php | 8 +++++++- tests/Executor/ExecutorTest.php | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php index 631185fdf..587ae67f5 100644 --- a/src/Utils/Utils.php +++ b/src/Utils/Utils.php @@ -267,10 +267,16 @@ public static function suggestionList(string $input, array $options): array */ public static function extractKey($objectLikeValue, string $key) { - if (\is_array($objectLikeValue) || $objectLikeValue instanceof \ArrayAccess) { + if (\is_array($objectLikeValue)) { return $objectLikeValue[$key] ?? null; } + if ($objectLikeValue instanceof \ArrayAccess) { + return $objectLikeValue[$key] + ?? $objectLikeValue->{$key} // @phpstan-ignore-line Variable property access on ArrayAccess is fine here, we do the same for arbitrary objects + ?? null; + } + if (\is_object($objectLikeValue)) { return $objectLikeValue->{$key} ?? null; } diff --git a/tests/Executor/ExecutorTest.php b/tests/Executor/ExecutorTest.php index f70412e3a..291202dc5 100644 --- a/tests/Executor/ExecutorTest.php +++ b/tests/Executor/ExecutorTest.php @@ -1117,6 +1117,7 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo 'name' => 'ArrayAccess', 'fields' => [ 'set' => Type::int(), + 'setProperty' => Type::int(), 'unsetNull' => Type::int(), 'unsetThrow' => Type::int(), ], @@ -1151,6 +1152,8 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo 'arrayAccess' => [ 'type' => $ArrayAccess, 'resolve' => static fn (): \ArrayAccess => new class() implements \ArrayAccess { + public ?int $setProperty = 1; + /** @param mixed $offset */ #[\ReturnTypeWillChange] public function offsetExists($offset): bool @@ -1244,6 +1247,7 @@ public function __get(string $name): ?int } arrayAccess { set + setProperty unsetNull unsetThrow } @@ -1271,6 +1275,7 @@ public function __get(string $name): ?int ], 'arrayAccess' => [ 'set' => 1, + 'setProperty' => 1, 'unsetNull' => null, 'unsetThrow' => null, ],