[Debug] UndefinedMethodFatalErrorHandler - Handle anonymous classes

This commit is contained in:
SpacePossum 2016-12-21 12:36:34 +01:00 committed by Nicolas Grekas
parent 948b44c461
commit ed713aef2b
2 changed files with 16 additions and 1 deletions

View File

@ -36,8 +36,13 @@ class UndefinedMethodFatalErrorHandler implements FatalErrorHandlerInterface
$message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className); $message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $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);
}
$candidates = array(); $candidates = array();
foreach (get_class_methods($className) as $definedMethodName) { foreach ($methods as $definedMethodName) {
$lev = levenshtein($methodName, $definedMethodName); $lev = levenshtein($methodName, $definedMethodName);
if ($lev <= strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) { if ($lev <= strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) {
$candidates[] = $definedMethodName; $candidates[] = $definedMethodName;
@ -52,6 +57,7 @@ class UndefinedMethodFatalErrorHandler implements FatalErrorHandlerInterface
} else { } else {
$candidates = '"'.$last; $candidates = '"'.$last;
} }
$message .= "\nDid you mean to call ".$candidates; $message .= "\nDid you mean to call ".$candidates;
} }

View File

@ -61,6 +61,15 @@ class UndefinedMethodFatalErrorHandlerTest extends \PHPUnit_Framework_TestCase
), ),
"Attempted to call an undefined method named \"offsetFet\" of class \"SplObjectStorage\".\nDid you mean to call e.g. \"offsetGet\", \"offsetSet\" or \"offsetUnset\"?", "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".',
),
); );
} }
} }