8000 minor #36353 [ErrorHandler] Remove trigger_deprecation frame from tra… · symfony/symfony@607e8d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 607e8d6

Browse files
committed
minor #36353 [ErrorHandler] Remove trigger_deprecation frame from trace (fancyweb, nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [ErrorHandler] Remove trigger_deprecation frame from trace | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This accounts for the new `trigger_deprecation` function from [symfony/deprecation-contracts](https://github.com/symfony/deprecation-contracts). Replaces #36329 Commits ------- d4eb4a4 [ErrorHandler] Remove trigger_deprecation frame from trace (add tests) c293aee [ErrorHandler] Remove trigger_deprecation frame from trace
2 parents 1ee1c81 + d4eb4a4 commit 607e8d6

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

src/Symfony/Component/ErrorHandler/ErrorHandler.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class ErrorHandler
9191
private $tracedErrors = 0x77FB; // E_ALL - E_STRICT - E_PARSE
9292
private $screamedErrors = 0x55; // E_ERROR + E_CORE_ERROR + E_COMPILE_ERROR + E_PARSE
9393
private $loggedErrors = 0;
94-
private $traceReflector;
94+
private $configureException;
9595
private $debug;
9696

9797
private $isRecursive = 0;
@@ -187,8 +187,14 @@ public function __construct(BufferingLogger $bootstrappingLogger = null, bool $d
187187
$this->bootstrappingLogger = $bootstrappingLogger;
188188
$this->setDefaultLogger($bootstrappingLogger);
189189
}
190-
$this->traceReflector = new \ReflectionProperty('Exception', 'trace');
191-
$this->traceReflector->setAccessible(true);
190+
$traceReflector = new \ReflectionProperty('Exception', 'trace');
191+
$traceReflector->setAccessible(true);
192+
$this->configureException = \Closure::bind(static function ($e, $trace, $file = null, $line = null) use ($traceReflector) {
193+
$traceReflector->setValue($e, $trace);
194+
$e->file = $file ?? $e->file;
195+
$e->line = $line ?? $e->line;
196+
}, null, new class() extends \Exception {
197+
});
192198
$this->debug = $debug;
193199
}
194200

@@ -473,9 +479,9 @@ public function handleError(int $type, string $message, string $file, int $line)
473479
if ($throw || $this->tracedErrors & $type) {
474480
$backtrace = $errorAsException->getTrace();
475481
$lightTrace = $this->cleanTrace($backtrace, $type, $file, $line, $throw);
476-
$this->traceReflector->setValue($errorAsException, $lightTrace);
482+
($this->configureException)($errorAsException, $lightTrace, $file, $line);
477483
} else {
478-
$this->traceReflector->setValue($errorAsException, []);
484+
($this->configureException)($errorAsException, []);
479485
$backtrace = [];
480486
}
481487
}
@@ -736,7 +742,7 @@ protected function getErrorEnhancers(): iterable
736742
/**
737743
* Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
738744
*/
739-
private function cleanTrace(array $backtrace, int $type, string $file, int $line, bool $throw): array
745+
private function cleanTrace(array $backtrace, int $type, string &$file, int &$line, bool $throw): array
740746
{
741747
$lightTrace = $backtrace;
742748

@@ -746,6 +752,19 @@ private function cleanTrace(array $backtrace, int $type, string $file, int $line
746752
break;
747753
}
748754
}
755+
if (E_USER_DEPRECATED === $type) {
756+
for ($i = 0; isset($lightTrace[$i]); ++$i) {
757+
if (!isset($lightTrace[$i]['file'], $lightTrace[$i]['line'], $lightTrace[$i]['function'])) {
758+
continue;
759+
}
760+
if (!isset($lightTrace[$i]['class']) && 'trigger_deprecation' === $lightTrace[$i]['function']) {
761+
$file = $lightTrace[$i]['file'];
762+
$line = $lightTrace[$i]['line'];
763+
$lightTrace = \array_slice($lightTrace, 1 + $i);
764+
break;
765+
}
766+
}
767+
}
749768
if (class_exists(DebugClassLoader::class, false)) {
750769
for ($i = \count($lightTrace) - 2; 0 < $i; --$i) {
751770
if (DebugClassLoader::class === ($lightTrace[$i]['class'] ?? null)) {

src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,4 +652,29 @@ public function testAssertQuietEval()
652652
$this->assertSame('warning', $logs[0][0]);
653653
$this->assertSame('Warning: assert(): assert(false) failed', $logs[0][1]);
654654
}
655+
656+
public function testHandleTriggerDeprecation()
657+
{
658+
try {
659+
$handler = ErrorHandler::register();
660+
$handler->setDefaultLogger($logger = new BufferingLogger());
661+
662+
$expectedLine = __LINE__ + 1;
663+
trigger_deprecation('foo', '1.2.3', 'bar');
664+
665+
/** @var \ErrorException $exception */
666+
$exception = $logger->cleanLogs()[0][2]['exception'];
667+
668+
$this->assertSame($expectedLine, $exception->getLine());
669+
$this->assertSame(__FILE__, $exception->getFile());
670+
671+
$frame = $exception->getTrace()[0];
672+
$this->assertSame(__CLASS__, $frame['class']);
673+
$this->assertSame(__FUNCTION__, $frame['function']);
674+
$this->assertSame('->', $frame['type']);
675+
} finally {
676+
restore_error_handler();
677+
restore_exception_handler();
678+
}
679+
}
655680
}

src/Symfony/Component/ErrorHandler/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
},
2424
"require-dev": {
2525
"symfony/http-kernel": "^4.4|^5.0",
26-
"symfony/serializer": "^4.4|^5.0"
26+
"symfony/serializer": "^4.4|^5.0",
27+
"symfony/deprecation-contracts": "^2.1"
2728
},
2829
"autoload": {
2930
"psr-4": { "Symfony\\Component\\ErrorHandler\\": "" },

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,18 @@ protected function initializeContainer()
491491
break;
492492
}
493493
}
494+
for ($i = 0; isset($backtrace[$i]); ++$i) {
495+
if (!isset($backtrace[$i]['file'], $backtrace[$i]['line'], $backtrace[$i]['function'])) {
496+
continue;
497+
}
498+
if (!isset($backtrace[$i]['class']) && 'trigger_deprecation' === $backtrace[$i]['function']) {
499+
$file = $backtrace[$i]['file'];
500+
$line = $backtrace[$i]['line'];
501+
$backtrace = \array_slice($backtrace, 1 + $i);
502+
break;
503+
}
504+
}
505+
494506
// Remove frames added by DebugClassLoader.
495507
for ($i = \count($backtrace) - 2; 0 < $i; --$i) {
496508
if (\in_array($backtrace[$i]['class'] ?? null, [DebugClassLoader::class, LegacyDebugClassLoader::class], true)) {

0 commit comments

Comments
 (0)
0