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\UndefinedFunctionException;
15 use Symfony\Component\Debug\Exception\FatalErrorException;
18 * ErrorHandler for undefined functions.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class UndefinedFunctionFatalErrorHandler implements FatalErrorHandlerInterface
27 public function handleError(array $error, FatalErrorException $exception)
29 $messageLen = strlen($error['message']);
30 $notFoundSuffix = '()';
31 $notFoundSuffixLen = strlen($notFoundSuffix);
32 if ($notFoundSuffixLen > $messageLen) {
36 if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) {
40 $prefix = 'Call to undefined function ';
41 $prefixLen = strlen($prefix);
42 if (0 !== strpos($error['message'], $prefix)) {
46 $fullyQualifiedFunctionName = substr($error['message'], $prefixLen, -$notFoundSuffixLen);
47 if (false !== $namespaceSeparatorIndex = strrpos($fullyQualifiedFunctionName, '\\')) {
48 $functionName = substr($fullyQualifiedFunctionName, $namespaceSeparatorIndex + 1);
49 $namespacePrefix = substr($fullyQualifiedFunctionName, 0, $namespaceSeparatorIndex);
50 $message = sprintf('Attempted to call function "%s" from namespace "%s".', $functionName, $namespacePrefix);
52 $functionName = $fullyQualifiedFunctionName;
53 $message = sprintf('Attempted to call function "%s" from the global namespace.', $functionName);
56 $candidates = array();
57 foreach (get_defined_functions() as $type => $definedFunctionNames) {
58 foreach ($definedFunctionNames as $definedFunctionName) {
59 if (false !== $namespaceSeparatorIndex = strrpos($definedFunctionName, '\\')) {
60 $definedFunctionNameBasename = substr($definedFunctionName, $namespaceSeparatorIndex + 1);
62 $definedFunctionNameBasename = $definedFunctionName;
65 if ($definedFunctionNameBasename === $functionName) {
66 $candidates[] = '\\'.$definedFunctionName;
73 $last = array_pop($candidates).'"?';
75 $candidates = 'e.g. "'.implode('", "', $candidates).'" or "'.$last;
77 $candidates = '"'.$last;
79 $message .= "\nDid you mean to call ".$candidates;
82 return new UndefinedFunctionException($message, $exception);