Skip to content

Commit

Permalink
Merge pull request #5 from petrknap/chores
Browse files Browse the repository at this point in the history
Improved isolation between internal and non-internal code base
  • Loading branch information
petrknap authored Oct 12, 2024
2 parents 8d84225 + 7e7a83b commit a2bdd8c
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 53 deletions.
7 changes: 1 addition & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/Exception/ParentProfileIsNotStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Profiler\Exception;

use LogicException;

/**
* @internal exception should never be thrown out
*/
final class ParentProfileIsNotStarted extends LogicException implements ProfilerException
{
}
3 changes: 1 addition & 2 deletions src/Exception/ProfileCouldNotBeFinished.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 1 addition & 2 deletions src/Exception/ProfileCouldNotBeProcessed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 1 addition & 2 deletions src/Exception/ProfileCouldNotBeStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 0 additions & 3 deletions src/Exception/ProfileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Throwable;

/**
* @internal interface can apply breaking changes within the same major version
*/
interface ProfileException extends Throwable
{
}
11 changes: 11 additions & 0 deletions src/Exception/ProfilingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Profiler\Exception;

use Throwable;

interface ProfilingException extends Throwable
{
}
7 changes: 5 additions & 2 deletions src/Exception/ProfilingHasBeenAlreadyFinished.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use LogicException;

final class ProfilingHasBeenAlreadyFinished extends LogicException implements ProfilerException
/**
* @todo rename to `ProfilingCouldNotBeFinished`
* @todo remove implementation of {@see ProfilerException}
*/
final class ProfilingHasBeenAlreadyFinished extends LogicException implements ProfilerException, ProfilingException
{
use ThrowIf;
}
20 changes: 0 additions & 20 deletions src/Exception/ThrowIf.php

This file was deleted.

17 changes: 14 additions & 3 deletions src/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ public function __construct()
$this->outputOption = Optional::empty();
}

public function getState(): ProfileState
{
return $this->state;
}

/**
* @throws Exception\ProfileCouldNotBeStarted
*/
public function start(): void
{
Exception\ProfileCouldNotBeStarted::throwIf($this->state !== ProfileState::Created);
if ($this->state !== ProfileState::Created) {
throw new Exception\ProfileCouldNotBeStarted();
}

$this->state = ProfileState::Started;
$this->timeBefore = OptionalFloat::of(microtime(as_float: true));
Expand All @@ -61,7 +68,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));
Expand All @@ -73,7 +82,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);
Expand Down
2 changes: 1 addition & 1 deletion src/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $profile */
$profile = $profiling->finish();
$profile->setOutput($output);
Expand Down
30 changes: 18 additions & 12 deletions src/Profiling.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class Profiling
{
private bool $wasFinished = false;

/**
* @param Profile<mixed> $profile
*/
Expand All @@ -29,22 +27,21 @@ public static function start(): self
*/
public function finish(): ProfileInterface
{
Exception\ProfilingHasBeenAlreadyFinished::throwIf($this->wasFinished);

$this->profile->finish();
$this->wasFinished = true;
try {
$this->profile->finish();

return $this->profile;
return $this->profile;
} catch (Exception\ProfileException $profileException) {
throw new Exception\ProfilingHasBeenAlreadyFinished(previous: $profileException);
}
}

/**
* @throws Exception\ProfilingHasBeenAlreadyFinished
* @internal should be used only by {@see Profiler::profile()}
*/
public function createNestedProfiler(): ProfilerInterface
public static function createNestedProfiler(Profiling $profiling): ProfilerInterface
{
Exception\ProfilingHasBeenAlreadyFinished::throwIf($this->wasFinished);

return new class ($this->profile) extends Profiler {
return new class ($profiling->profile) extends Profiler {
/**
* @param Profile<mixed> $parentProfile
*/
Expand All @@ -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);
}
};
}
}

0 comments on commit a2bdd8c

Please sign in to comment.