8000 bug #29190 [Debug][HttpKernel] remove frames added by DebugClassLoade… · symfony/symfony@429f500 · GitHub
[go: up one dir, main page]

Skip to content

Commit 429f500

Browse files
committed
bug #29190 [Debug][HttpKernel] remove frames added by DebugClassLoader in stack traces (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Debug][HttpKernel] remove frames added by DebugClassLoader in stack traces | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28818 | License | MIT | Doc PR | - ![image](https://user-images.githubusercontent.com/243674/48362054-14ee0100-e6a3-11e8-82e1-b185af45fb8b.png) Commits ------- 76e7944 [Debug][HttpKernel] remove frames added by DebugClassLoader in stack traces
2 parents c1c38f5 + 76e7944 commit 429f500

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/Symfony/Component/Debug/ErrorHandler.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ public function handleError($type, $message, $file, $line)
417417
self::$toStringException = null;
418418
} elseif (!$throw && !($type & $level)) {
419419
if (!isset(self::$silencedErrorCache[$id = $file.':'.$line])) {
420-
$lightTrace = $this->tracedErrors & $type ? $this->cleanTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3), $type, $file, $line, false) : array();
421-
$errorAsException = new SilencedErrorContext($type, $file, $line, $lightTrace);
420+
$lightTrace = $this->tracedErrors & $type ? $this->cleanTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5), $type, $file, $line, false) : array();
421+
$errorAsException = new SilencedErrorContext($type, $file, $line, isset($lightTrace[1]) ? array($lightTrace[0]) : $lightTrace);
422422
} elseif (isset(self::$silencedErrorCache[$id][$message])) {
423423
$lightTrace = null;
424424
$errorAsException = self::$silencedErrorCache[$id][$message];
@@ -441,7 +441,6 @@ public function handleError($type, $message, $file, $line)
441441
} else {
442442
$errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
443443

444-
// Clean the trace by removing function arguments and the first frames added by the error handler itself.
445444
if ($throw || $this->tracedErrors & $type) {
446445
$backtrace = $errorAsException->getTrace();
447446
$lightTrace = $this->cleanTrace($backtrace, $type, $file, $line, $throw);
@@ -669,6 +668,9 @@ protected function getFatalErrorHandlers()
669668
);
670669
}
671670

671+
/**
672+
* Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
673+
*/
672674
private function cleanTrace($backtrace, $type, $file, $line, $throw)
673675
{
674676
$lightTrace = $backtrace;
@@ -679,6 +681,13 @@ private function cleanTrace($backtrace, $type, $file, $line, $throw)
679681
break;
680682
}
681683
}
684+
if (class_exists(DebugClassLoader::class, false)) {
685+
for ($i = \count($lightTrace) - 2; 0 < $i; --$i) {
686+
if (DebugClassLoader::class === ($lightTrace[$i]['class'] ?? null)) {
687+
array_splice($lightTrace, --$i, 2);
688+
}
689+
}
690+
}
682691
if (!($throw || $this->scopedErrors & $type)) {
683692
for ($i = 0; isset($lightTrace[$i]); ++$i) {
684693
unset($lightTrace[$i]['args'], $lightTrace[$i]['object']);

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\ConfigCache;
1717
use Symfony\Component\Config\Loader\DelegatingLoader;
1818
use Symfony\Component\Config\Loader\LoaderResolver;
19+
use Symfony\Component\Debug\DebugClassLoader;
1920
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2021
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2122
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -504,21 +505, E31F 28 @@ protected function initializeContainer()
504505
return;
505506
}
506507

507-
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
508+
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
508509
// Clean the trace by removing first frames added by the error handler itself.
509510
for ($i = 0; isset($backtrace[$i]); ++$i) {
510511
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
511512
$backtrace = \array_slice($backtrace, 1 + $i);
512513
break;
513514
}
514515
}
516+
// Remove frames added by DebugClassLoader.
517+
for ($i = \count($backtrace) - 2; 0 < $i; --$i) {
518+
if (DebugClassLoader::class === ($backtrace[$i]['class'] ?? null)) {
519+
$backtrace = array($backtrace[$i + 1]);
520+
break;
521+
}
522+
}
515523

516524
$collectedLogs[$message] = array(
517525
'type' => $type,
518526
'message' => $message,
519527
'file' => $file,
520528
'line' => $line,
521-
'trace' => $backtrace,
529+
'trace' => array($backtrace[0]),
522530
'count' => 1,
523531
);
524532
});

0 commit comments

Comments
 (0)
0