[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/error-handler/ErrorEnhancer/ -> UndefinedFunctionErrorEnhancer.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer