Repository: MarcinOrlowski/laravel-api-response-builder (php)

Issue:
ToArrayConverter: Missing check for toArray method existence
Missing Method Check in ToArrayConverter: src/Converters/ToArrayConverter.php fails to check for the existence of the toArray method before calling it, risking fatal errors.

Pull request description:
Fix: Add method_exists check in ToArrayConverter (closes #256)
This PR addresses issue #256 by adding a check in ToArrayConverter to ensure the toArray method exists before calling it. Includes unit tests and changelog update.

Patch:
diff --git a/docs/CHANGES.md b/docs/CHANGES.md
index fc50858..c621ca4 100644
--- a/docs/CHANGES.md
+++ b/docs/CHANGES.md
@@ -24,6 +24,7 @@ should be able to easily backport future new features to older versions rather e
 
 * v12.0.0 (2025-04-16)
   * Added support for Laravel v12.
+  * [BR-256] Fixed `ToArrayConverter` potentially causing fatal error if object lacks `toArray` method.
 
 * v11.0.0 (2024-05-06)
   * **BACKWARD INCOMPATIBLE CHANGES** ([more info](compatibility.md)).
diff --git a/src/Converters/ToArrayConverter.php b/src/Converters/ToArrayConverter.php
index cc14099..32d8fc3 100644
--- a/src/Converters/ToArrayConverter.php
+++ b/src/Converters/ToArrayConverter.php
@@ -37,8 +37,15 @@ final class ToArrayConverter implements ConverterContract
     {
         Validator::assertIsObject('obj', $obj);
 
+        if (!\method_exists($obj, 'toArray')) {
+            $cls = \get_class($obj);
+            throw new \InvalidArgumentException(
+                "Object of class '{$cls}' does not have a 'toArray' method."
+            );
+        }
+
         /** @var JsonResource $obj */
-        $request = request();
+        $request = \request();
         return (array)$obj->toArray($request);
     }
 


Test patch:
diff --git a/tests/phpunit/Converter/ConverterTest.php b/tests/phpunit/Converter/ConverterTest.php
index b8261c4..58fabf0 100644
--- a/tests/phpunit/Converter/ConverterTest.php
+++ b/tests/phpunit/Converter/ConverterTest.php
@@ -80,4 +80,29 @@ class ConverterTest extends TestCase
         $this->assertEquals($child_val, $result[ TestModel::FIELD_NAME ]);
     }
 
+    /**
+     * Checks if ToArrayConverter throws exception for object without toArray method.
+     */
+    public function testToArrayConverterThrowsExceptionForObjectWithoutToArrayMethod(): void
+    {
+        // GIVEN an object without a toArray method
+        $obj = new \stdClass();
+        $obj->foo = Generator::getRandomString('foo');
+
+        // HAVING configuration mapping stdClass to ToArrayConverter
+        $key = Generator::getRandomString('key');
+        Config::set(RB::CONF_KEY_CONVERTER_CLASSES, [
+            \get_class($obj) => [
+                RB::KEY_HANDLER => ToArrayConverter::class,
+                RB::KEY_KEY     => $key,
+            ],
+        ]);
+
+        // THEN we expect an exception
+        $this->expectException(\InvalidArgumentException::class);
+
+        // WHEN we try to convert the object
+        (new Converter())->convert($obj);
+    }
+
 } // end of class