From cb7f2a4dfca4bcae65fc4da6724d8a69855dd8f3 Mon Sep 17 00:00:00 2001 From: Matthias Vogel Date: Fri, 23 May 2025 14:56:53 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20singleton=20handling=20?= =?UTF-8?q?=E2=9C=85=20add=20test=20for=20this=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Classes/Utility/TimingUtility.php | 4 +++- Tests/TimingUtilityTest.php | 4 ++++ phpstan-baseline.neon | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Classes/Utility/TimingUtility.php b/Classes/Utility/TimingUtility.php index 05814b6..e00367a 100644 --- a/Classes/Utility/TimingUtility.php +++ b/Classes/Utility/TimingUtility.php @@ -22,6 +22,8 @@ final class TimingUtility implements SingletonInterface { public const MAX_SINGLE_HEADER_SIZE = 2 ** 12; + private static ?TimingUtility $instance = null; + private bool $registered = false; /** @var bool */ @@ -41,7 +43,7 @@ public function __construct(private readonly RegisterShutdownFunctionInterface $ public static function getInstance(): TimingUtility { - return GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ diff --git a/Tests/TimingUtilityTest.php b/Tests/TimingUtilityTest.php index dabdcd4..04f5176 100644 --- a/Tests/TimingUtilityTest.php +++ b/Tests/TimingUtilityTest.php @@ -48,6 +48,10 @@ protected function tearDown(): void public function getInstance(): void { self::assertInstanceOf(TimingUtility::class, $firstCall = TimingUtility::getInstance()); + // even if we clear GeneralUtility, the instance should be the same + // we measure so erly in TYPO3s bootstrap, that GeneralUtility singletons are cleared after our first measurement + // but we need to be sure, that the instance is not recreated as that will run the shutdown function again, and that is not good! + GeneralUtility::resetSingletonInstances([]); self::assertSame($firstCall, TimingUtility::getInstance()); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index bffe951..bd8c450 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -789,7 +789,7 @@ parameters: path: Tests/TimingUtilityTest.php - - message: '#^Method Kanti\\ServerTiming\\Service\\SentryServiceInterface@anonymous/Tests/TimingUtilityTest\.php\:305\:\:sendSentryTrace\(\) never returns null so it can be removed from the return type\.$#' + message: '#^Method Kanti\\ServerTiming\\Service\\SentryServiceInterface@anonymous/Tests/TimingUtilityTest\.php\:[0-9]+\:\:sendSentryTrace\(\) never returns null so it can be removed from the return type\.$#' identifier: return.unusedType count: 1 path: Tests/TimingUtilityTest.php