UndefinedFunctionErrorEnhancer.php 3.0 KB

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