diff --git a/src/Exception/NotCallableException.php b/src/Exception/NotCallableException.php index 7f6cb3c..ae09e84 100644 --- a/src/Exception/NotCallableException.php +++ b/src/Exception/NotCallableException.php @@ -14,7 +14,7 @@ public static function fromInvalidCallable($value, bool $containerEntry = false) { if (is_object($value)) { $message = sprintf('Instance of %s is not a callable', get_class($value)); - } elseif (is_array($value) && isset($value[0], $value[1])) { + } elseif (is_array($value) && array_key_exists(0, $value) && array_key_exists(1, $value)) { $class = is_object($value[0]) ? get_class($value[0]) : $value[0]; $extra = method_exists($class, '__call') || method_exists($class, '__callStatic') @@ -23,9 +23,9 @@ public static function fromInvalidCallable($value, bool $containerEntry = false) $message = sprintf('%s::%s() is not a callable.%s', $class, $value[1], $extra); } elseif ($containerEntry) { - $message = var_export($value, true) . ' is neither a callable nor a valid container entry'; + $message = print_r($value, true) . ' is neither a callable nor a valid container entry'; } else { - $message = var_export($value, true) . ' is not a callable'; + $message = print_r($value, true) . ' is not a callable'; } return new self($message); diff --git a/tests/Exception/NotCallableExceptionTest.php b/tests/Exception/NotCallableExceptionTest.php new file mode 100644 index 0000000..b266a25 --- /dev/null +++ b/tests/Exception/NotCallableExceptionTest.php @@ -0,0 +1,65 @@ +self = $obj; + + $exception = NotCallableException::fromInvalidCallable($obj); + + $this->assertSame('Instance of stdClass is not a callable', $exception->getMessage()); + } + + /** + * @test + */ + #[\PHPUnit\Framework\Attributes\Test] + public function handles_array_with_object_and_null_method() + { + $obj = new stdClass(); + + $exception = NotCallableException::fromInvalidCallable([$obj, null]); + + $this->assertSame('stdClass::() is not a callable.', $exception->getMessage()); + } + + /** + * @test + */ + #[\PHPUnit\Framework\Attributes\Test] + public function handles_circular_array_reference() + { + $a = []; + $a[] = &$a; + + $exception = NotCallableException::fromInvalidCallable($a); + + $this->assertStringContainsString('is not a callable', $exception->getMessage()); + } + + /** + * @test + */ + #[\PHPUnit\Framework\Attributes\Test] + public function handles_circular_array_reference_as_container_entry() + { + $a = []; + $a[] = &$a; + + $exception = NotCallableException::fromInvalidCallable($a, true); + + $this->assertStringContainsString('is neither a callable nor a valid container entry', $exception->getMessage()); + } +}