| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Debug\FatalErrorHandler; |
| 13 |
|
| 14 |
use Symfony\Component\Debug\Exception\FatalErrorException; |
| 15 |
use Symfony\Component\Debug\Exception\UndefinedFunctionException; |
| 16 |
|
| 17 |
/** |
| 18 |
* ErrorHandler for undefined functions. |
| 19 |
* |
| 20 |
* @author Fabien Potencier <fabien@symfony.com> |
| 21 |
*/ |
| 22 |
class UndefinedFunctionFatalErrorHandler implements FatalErrorHandlerInterface |
| 23 |
{ |
| 24 |
/** |
| 25 |
* {@inheritdoc} |
| 26 |
*/ |
| 27 |
public function handleError(array $error, FatalErrorException $exception) |
| 28 |
{ |
| 29 |
$messageLen = \strlen($error['message']); |
| 30 |
$notFoundSuffix = '()'; |
| 31 |
$notFoundSuffixLen = \strlen($notFoundSuffix); |
| 32 |
if ($notFoundSuffixLen > $messageLen) { |
| 33 |
return null; |
| 34 |
} |
| 35 |
|
| 36 |
if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) { |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
$prefix = 'Call to undefined function '; |
| 41 |
$prefixLen = \strlen($prefix); |
| 42 |
if (0 !== strpos($error['message'], $prefix)) { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 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); |
| 51 |
} else { |
| 52 |
$functionName = $fullyQualifiedFunctionName; |
| 53 |
$message = sprintf('Attempted to call function "%s" from the global namespace.', $functionName); |
| 54 |
} |
| 55 |
|
| 56 |
$candidates = []; |
| 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); |
| 61 |
} else { |
| 62 |
$definedFunctionNameBasename = $definedFunctionName; |
| 63 |
} |
| 64 |
|
| 65 |
if ($definedFunctionNameBasename === $functionName) { |
| 66 |
$candidates[] = '\\'.$definedFunctionName; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if ($candidates) { |
| 72 |
sort($candidates); |
| 73 |
$last = array_pop($candidates).'"?'; |
| 74 |
if ($candidates) { |
| 75 |
$candidates = 'e.g. "'.implode('", "', $candidates).'" or "'.$last; |
| 76 |
} else { |
| 77 |
$candidates = '"'.$last; |
| 78 |
} |
| 79 |
$message .= "\nDid you mean to call ".$candidates; |
| 80 |
} |
| 81 |
|
| 82 |
return new UndefinedFunctionException($message, $exception); |
| 83 |
} |
| 84 |
} |
| 85 |
|