From 8759397b6ae420a1bd5f2f66caf973152a634992 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:16:29 +0200 Subject: [PATCH 1/5] refactor: simplified exceptions --- src/Exception/ProfileCouldNotBeFinished.php | 3 +-- src/Exception/ProfileCouldNotBeProcessed.php | 3 +-- src/Exception/ProfileCouldNotBeStarted.php | 3 +-- src/Exception/ProfileException.php | 2 +- src/Exception/ProfilingException.php | 11 ++++++++++ .../ProfilingHasBeenAlreadyFinished.php | 6 ++++-- src/Exception/ThrowIf.php | 20 ------------------- src/Profile.php | 12 ++++++++--- src/Profiling.php | 9 +++------ 9 files changed, 31 insertions(+), 38 deletions(-) create mode 100644 src/Exception/ProfilingException.php delete mode 100644 src/Exception/ThrowIf.php diff --git a/src/Exception/ProfileCouldNotBeFinished.php b/src/Exception/ProfileCouldNotBeFinished.php index 11e8238..be518ca 100644 --- a/src/Exception/ProfileCouldNotBeFinished.php +++ b/src/Exception/ProfileCouldNotBeFinished.php @@ -7,9 +7,8 @@ use LogicException; /** - * @internal object can apply breaking changes within the same major version + * @internal exception should never be thrown out */ final class ProfileCouldNotBeFinished extends LogicException implements ProfileException { - use ThrowIf; } diff --git a/src/Exception/ProfileCouldNotBeProcessed.php b/src/Exception/ProfileCouldNotBeProcessed.php index 6425ce9..9b03880 100644 --- a/src/Exception/ProfileCouldNotBeProcessed.php +++ b/src/Exception/ProfileCouldNotBeProcessed.php @@ -7,9 +7,8 @@ use LogicException; /** - * @internal object can apply breaking changes within the same major version + * @internal exception should never be thrown out */ final class ProfileCouldNotBeProcessed extends LogicException implements ProfileException { - use ThrowIf; } diff --git a/src/Exception/ProfileCouldNotBeStarted.php b/src/Exception/ProfileCouldNotBeStarted.php index 88b70f3..87d0d04 100644 --- a/src/Exception/ProfileCouldNotBeStarted.php +++ b/src/Exception/ProfileCouldNotBeStarted.php @@ -7,9 +7,8 @@ use LogicException; /** - * @internal object can apply breaking changes within the same major version + * @internal exception should never be thrown out */ final class ProfileCouldNotBeStarted extends LogicException implements ProfileException { - use ThrowIf; } diff --git a/src/Exception/ProfileException.php b/src/Exception/ProfileException.php index 5d070e4..1d29a4f 100644 --- a/src/Exception/ProfileException.php +++ b/src/Exception/ProfileException.php @@ -7,7 +7,7 @@ use Throwable; /** - * @internal interface can apply breaking changes within the same major version + * @internal exception should never be thrown out */ interface ProfileException extends Throwable { diff --git a/src/Exception/ProfilingException.php b/src/Exception/ProfilingException.php new file mode 100644 index 0000000..3f329d0 --- /dev/null +++ b/src/Exception/ProfilingException.php @@ -0,0 +1,11 @@ +state !== ProfileState::Created); + if ($this->state !== ProfileState::Created) { + throw new Exception\ProfileCouldNotBeStarted(); + } $this->state = ProfileState::Started; $this->timeBefore = OptionalFloat::of(microtime(as_float: true)); @@ -61,7 +63,9 @@ public function start(): void */ public function finish(): void { - Exception\ProfileCouldNotBeFinished::throwIf($this->state !== ProfileState::Started); + if ($this->state !== ProfileState::Started) { + throw new Exception\ProfileCouldNotBeFinished(); + } $this->state = ProfileState::Finished; $this->timeAfter = OptionalFloat::of(microtime(as_float: true)); @@ -73,7 +77,9 @@ public function finish(): void */ public function process(callable $processor): mixed { - Exception\ProfileCouldNotBeProcessed::throwIf($this->state !== ProfileState::Finished); + if ($this->state !== ProfileState::Finished) { + throw new Exception\ProfileCouldNotBeProcessed(); + } $output = $this->getOutput(); $processor($this); diff --git a/src/Profiling.php b/src/Profiling.php index 0cd6619..3b6256c 100644 --- a/src/Profiling.php +++ b/src/Profiling.php @@ -29,7 +29,9 @@ public static function start(): self */ public function finish(): ProfileInterface { - Exception\ProfilingHasBeenAlreadyFinished::throwIf($this->wasFinished); + if ($this->wasFinished) { + throw new Exception\ProfilingHasBeenAlreadyFinished(); + } $this->profile->finish(); $this->wasFinished = true; @@ -37,13 +39,8 @@ public function finish(): ProfileInterface return $this->profile; } - /** - * @throws Exception\ProfilingHasBeenAlreadyFinished - */ public function createNestedProfiler(): ProfilerInterface { - Exception\ProfilingHasBeenAlreadyFinished::throwIf($this->wasFinished); - return new class ($this->profile) extends Profiler { /** * @param Profile $parentProfile From 6a9735792d308e7068f2db0fe9f13e0165069c11 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:28:14 +0200 Subject: [PATCH 2/5] refactor: nested profiler factory is now static and internal --- src/Profiler.php | 2 +- src/Profiling.php | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Profiler.php b/src/Profiler.php index 62aa103..f717949 100644 --- a/src/Profiler.php +++ b/src/Profiler.php @@ -14,7 +14,7 @@ public function profile(callable $callable): ProcessableProfileInterface & ProfileWithOutputInterface { $profiling = Profiling::start(); - $output = $callable($profiling->createNestedProfiler()); + $output = $callable(Profiling::createNestedProfiler($profiling)); /** @var Profile $profile */ $profile = $profiling->finish(); $profile->setOutput($output); diff --git a/src/Profiling.php b/src/Profiling.php index 3b6256c..da6d414 100644 --- a/src/Profiling.php +++ b/src/Profiling.php @@ -39,9 +39,12 @@ public function finish(): ProfileInterface return $this->profile; } - public function createNestedProfiler(): ProfilerInterface + /** + * @internal should be used only by {@see Profiler::profile()} + */ + public static function createNestedProfiler(Profiling $profiling): ProfilerInterface { - return new class ($this->profile) extends Profiler { + return new class ($profiling->profile) extends Profiler { /** * @param Profile $parentProfile */ From 35e8e152d52f83458e93ab920652f516429cf964 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:41:23 +0200 Subject: [PATCH 3/5] feat: nested profiler now check state of parent profile on profile --- src/Exception/ParentProfileIsNotStarted.php | 14 ++++++++++++ src/Profile.php | 5 +++++ src/Profiling.php | 24 +++++++++++++-------- 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/Exception/ParentProfileIsNotStarted.php diff --git a/src/Exception/ParentProfileIsNotStarted.php b/src/Exception/ParentProfileIsNotStarted.php new file mode 100644 index 0000000..5a7fdf3 --- /dev/null +++ b/src/Exception/ParentProfileIsNotStarted.php @@ -0,0 +1,14 @@ +outputOption = Optional::empty(); } + public function getState(): ProfileState + { + return $this->state; + } + /** * @throws Exception\ProfileCouldNotBeStarted */ diff --git a/src/Profiling.php b/src/Profiling.php index da6d414..590f5bb 100644 --- a/src/Profiling.php +++ b/src/Profiling.php @@ -6,8 +6,6 @@ final class Profiling { - private bool $wasFinished = false; - /** * @param Profile $profile */ @@ -29,14 +27,13 @@ public static function start(): self */ public function finish(): ProfileInterface { - if ($this->wasFinished) { - throw new Exception\ProfilingHasBeenAlreadyFinished(); - } + try { + $this->profile->finish(); - $this->profile->finish(); - $this->wasFinished = true; - - return $this->profile; + return $this->profile; + } catch (Exception\ProfileException $profileException) { + throw new Exception\ProfilingHasBeenAlreadyFinished(previous: $profileException); + } } /** @@ -53,6 +50,15 @@ public function __construct( ) { $this->parentProfile = $parentProfile; } + + public function profile(callable $callable): ProcessableProfileInterface & ProfileWithOutputInterface + { + if ($this->parentProfile?->getState() !== ProfileState::Started) { + throw new Exception\ParentProfileIsNotStarted(); + } + + return parent::profile($callable); + } }; } } From ce289eb892963b8f342c7d6deacb6e40d9d9d38c Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:51:53 +0200 Subject: [PATCH 4/5] chore: removed unnecessary internal annotation --- src/Exception/ProfileException.php | 3 --- src/Exception/ProfilingHasBeenAlreadyFinished.php | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Exception/ProfileException.php b/src/Exception/ProfileException.php index 1d29a4f..7217cae 100644 --- a/src/Exception/ProfileException.php +++ b/src/Exception/ProfileException.php @@ -6,9 +6,6 @@ use Throwable; -/** - * @internal exception should never be thrown out - */ interface ProfileException extends Throwable { } diff --git a/src/Exception/ProfilingHasBeenAlreadyFinished.php b/src/Exception/ProfilingHasBeenAlreadyFinished.php index 084e3ad..4bc940e 100644 --- a/src/Exception/ProfilingHasBeenAlreadyFinished.php +++ b/src/Exception/ProfilingHasBeenAlreadyFinished.php @@ -7,6 +7,7 @@ use LogicException; /** + * @todo rename to `ProfilingCouldNotBeFinished` * @todo remove implementation of {@see ProfilerException} */ final class ProfilingHasBeenAlreadyFinished extends LogicException implements ProfilerException, ProfilingException From 7e7a83b4f367721027de7ce0e49df2921f9f73e4 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sat, 12 Oct 2024 23:05:42 +0200 Subject: [PATCH 5/5] chore: simplified Dockerfile --- Dockerfile | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index fa32316..4a973fa 100755 --- a/Dockerfile +++ b/Dockerfile @@ -6,15 +6,10 @@ RUN apt-get update \ && apt-get install -y --no-install-recommends \ git \ unzip \ - && php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \ - && php -r "copy('https://composer.github.io/installer.sig', 'composer-setup.php.sig');" \ - && php -r "if (trim(hash_file('SHA384', 'composer-setup.php')) === trim(file_get_contents('composer-setup.php.sig'))) { echo 'Installer verified' . PHP_EOL; exit(0); } else { echo 'Installer corrupt' . PHP_EOL; unlink('composer-setup.php'); unlink('composer-setup.php.sig'); exit(-1); }" \ - && php composer-setup.php \ - && php -r "unlink('composer-setup.php'); unlink('composer-setup.php.sig');" \ - && mv composer.phar /usr/local/bin/composer \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ ; +COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer # endregion # region included composer-library