Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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