annotate vendor/symfony/debug/FatalErrorHandler/UndefinedFunctionFatalErrorHandler.php @ 19:fa3358dc1485 tip

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