Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Debug\FatalErrorHandler; Chris@0: Chris@0: use Symfony\Component\Debug\Exception\FatalErrorException; Chris@0: use Symfony\Component\Debug\Exception\UndefinedMethodException; Chris@0: Chris@0: /** Chris@0: * ErrorHandler for undefined methods. Chris@0: * Chris@0: * @author Grégoire Pineau Chris@0: */ Chris@0: class UndefinedMethodFatalErrorHandler implements FatalErrorHandlerInterface Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function handleError(array $error, FatalErrorException $exception) Chris@0: { Chris@0: preg_match('/^Call to undefined method (.*)::(.*)\(\)$/', $error['message'], $matches); Chris@0: if (!$matches) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $className = $matches[1]; Chris@0: $methodName = $matches[2]; Chris@0: Chris@0: $message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className); Chris@0: Chris@0: if (!class_exists($className) || null === $methods = get_class_methods($className)) { Chris@0: // failed to get the class or its methods on which an unknown method was called (for example on an anonymous class) Chris@0: return new UndefinedMethodException($message, $exception); Chris@0: } Chris@0: Chris@17: $candidates = []; Chris@0: foreach ($methods as $definedMethodName) { Chris@0: $lev = levenshtein($methodName, $definedMethodName); Chris@17: if ($lev <= \strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) { Chris@0: $candidates[] = $definedMethodName; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($candidates) { Chris@0: sort($candidates); Chris@0: $last = array_pop($candidates).'"?'; Chris@0: if ($candidates) { Chris@0: $candidates = 'e.g. "'.implode('", "', $candidates).'" or "'.$last; Chris@0: } else { Chris@0: $candidates = '"'.$last; Chris@0: } Chris@0: Chris@0: $message .= "\nDid you mean to call ".$candidates; Chris@0: } Chris@0: Chris@0: return new UndefinedMethodException($message, $exception); Chris@0: } Chris@0: }