@@ -29,50 +29,34 @@ class ClassNotFoundFatalErrorHandler implements FatalErrorHandlerInterface
29
29
*/
30
30
public function handleError (array $ error , FatalErrorException $ exception )
31
31
{
32
- $ messageLen = \strlen ($ error ['message ' ]);
33
- $ notFoundSuffix = '\' not found ' ;
34
- $ notFoundSuffixLen = \strlen ($ notFoundSuffix );
35
- if ($ notFoundSuffixLen > $ messageLen ) {
32
+ if (!preg_match ('/^(Class|Interface|Trait) [ \'"]([^ \'"]+)[ \'"] not found$/ ' , $ error ['message ' ], $ matches )) {
36
33
return null ;
37
34
}
38
-
39
- if (0 !== substr_compare ($ error ['message ' ], $ notFoundSuffix , -$ notFoundSuffixLen )) {
40
- return null ;
35
+ $ typeName = strtolower ($ matches [1 ]);
36
+ $ fullyQualifiedClassName = $ matches [2 ];
37
+
38
+ if (false !== $ namespaceSeparatorIndex = strrpos ($ fullyQualifiedClassName , '\\' )) {
39
+ $ className = substr ($ fullyQualifiedClassName , $ namespaceSeparatorIndex + 1 );
40
+ $ namespacePrefix = substr ($ fullyQualifiedClassName , 0 , $ namespaceSeparatorIndex );
41
+ $ message = sprintf ('Attempted to load %s "%s" from namespace "%s". ' , $ typeName , $ className , $ namespacePrefix );
42
+ $ tail = ' for another namespace? ' ;
43
+ } else {
44
+ $ className = $ fullyQualifiedClassName ;
45
+ $ message = sprintf ('Attempted to load %s "%s" from the global namespace. ' , $ typeName , $ className );
46
+ $ tail = '? ' ;
41
47
}
42
48
43
- foreach (['class ' , 'interface ' , 'trait ' ] as $ typeName ) {
44
- $ prefix = ucfirst ($ typeName ).' \'' ;
45
- $ prefixLen = \strlen ($ prefix );
46
- if (0 !== strpos ($ error ['message ' ], $ prefix )) {
47
- continue ;
48
- }
49
-
50
- $ fullyQualifiedClassName = substr ($ error ['message ' ], $ prefixLen , -$ notFoundSuffixLen );
51
- if (false !== $ namespaceSeparatorIndex = strrpos ($ fullyQualifiedClassName , '\\' )) {
52
- $ className = substr ($ fullyQualifiedClassName , $ namespaceSeparatorIndex + 1 );
53
- $ namespacePrefix = substr ($ fullyQualifiedClassName , 0 , $ namespaceSeparatorIndex );
54
- $ message = sprintf ('Attempted to load %s "%s" from namespace "%s". ' , $ typeName , $ className , $ namespacePrefix );
55
- $ tail = ' for another namespace? ' ;
49
+ if ($ candidates = $ this ->getClassCandidates ($ className )) {
50
+ $ tail = array_pop ($ candidates ).'"? ' ;
51
+ if ($ candidates ) {
52
+ $ tail = ' for e.g. " ' .implode ('", " ' , $ candidates ).'" or " ' .$ tail ;
56
53
} else {
57
- $ className = $ fullyQualifiedClassName ;
58
- $ message = sprintf ('Attempted to load %s "%s" from the global namespace. ' , $ typeName , $ className );
59
- $ tail = '? ' ;
54
+ $ tail = ' for " ' .$ tail ;
60
55
}
61
-
62
- if ($ candidates = $ this ->getClassCandidates ($ className )) {
63
- $ tail = array_pop ($ candidates ).'"? ' ;
64
- if ($ candidates ) {
65
- $ tail = ' for e.g. " ' .implode ('", " ' , $ candidates ).'" or " ' .$ tail ;
66
- } else {
67
- $ tail = ' for " ' .$ tail ;
68
- }
69
- }
70
- $ message .= "\nDid you forget a \"use \" statement " .$ tail ;
71
-
72
- return new ClassNotFoundException ($ message , $ exception );
73
56
}
57
+ $ message .= "\nDid you forget a \"use \" statement " .$ tail ;
74
58
75
- return null ;
59
+ return new ClassNotFoundException ( $ message , $ exception ) ;
76
60
}
77
61
78
62
/**
0 commit comments