8000 [Debug] Parse "x not found" errors correctly on php 8. · symfony/symfony@275496a · GitHub
[go: up one dir, main page]

Skip to content

Commit 275496a

Browse files
committed
[Debug] Parse "x not found" errors correctly on php 8.
1 parent 4351a70 commit 275496a

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

src/Symfony/Component/Debug/FatalErrorHandler/ClassNotFoundFatalErrorHandler.php

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,34 @@ class ClassNotFoundFatalErrorHandler implements FatalErrorHandlerInterface
2929
*/
3030
public function handleError(array $error, FatalErrorException $exception)
3131
{
32-
$messageLen = \strlen($error['message']);
33-
$notFoundSuffix = '\' not found';
34-
$notFoundSuffixLen = \strlen($notFoundSuffix);
35-
if ($notFoundSuffixLen & 8000 gt; $messageLen) {
32+
if (!preg_match('/^(Class|Interface|Trait) [\'"]([^\'"]+)[\'"] not found$/', $error['message'], $matches)) {
3633
return null;
3734
}
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 = '?';
4147
}
4248

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;
5653
} else {
57-
$className = $fullyQualifiedClassName;
58-
$message = sprintf('Attempted to load %s "%s" from the global namespace.', $typeName, $className);
59-
$tail = '?';
54+
$tail = ' for "'.$tail;
6055
}
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);
7356
}
57+
$message .= "\nDid you forget a \"use\" statement".$tail;
7458

75-
return null;
59+
return new ClassNotFoundException($message, $exception);
7660
}
7761

7862
/**

src/Symfony/Component/Debug/Tests/FatalErrorHandler/ClassNotFoundFatalErrorHandlerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public function provideClassNotFoundData()
8080
$debugClassLoader = new DebugClassLoader([$autoloader, 'loadClass']);
8181

8282
return [
83+
[
84+
[
85+
'type' => 1,
86+
'line' => 12,
87+
'file' => 'foo.php',
88+
'message' => 'Class "WhizBangFactory" not found',
89+
],
90+
"/^Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement\?$/",
91+
],
8392
[
8493
[
8594
'type' => 1,
@@ -98,6 +107,33 @@ public function provideClassNotFoundData()
98107
],
99108
"/^Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/",
100109
],
110+
[
111+
[
112+
'type' => 1,
113+
'line' => 12,
114+
'file' => 'foo.php',
115+
'message' => 'Class "Foo\\Bar\\WhizBangFactory" not found',
116+
],
117+
"/^Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/",
118+
],
119+
[
120+
[
121+
'type' => 1,
122+
'line' => 12,
123+
'file' => 'foo.php',
124+
'message' => 'Interface "Foo\\Bar\\WhizBangInterface" not found',
125+
],
126+
"/^Attempted to load interface \"WhizBangInterface\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/",
127+
],
128+
[
129+
[
130+
'type' => 1,
131+
'line' => 12,
132+
'file' => 'foo.php',
133+
'message' => 'Trait "Foo\\Bar\\WhizBangTrait" not found',
134+
],
135+
"/^Attempted to load trait \"WhizBangTrait\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/",
136+
],
101137
[
102138
[
103139
'type' => 1,

0 commit comments

Comments
 (0)
0