From 154b516cebdd65fc5eb6b2b284fa22d59f7f7061 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Mon, 14 Jul 2025 08:55:38 +0900 Subject: [PATCH 1/5] test: add test for creating bracketed object in InputQuery --- tests/InputQueryTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/InputQueryTest.php b/tests/InputQueryTest.php index 979ff8a..5f3c5ae 100644 --- a/tests/InputQueryTest.php +++ b/tests/InputQueryTest.php @@ -118,6 +118,26 @@ public function testCreateNestedObject(): void $this->assertSame('john@example.com', $todo->author->email); } + public function testCreateBracketObject(): void + { + $query = [ + 'title' => 'Buy milk', + 'author' => [ + 'name' => 'John', + 'email' => 'john@example.com', + ], + ]; + + $todo = $this->inputQuery->newInstance(TodoInput::class, $query); + + $this->assertInstanceOf(TodoInput::class, $todo); + /** @var TodoInput $todo */ + $this->assertSame('Buy milk', $todo->title); + $this->assertInstanceOf(AuthorInput::class, $todo->author); + $this->assertSame('John', $todo->author->name); + $this->assertSame('john@example.com', $todo->author->email); + } + public function testCreateMixedInputAndDI(): void { $query = [ From 93f4b04c631232740e24619ce3e0af71d0707126 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Mon, 14 Jul 2025 08:58:05 +0900 Subject: [PATCH 2/5] refactor: handle direct nested array initialization in InputQuery --- src/InputQuery.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/InputQuery.php b/src/InputQuery.php index 096f9e3..a90586b 100644 --- a/src/InputQuery.php +++ b/src/InputQuery.php @@ -263,6 +263,13 @@ private function resolveObjectType(ReflectionParameter $param, array $query, arr return $arrayObjectResult; } + // Check if the parameter name exists as a direct nested array in query + if (array_key_exists($paramName, $query) && is_array($query[$paramName])) { + assert(class_exists($className)); + /** @var class-string $className */ + return $this->newInstance($className, $query[$paramName]); + } + // Regular object type with #[Input] - create nested $nestedQuery = $this->extractNestedQuery($paramName, $query); From e11e9cd176b1e182a4117abf0677f1aaf47a9b13 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Tue, 15 Jul 2025 18:01:03 +0900 Subject: [PATCH 3/5] refactor: remove redundant type annotation in InputQuery --- src/InputQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InputQuery.php b/src/InputQuery.php index a90586b..57b9912 100644 --- a/src/InputQuery.php +++ b/src/InputQuery.php @@ -266,7 +266,7 @@ private function resolveObjectType(ReflectionParameter $param, array $query, arr // Check if the parameter name exists as a direct nested array in query if (array_key_exists($paramName, $query) && is_array($query[$paramName])) { assert(class_exists($className)); - /** @var class-string $className */ + return $this->newInstance($className, $query[$paramName]); } From f859c3997f6108bae2508344d5900703f56f0281 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Wed, 16 Jul 2025 09:14:41 +0900 Subject: [PATCH 4/5] refactor: add explicit type annotations for nested query handling in InputQuery --- src/InputQuery.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/InputQuery.php b/src/InputQuery.php index 57b9912..c77bf00 100644 --- a/src/InputQuery.php +++ b/src/InputQuery.php @@ -267,7 +267,11 @@ private function resolveObjectType(ReflectionParameter $param, array $query, arr if (array_key_exists($paramName, $query) && is_array($query[$paramName])) { assert(class_exists($className)); - return $this->newInstance($className, $query[$paramName]); + /** @var Query $nestedQuery */ + $nestedQuery = $query[$paramName]; + + /** @var class-string $className */ + return $this->newInstance($className, $nestedQuery); } // Regular object type with #[Input] - create nested From faf0f06eb1d7dd4c766f775c82d2508d866391cf Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 16 Jul 2025 22:33:22 +0900 Subject: [PATCH 5/5] docs: improve assert() message to clarify its purpose MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added descriptive message to assert(class_exists($className)) to explain: - This is an internal logic error, not invalid user input - $className comes from ReflectionParameter type info, so it should always exist - assert() is the appropriate tool for checking "impossible" conditions - Can be disabled in production for performance, unlike explicit exceptions This addresses code review concerns about assert() usage while maintaining the correct semantic distinction between internal logic errors (assert) and user input validation (exceptions). 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/InputQuery.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/InputQuery.php b/src/InputQuery.php index c77bf00..d1e683a 100644 --- a/src/InputQuery.php +++ b/src/InputQuery.php @@ -265,7 +265,8 @@ private function resolveObjectType(ReflectionParameter $param, array $query, arr // Check if the parameter name exists as a direct nested array in query if (array_key_exists($paramName, $query) && is_array($query[$paramName])) { - assert(class_exists($className)); + // This should never fail as $className comes from ReflectionParameter type info + assert(class_exists($className), "Internal logic error: Class '$className' from reflection should exist"); /** @var Query $nestedQuery */ $nestedQuery = $query[$paramName];