Skip to content

Commit

Permalink
Remove unused result exception
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaudo committed Mar 30, 2023
1 parent 242d397 commit b5de6a5
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 279 deletions.
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,3 @@ return match (true) {
default => throw new MyException(),
}
```

Results must be used
--------------------

One of the great features of Rust results is that its compiler requires you to
use each returned result.

In PHP there is no compile step, but it is possible to throw an exception from
the `__destruct()` method if none of the methods of the result have been called.

It's a bit risky, since the PHP documentation says that throwing an exception
from a destructor could cause a fatal error (if called at script termination
time).

However, forgetting to handle a result is also risky, and I haven't found a
better way to warn developers of unused results.
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ parameters:
paths:
- src
- tests
excludePaths:
analyse:
- src/Backtrace.php
checkGenericClassInNonGenericObjectType: false
66 changes: 0 additions & 66 deletions src/Backtrace.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Hereldar\Results;

use Closure;
use Hereldar\Results\Exceptions\UnusedResult;
use Hereldar\Results\Interfaces\IResult;
use RuntimeException;
use Throwable;
Expand All @@ -15,23 +14,12 @@
*/
final class Error implements IResult
{
private bool $used = false;
private readonly Backtrace $trace;

/**
* @param E $exception
*/
private function __construct(
private readonly Throwable $exception,
) {
$this->trace = new Backtrace($this::class);
}

public function __destruct()
{
if (!$this->used) {
throw new UnusedResult($this, (string) $this->trace);
}
}

/**
Expand Down Expand Up @@ -74,8 +62,6 @@ public static function withMessage(string $message): self
*/
public function andThen(Ok|Error|Closure $result): static
{
$this->used = true;

return $this;
}

Expand All @@ -84,8 +70,6 @@ public function andThen(Ok|Error|Closure $result): static
*/
public function exception(): Throwable
{
$this->used = true;

return $this->exception;
}

Expand All @@ -94,15 +78,11 @@ public function exception(): Throwable
*/
public function hasException(): bool
{
$this->used = true;

return true;
}

public function hasMessage(): bool
{
$this->used = true;

return ($this->exception->getMessage() !== '');
}

Expand All @@ -111,8 +91,6 @@ public function hasMessage(): bool
*/
public function hasValue(): bool
{
$this->used = true;

return false;
}

Expand All @@ -121,8 +99,6 @@ public function hasValue(): bool
*/
public function isError(): bool
{
$this->used = true;

return true;
}

Expand All @@ -131,15 +107,11 @@ public function isError(): bool
*/
public function isOk(): bool
{
$this->used = true;

return false;
}

public function message(): string
{
$this->used = true;

return $this->exception->getMessage();
}

Expand All @@ -150,8 +122,6 @@ public function message(): string
*/
public function onFailure(Closure $action): static
{
$this->used = true;

$action($this->exception);

return $this;
Expand All @@ -164,8 +134,6 @@ public function onFailure(Closure $action): static
*/
public function onSuccess(Closure $action): static
{
$this->used = true;

return $this;
}

Expand All @@ -181,8 +149,6 @@ public function onSuccess(Closure $action): static
*/
public function or(mixed $value): mixed
{
$this->used = true;

if ($value instanceof Closure) {
return $value();
}
Expand All @@ -192,8 +158,6 @@ public function or(mixed $value): mixed

public function orDie(int|string $status = null): never
{
$this->used = true;

if (isset($status)) {
exit($status);
}
Expand All @@ -219,8 +183,6 @@ public function orDie(int|string $status = null): never
*/
public function orElse(Ok|Error|Closure $result): Ok|Error
{
$this->used = true;

if ($result instanceof Closure) {
return $result();
}
Expand All @@ -235,8 +197,6 @@ public function orElse(Ok|Error|Closure $result): Ok|Error
*/
public function orFail(): never
{
$this->used = true;

throw $this->exception;
}

Expand All @@ -245,8 +205,6 @@ public function orFail(): never
*/
public function orFalse(): bool
{
$this->used = true;

return false;
}

Expand All @@ -257,8 +215,6 @@ public function orFalse(): bool
*/
public function orNull(): mixed
{
$this->used = true;

return null;
}

Expand All @@ -274,8 +230,6 @@ public function orNull(): mixed
*/
public function orThrow(Throwable|Closure $exception): never
{
$this->used = true;

if ($exception instanceof Closure) {
throw $exception($this->exception);
}
Expand All @@ -290,8 +244,6 @@ public function orThrow(Throwable|Closure $exception): never
*/
public function value(): mixed
{
$this->used = true;

return null;
}
}
25 changes: 0 additions & 25 deletions src/Exceptions/UnusedResult.php

This file was deleted.

0 comments on commit b5de6a5

Please sign in to comment.