Skip to content

Commit

Permalink
Improving Cloak implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 25, 2023
1 parent e759b9d commit b120006
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
61 changes: 59 additions & 2 deletions src/Error/Cloak.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,77 @@ public function __invoke(mixed ...$arguments): mixed
return true;
};

$this->exception = null;
set_error_handler($errorHandler, $this->errorLevel);
$result = ($this->closure)(...$arguments);
restore_error_handler();

return match (true) {
null === $this->exception,
null === $this->exception, /* @phpstan-ignore-line */
self::SILENCE_ERROR === $this->behaviour,
false === self::$useException => $result,
default => throw $this->exception,
default => throw $this->exception, /* @phpstan-ignore-line */
};
}

public function lastError(): ?ErrorException
{
return $this->exception;
}

public function errorsAreSilenced(): bool
{
return !$this->errorsAreThrown();
}

public function errorsAreThrown(): bool
{
return self::THROW_ON_ERROR === $this->behaviour
|| true === self::$useException;
}

public function suppressAll(): bool
{
return $this->suppress(E_ALL);
}

public function suppressWarning(): bool
{
return $this->suppress(E_WARNING);
}

public function suppressNotice(): bool
{
return $this->suppress(E_NOTICE);
}

public function suppressDeprecated(): bool
{
return $this->suppress(E_DEPRECATED);
}

public function suppressStrict(): bool
{
return $this->suppress(E_STRICT);
}

public function suppressUserWarning(): bool
{
return $this->suppress(E_USER_WARNING);
}

public function suppressUserNotice(): bool
{
return $this->suppress(E_USER_NOTICE);
}

public function suppressUserDeprecated(): bool
{
return $this->suppress(E_USER_DEPRECATED);
}

public function suppress(int $errorLevel): bool
{
return 0 !== ($errorLevel & $this->errorLevel);
}
}
22 changes: 22 additions & 0 deletions src/Error/CloakTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function it_returns_information_about_its_error_reporting_level(): void

self::assertFalse($res);
self::assertSame(E_WARNING, $lambda->errorLevel);
self::assertTrue($lambda->suppressWarning());
self::assertFalse($lambda->suppressNotice());
self::assertInstanceOf(ErrorException::class, $lambda->lastError());
}

Expand Down Expand Up @@ -120,4 +122,24 @@ public function testCaptureNothingThrowNoException(): void

self::assertSame('FOO', $strtoupper('foo'));
}

#[Test]
public function it_can_detect_the_level_to_suppress(): void
{
$touch = new Cloak(
touch(...),
E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED,
Cloak::THROW_ON_ERROR
);

self::assertTrue($touch->suppressAll());
self::assertFalse($touch->suppressStrict());
self::assertFalse($touch->suppressDeprecated());
self::assertFalse($touch->suppressNotice());
self::assertTrue($touch->suppressUserNotice());
self::assertTrue($touch->suppressUserDeprecated());
self::assertTrue($touch->suppressUserWarning());
self::assertTrue($touch->errorsAreThrown());
self::assertFalse($touch->errorsAreSilenced());
}
}
36 changes: 36 additions & 0 deletions src/Error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,42 @@ if (!$touch = Cloak::warning(touch(...), Cloak::SILENCE_ERROR)) {

## Available properties and methods

### Accessing the Error Reporting Level

Once instantiated, you can always access the Error reporting level via the `suppress*` methods
For instance if you need to know if the instance will suppress user deprecated error
you can do the following:

```php
$touch = Cloak::warning(touch(...));
if ($touch->suppressUserDeprecated()) {
//touch will record E_USER_DEPRECATED errors.
}
```

The following methods are available.

```php
<?php
use Bakame\Aide\Error\Cloak;

Cloak::suppressAll();
Cloak::suppressWarning();
Cloak::suppresNotice();
Cloak::suppressDeprecated();
Cloak::suppresStrict();
Cloak::suppresUserWarning();
Cloak::suppressUserNotice();
Cloak::suppressUserDeprecated();
```

### Accessing the error

To access the last error store in the instance you need to call the `Cloak::lastError` method.
If no error occurred during the last execution of the class the method will return `null`,
otherwise you will get an `ErrorException` class containing all the detail about the
last error.

### Controlling when to throw or not your errors.

The class general behaviour is controlled by two (2) static method:
Expand Down

0 comments on commit b120006

Please sign in to comment.