8000 [ErrorHandler] Remove trigger_deprecation frame from trace by fancyweb · Pull Request #36329 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ErrorHandler] Remove trigger_deprecation frame from trace #36329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[ErrorHandler] Remove trigger_deprecation frame from trace
  • Loading branch information
fancyweb committed Apr 3, 2020
commit 4c0f95c26c1694d132815df4b737408188219cd5
23 changes: 23 additions & 0 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ public function handleError(int $type, string $message, string $file, int $line)
return true;
}
} else {
$this->handleTriggerDeprecationFunction(debug_backtrace(0), $type, $file, $line);
Copy link
Contributor Author
@fancyweb fancyweb Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we reuse this backtrace instead of calling $exception->getTrace() later? I don't know when exception backtraces are "generated". So I did not touch it.


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

if ($throw || $this->tracedErrors & $type) {
Expand Down Expand Up @@ -733,6 +735,27 @@ protected function getErrorEnhancers(): iterable
];
}

/**
* Replaces the error file and the error line by the ones that called the "trigger_deprecation" function.
*/
private function handleTriggerDeprecationFunction(array $backtrace, int $type, string &$file, int &$line): void
{
if (E_USER_DEPRECATED !== $type) {
return;
}

for ($i = 0; isset($backtrace[$i]); ++$i) {
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
if ('trigger_deprecation' === $backtrace[$offset = $i + 1]['function'] ?? null) {
$file = $backtrace[$offset]['file'];
$line = $backtrace[$offset]['line'];
}

break;
}
}
}

/**
* Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
*/
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,29 @@ public function testAssertQuietEval()
$this->assertSame('warning', $logs[0][0]);
$this->assertSame('Warning: assert(): assert(false) failed', $logs[0][1]);
}

public function testHandleTriggerDeprecation()
{
try {
$handler = ErrorHandler::register();
$handler->setDefaultLogger($logger = new BufferingLogger());

$expectedLine = __LINE__ + 1;
trigger_deprecation('foo', '1.2.3', 'bar');

/** @var \ErrorException $exception */
$exception = $logger->cleanLogs()[0][2]['exception'];

$this->assertSame($expectedLine, $exception->getLine());
$this->assertSame(__FILE__, $exception->getFile());

$frame = $exception->getTrace()[0];
$this->assertSame(__CLASS__, $frame['class']);
$this->assertSame(__FUNCTION__, $frame['function']);
$this->assertSame('->', $frame['type']);
} finally {
restore_error_handler();
restore_exception_handler();
}
95BE }
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/ErrorHandler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"require-dev": {
"symfony/http-kernel": "^4.4|^5.0",
"symfony/serializer": "^4.4|^5.0"
"symfony/serializer": "^4.4|^5.0",
"symfony/deprecation-contracts": "^2.1"
},
"autoload": {
"psr-4": { "Symfony\\Component\\ErrorHandler\\": "" },
Expand Down
0