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

Skip to content

Commit d9c9aea

Browse files
committed
[ErrorHandler] Parse "x not found" errors correctly on php 8.
1 parent fff8c00 commit d9c9aea

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

src/Symfony/Component/ErrorHandler/ErrorEnhancer/ClassNotFoundErrorEnhancer.php

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,34 @@ public function enhance(\Throwable $error): ?\Throwable
2929
{
3030
// Some specific versions of PHP produce a fatal error when extending a not found class.
3131
$message = !$error instanceof FatalError ? $error->getMessage() : $error->getError()['message'];
32-
$messageLen = \strlen($message);
33-
$notFoundSuffix = '\' not found';
34-
$notFoundSuffixLen = \strlen($notFoundSuffix);
35-
if ($notFoundSuffixLen > $messageLen) {
32+
if (!preg_match('/^(Class|Interface|Trait) [\'"]([^\'"]+)[\'"] not found$/', $message, $matches)) {
3633
return null;
3734
}
38-
39-
if (0 !== substr_compare($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($message, $prefix)) {
47-
continue;
48-
}
49-
50-
$fullyQualifiedClassName = substr($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 ClassNotFoundError($message, $error);
7356
}
57+
$message .= "\nDid you forget a \"use\" statement".$tail;
7458

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

7862
/**

src/Symfony/Component/ErrorHandler/Tests/ErrorEnhancer/ClassNotFoundErrorEnhancerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,30 @@ public function provideClassNotFoundData()
8181
$debugClassLoader = new DebugClassLoader([$autoloader, 'loadClass']);
8282

8383
return [
84+
[
85+
'Class "WhizBangFactory" not found',
86+
"Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement?",
87+
],
8488
[
8589
'Class \'WhizBangFactory\' not found',
8690
"Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement?",
8791
],
92+
[
93+
'Class "Foo\\Bar\\WhizBangFactory" not found',
94+
"Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
95+
],
8896
[
8997
'Class \'Foo\\Bar\\WhizBangFactory\' not found',
9098
"Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
9199
],
100+
[
101+
'Interface "Foo\\Bar\\WhizBangInterface" not found',
102+
"Attempted to load interface \"WhizBangInterface\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
103+
],
104+
[
105+
'Trait "Foo\\Bar\\WhizBangTrait" not found',
106+
"Attempted to load trait \"WhizBangTrait\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
107+
],
92108
[
93109
'Class \'UndefinedFunctionError\' not found',
94110
"Attempted to load class \"UndefinedFunctionError\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony\Component\ErrorHandler\Error\UndefinedFunctionError\"?",

0 commit comments

Comments
 (0)
0