Skip to content

Commit

Permalink
bug #54031 [ErrorHandler] Fix parsing messages that contain anonymous…
Browse files Browse the repository at this point in the history
… classes on PHP >= 8.3.3 (nicolas-grekas)

This PR was merged into the 5.4 branch.

Discussion
----------

[ErrorHandler] Fix parsing messages that contain anonymous classes on PHP >= 8.3.3

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Follows php/php-src#13153 and #40065

Commits
-------

699f400 [ErrorHandler] Fix parsing messages that contain anonymous classes on PHP >= 8.3.3
  • Loading branch information
nicolas-grekas committed Feb 22, 2024
2 parents 526bb8b + 699f400 commit 21afb26
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Expand Up @@ -1097,7 +1097,7 @@ private function fixReturnStatements(\ReflectionMethod $method, string $returnTy
$braces = 0;
for (; $i < $end; ++$i) {
if (!$inClosure) {
$inClosure = str_contains($code[$i], 'function (');
$inClosure = false !== strpos($code[$i], 'function (');
}

if ($inClosure) {
Expand Down
10 changes: 7 additions & 3 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Expand Up @@ -457,23 +457,27 @@ public function handleError(int $type, string $message, string $file, int $line)
return true;
}
} else {
if (false !== strpos($message, '@anonymous')) {
if (PHP_VERSION_ID < 80303 && false !== strpos($message, '@anonymous')) {
$backtrace = debug_backtrace(false, 5);

for ($i = 1; isset($backtrace[$i]); ++$i) {
if (isset($backtrace[$i]['function'], $backtrace[$i]['args'][0])
&& ('trigger_error' === $backtrace[$i]['function'] || 'user_error' === $backtrace[$i]['function'])
) {
if ($backtrace[$i]['args'][0] !== $message) {
$message = $this->parseAnonymousClass($backtrace[$i]['args'][0]);
$logMessage = $this->levels[$type].': '.$message;
$message = $backtrace[$i]['args'][0];
}

break;
}
}
}

if (false !== strpos($message, "@anonymous\0")) {
$message = $this->parseAnonymousClass($message);
$logMessage = $this->levels[$type].': '.$message;
}

$errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);

if ($throw || $this->tracedErrors & $type) {
Expand Down

0 comments on commit 21afb26

Please sign in to comment.