From 094710572cf055011680b42d39f34c59457d5723 Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Wed, 21 Dec 2016 12:36:34 +0100 Subject: [PATCH 1/2] Handle anonymous class without methods correctly. --- .../UndefinedMethodFatalErrorHandler.php | 6 +++++- .../UndefinedMethodFatalErrorHandlerTest.php | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php b/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php index f734d6bb7dd9e..50fd9556d700b 100644 --- a/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php +++ b/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php @@ -36,8 +36,12 @@ public function handleError(array $error, FatalErrorException $exception) $message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className); + if (null === $methods = get_class_methods($className)) { + return new UndefinedMethodException($message, $exception); + } + $candidates = array(); - foreach (get_class_methods($className) as $definedMethodName) { + foreach ($methods as $definedMethodName) { $lev = levenshtein($methodName, $definedMethodName); if ($lev <= strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) { $candidates[] = $definedMethodName; diff --git a/src/Symfony/Component/Debug/Tests/FatalErrorHandler/UndefinedMethodFatalErrorHandlerTest.php b/src/Symfony/Component/Debug/Tests/FatalErrorHandler/UndefinedMethodFatalErrorHandlerTest.php index de7b21c69978b..22cbc3033bd6c 100644 --- a/src/Symfony/Component/Debug/Tests/FatalErrorHandler/UndefinedMethodFatalErrorHandlerTest.php +++ b/src/Symfony/Component/Debug/Tests/FatalErrorHandler/UndefinedMethodFatalErrorHandlerTest.php @@ -61,6 +61,15 @@ public function provideUndefinedMethodData() ), "Attempted to call an undefined method named \"offsetFet\" of class \"SplObjectStorage\".\nDid you mean to call e.g. \"offsetGet\", \"offsetSet\" or \"offsetUnset\"?", ), + array( + array( + 'type' => 1, + 'message' => 'Call to undefined method class@anonymous::test()', + 'file' => '/home/possum/work/symfony/test.php', + 'line' => 11, + ), + 'Attempted to call an undefined method named "test" of class "class@anonymous".', + ), ); } } From 591b2cad06811e8940ec0ab4aba0f3e27b176e2b Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Tue, 27 Dec 2016 07:11:49 +0100 Subject: [PATCH 2/2] Update after review. --- .../FatalErrorHandler/UndefinedMethodFatalErrorHandler.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php b/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php index 50fd9556d700b..6fa62b6f24fbb 100644 --- a/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php +++ b/src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php @@ -36,7 +36,8 @@ public function handleError(array $error, FatalErrorException $exception) $message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className); - if (null === $methods = get_class_methods($className)) { + if (!class_exists($className) || null === $methods = get_class_methods($className)) { + // failed to get the class or its methods on which an unknown method was called (for example on an anonymous class) return new UndefinedMethodException($message, $exception); } @@ -56,6 +57,7 @@ public function handleError(array $error, FatalErrorException $exception) } else { $candidates = '"'.$last; } + $message .= "\nDid you mean to call ".$candidates; }