UndefinedFunctionErrorEnhancer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\ErrorHandler\ErrorEnhancer;
  11. use Symfony\Component\ErrorHandler\Error\FatalError;
  12. use Symfony\Component\ErrorHandler\Error\UndefinedFunctionError;
  13. /**
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class UndefinedFunctionErrorEnhancer implements ErrorEnhancerInterface
  17. {
  18. public function enhance(\Throwable $error): ?\Throwable
  19. {
  20. if ($error instanceof FatalError) {
  21. return null;
  22. }
  23. $message = $error->getMessage();
  24. $messageLen = \strlen($message);
  25. $notFoundSuffix = '()';
  26. $notFoundSuffixLen = \strlen($notFoundSuffix);
  27. if ($notFoundSuffixLen > $messageLen) {
  28. return null;
  29. }
  30. if (0 !== substr_compare($message, $notFoundSuffix, -$notFoundSuffixLen)) {
  31. return null;
  32. }
  33. $prefix = 'Call to undefined function ';
  34. $prefixLen = \strlen($prefix);
  35. if (!str_starts_with($message, $prefix)) {
  36. return null;
  37. }
  38. $fullyQualifiedFunctionName = substr($message, $prefixLen, -$notFoundSuffixLen);
  39. if (false !== $namespaceSeparatorIndex = strrpos($fullyQualifiedFunctionName, '\\')) {
  40. $functionName = substr($fullyQualifiedFunctionName, $namespaceSeparatorIndex + 1);
  41. $namespacePrefix = substr($fullyQualifiedFunctionName, 0, $namespaceSeparatorIndex);
  42. $message = \sprintf('Attempted to call function "%s" from namespace "%s".', $functionName, $namespacePrefix);
  43. } else {
  44. $functionName = $fullyQualifiedFunctionName;
  45. $message = \sprintf('Attempted to call function "%s" from the global namespace.', $functionName);
  46. }
  47. $candidates = [];
  48. foreach (get_defined_functions() as $type => $definedFunctionNames) {
  49. foreach ($definedFunctionNames as $definedFunctionName) {
  50. if (false !== $namespaceSeparatorIndex = strrpos($definedFunctionName, '\\')) {
  51. $definedFunctionNameBasename = substr($definedFunctionName, $namespaceSeparatorIndex + 1);
  52. } else {
  53. $definedFunctionNameBasename = $definedFunctionName;
  54. }
  55. if ($definedFunctionNameBasename === $functionName) {
  56. $candidates[] = '\\'.$definedFunctionName;
  57. }
  58. }
  59. }
  60. if ($candidates) {
  61. sort($candidates);
  62. $last = array_pop($candidates).'"?';
  63. if ($candidates) {
  64. $candidates = 'e.g. "'.implode('", "', $candidates).'" or "'.$last;
  65. } else {
  66. $candidates = '"'.$last;
  67. }
  68. $message .= "\nDid you mean to call ".$candidates;
  69. }
  70. return new UndefinedFunctionError($message, $error);
  71. }
  72. }