From 591e3e163c9b7d775171d772ce56187590b7b3ff Mon Sep 17 00:00:00 2001
From: David Grudl <david@grudl.com>
Date: Tue, 2 Jan 2024 22:25:16 +0100
Subject: [PATCH] removed Helpers::fixStack()

xdebug_get_function_stack() is available only if xdebug.mode=develop is set.
Inside a shutdown handler, it returns the complete callstack only when called from the CLI.
When used in a browser, the callstack is printed out and the function then does not return it.
The callstack output can be disabled using html_errors=0, but the function still won't return it.
---
 src/Tracy/Debugger/Debugger.php               |  2 +-
 src/Tracy/Helpers.php                         | 31 -------------------
 .../Debugger.E_COMPILE_ERROR.console.phpt     | 11 +------
 3 files changed, 2 insertions(+), 42 deletions(-)

diff --git a/src/Tracy/Debugger/Debugger.php b/src/Tracy/Debugger/Debugger.php
index 7e0fdeef5..fe45c539d 100644
--- a/src/Tracy/Debugger/Debugger.php
+++ b/src/Tracy/Debugger/Debugger.php
@@ -289,7 +289,7 @@ public static function shutdownHandler(): void
 	{
 		$error = error_get_last();
 		if (in_array($error['type'] ?? null, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE, E_RECOVERABLE_ERROR, E_USER_ERROR], true)) {
-			self::exceptionHandler(Helpers::fixStack(new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'])));
+			self::exceptionHandler(new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']));
 		} elseif (($error['type'] ?? null) === E_COMPILE_WARNING) {
 			error_clear_last();
 			self::errorHandler($error['type'], $error['message'], $error['file'], $error['line']);
diff --git a/src/Tracy/Helpers.php b/src/Tracy/Helpers.php
index 7bcdcbc7f..9eda3f62d 100644
--- a/src/Tracy/Helpers.php
+++ b/src/Tracy/Helpers.php
@@ -116,37 +116,6 @@ public static function findTrace(array $trace, array|string $method, ?int &$inde
 	}
 
 
-	/** @internal */
-	public static function fixStack(\Throwable $exception): \Throwable
-	{
-		if (function_exists('xdebug_get_function_stack')) {
-			$stack = [];
-			$trace = @xdebug_get_function_stack(); // @ xdebug compatibility warning
-			$trace = array_slice(array_reverse($trace), 2, -1);
-			foreach ($trace as $row) {
-				$frame = [
-					'file' => $row['file'],
-					'line' => $row['line'],
-					'function' => $row['function'] ?? '*unknown*',
-					'args' => [],
-				];
-				if (!empty($row['class'])) {
-					$frame['type'] = isset($row['type']) && $row['type'] === 'dynamic' ? '->' : '::';
-					$frame['class'] = $row['class'];
-				}
-
-				$stack[] = $frame;
-			}
-
-			$ref = new \ReflectionProperty('Exception', 'trace');
-			$ref->setAccessible(true);
-			$ref->setValue($exception, $stack);
-		}
-
-		return $exception;
-	}
-
-
 	/** @internal */
 	public static function errorTypeToString(int $type): string
 	{
diff --git a/tests/Tracy/Debugger.E_COMPILE_ERROR.console.phpt b/tests/Tracy/Debugger.E_COMPILE_ERROR.console.phpt
index ab566eacf..bcf29eb2b 100644
--- a/tests/Tracy/Debugger.E_COMPILE_ERROR.console.phpt
+++ b/tests/Tracy/Debugger.E_COMPILE_ERROR.console.phpt
@@ -24,16 +24,7 @@ $onFatalErrorCalled = false;
 
 register_shutdown_function(function () use (&$onFatalErrorCalled) {
 	Assert::true($onFatalErrorCalled);
-	Assert::match(extension_loaded('xdebug') ?
-'ErrorException: Cannot re-assign $this in %a%
-Stack trace:
-#0 %a%: third()
-#1 %a%: second()
-#2 %a%: first()
-#3 {main}
-Tracy is unable to log error: Logging directory is not specified.
-' :
-'ErrorException: Cannot re-assign $this in %a%
+	Assert::match('ErrorException: Cannot re-assign $this in %a%
 Stack trace:
 #0 [internal function]: Tracy\\Debugger::shutdownHandler()
 #1 {main}