Skip to content

Commit

Permalink
User covariant templates
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaudo committed Jun 17, 2023
1 parent 515877c commit 81c68bd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.1"
"php": "^8.1.17|^8.2.4"
},
"require-dev": {
"fakerphp/faker": "^1.21",
Expand Down
9 changes: 6 additions & 3 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Hereldar\Results;

use Closure;
use Hereldar\Results\Interfaces\IResult;
use Hereldar\Results\Interfaces\Resultlike;
use RuntimeException;
use Throwable;

/**
* @template E of Throwable
* @template-covariant E of Throwable
*/
final class Error implements IResult
final class Error implements Resultlike
{
/**
* @param E $exception
Expand Down Expand Up @@ -119,6 +119,8 @@ public function message(): string
* @param Closure(E):void $action
*
* @return $this
*
* @psalm-suppress InvalidTemplateParam
*/
public function onFailure(Closure $action): static
{
Expand Down Expand Up @@ -227,6 +229,7 @@ public function orNull(): mixed
*
* @psalm-suppress MoreSpecificImplementedParamType
* @psalm-suppress UndefinedDocblockClass
* @psalm-suppress InvalidTemplateParam
*/
public function orThrow(Throwable|Closure $exception): never
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @internal
*/
interface IResult
interface Resultlike
{
/**
* Returns the given `result` if this instance is a success.
Expand Down
10 changes: 6 additions & 4 deletions src/Ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace Hereldar\Results;

use Closure;
use Hereldar\Results\Interfaces\IResult;
use Hereldar\Results\Interfaces\Resultlike;
use Throwable;

/**
* @template T
* @template-covariant T
*/
final class Ok implements IResult
final class Ok implements Resultlike
{
/** @var self<null>|null */
private static ?Ok $empty = null;
Expand Down Expand Up @@ -135,6 +135,8 @@ public function onFailure(Closure $action): static
* @param Closure(T):void $action
*
* @return $this
*
* @psalm-suppress InvalidTemplateParam
*/
public function onSuccess(Closure $action): static
{
Expand All @@ -146,7 +148,7 @@ public function onSuccess(Closure $action): static
/**
* @template U
*
* @param U|Closure(T):U $value
* @param U|Closure():U $value
*
* @return T
*/
Expand Down

0 comments on commit 81c68bd

Please sign in to comment.