From 2286e8f5b1f86eea5de25e6c67b1af70ba7b71b6 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sat, 12 Oct 2024 23:42:39 +0200 Subject: [PATCH] refactor: nested profiler logic fully moved to nested profiler --- src/Profiler.php | 7 ------- src/Profiling.php | 10 ++++++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Profiler.php b/src/Profiler.php index f717949..ad05ac8 100644 --- a/src/Profiler.php +++ b/src/Profiler.php @@ -6,11 +6,6 @@ /* final */class Profiler implements ProfilerInterface { - /** - * @var Profile|null $parentProfile - */ - protected Profile|null $parentProfile = null; - public function profile(callable $callable): ProcessableProfileInterface & ProfileWithOutputInterface { $profiling = Profiling::start(); @@ -19,8 +14,6 @@ public function profile(callable $callable): ProcessableProfileInterface & Profi $profile = $profiling->finish(); $profile->setOutput($output); - $this->parentProfile?->addChild($profile); - return $profile; // @phpstan-ignore return.type } } diff --git a/src/Profiling.php b/src/Profiling.php index 590f5bb..6664ab0 100644 --- a/src/Profiling.php +++ b/src/Profiling.php @@ -46,18 +46,20 @@ public static function createNestedProfiler(Profiling $profiling): ProfilerInter * @param Profile $parentProfile */ public function __construct( - Profile $parentProfile, + private readonly Profile $parentProfile, ) { - $this->parentProfile = $parentProfile; } public function profile(callable $callable): ProcessableProfileInterface & ProfileWithOutputInterface { - if ($this->parentProfile?->getState() !== ProfileState::Started) { + if ($this->parentProfile->getState() !== ProfileState::Started) { throw new Exception\ParentProfileIsNotStarted(); } - return parent::profile($callable); + $profile = parent::profile($callable); + $this->parentProfile->addChild($profile); + + return $profile; } }; }