No Description

UndefinedFunctionFatalErrorHandler.php 3.4KB

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