4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Debug\FatalErrorHandler;
14 use Symfony\Component\Debug\Exception\FatalErrorException;
15 use Symfony\Component\Debug\Exception\UndefinedMethodException;
18 * ErrorHandler for undefined methods.
20 * @author Grégoire Pineau <lyrixx@lyrixx.info>
22 class UndefinedMethodFatalErrorHandler implements FatalErrorHandlerInterface
27 public function handleError(array $error, FatalErrorException $exception)
29 preg_match('/^Call to undefined method (.*)::(.*)\(\)$/', $error['message'], $matches);
34 $className = $matches[1];
35 $methodName = $matches[2];
37 $message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className);
39 if (!class_exists($className) || null === $methods = get_class_methods($className)) {
40 // failed to get the class or its methods on which an unknown method was called (for example on an anonymous class)
41 return new UndefinedMethodException($message, $exception);
44 $candidates = array();
45 foreach ($methods as $definedMethodName) {
46 $lev = levenshtein($methodName, $definedMethodName);
47 if ($lev <= strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) {
48 $candidates[] = $definedMethodName;
54 $last = array_pop($candidates).'"?';
56 $candidates = 'e.g. "'.implode('", "', $candidates).'" or "'.$last;
58 $candidates = '"'.$last;
61 $message .= "\nDid you mean to call ".$candidates;
64 return new UndefinedMethodException($message, $exception);