Skip to content

Commit

Permalink
Added error support(this also fixes generic data check)
Browse files Browse the repository at this point in the history
  • Loading branch information
TemirkhanN committed Oct 15, 2022
1 parent 0798429 commit bb27027
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
43 changes: 43 additions & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace TemirkhanN\Generic;

class Error
{
private string $message;
private int $code;
/** @var array<mixed> */
private array $details;

/**
* @param string $message
* @param int $code
* @param array<mixed> $details
*/
public function __construct(string $message, int $code = 0, array $details = [])
{
$this->message = $message;
$this->code = $code;
$this->details = $details;
}

public function getMessage(): string
{
return $this->message;
}

public function getCode(): int
{
return $this->code;
}

/**
* @return array<mixed>
*/
public function getDetails(): array
{
return $this->details;
}
}
2 changes: 1 addition & 1 deletion src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* This error shall lead to direct fix in the code
*/
class RuntimeException extends Exception
class RuntimeException extends \RuntimeException
{

}
42 changes: 25 additions & 17 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

namespace TemirkhanN\Generic;

use RuntimeException;

/**
* @template T
* @implements ResultInterface<T>
* @template E
*
* @implements ResultInterface<T, E>
*/
final class Result implements ResultInterface
{
private string $error = '';
/**
* @var ?E
*/
private $error = null;

/**
* @var T
Expand All @@ -24,9 +31,11 @@ private function __construct()
/**
* phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
*
* @param T $data
* @template DT
*
* @return static<T>
* @param DT $data
*
* @return static<DT, E>
*/
public static function success($data = null): self
{
Expand All @@ -38,16 +47,14 @@ public static function success($data = null): self
}

/**
* @param string $error
* @template Err
*
* @param Err $error
*
* @return static<T>
* @return static<T, Err>
*/
public static function error(string $error): self
public static function error($error): self
{
if ($error === '') {
throw new Exception\RuntimeException('Error shall contain some valid(non-empty) message');
}

$result = new self();
$result->error = $error;

Expand All @@ -56,21 +63,22 @@ public static function error(string $error): self

public function isSuccessful(): bool
{
return $this->error === '';
return $this->error === null;
}

public function getError(): string
public function getError()
{
if ($this->error === null) {
throw new RuntimeException('This is not an error result. Consider using isSuccessful');
}

return $this->error;
}

/**
* @return T
*/
public function getData()
{
if(!$this->isSuccessful()) {
throw new \RuntimeException('This is an error result. It contains no data.');
throw new RuntimeException('This is an error result. Consider using isSuccessful.');
}

return $this->data;
Expand Down
6 changes: 5 additions & 1 deletion src/ResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

/**
* @template T
* @template E
*/
interface ResultInterface
{
public function isSuccessful(): bool;

public function getError(): string;
/**
* @return E
*/
public function getError();

/**
* @return T
Expand Down
2 changes: 1 addition & 1 deletion tests/Phpstan/WishMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class WishMaker
{
/**
* @return ResultInterface<WishPromise>
* @return ResultInterface<WishPromise, string>
*/
public function pleaseAddNativeGenerics(): ResultInterface
{
Expand Down
1 change: 0 additions & 1 deletion tests/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function testSuccess(): void
$result = Result::success(['Some data']);

self::assertTrue($result->isSuccessful());
self::assertEmpty($result->getError());
self::assertEquals(['Some data'], $result->getData());
}
}

0 comments on commit bb27027

Please sign in to comment.