Skip to content

Commit

Permalink
Added PHPStan annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
gplanchat committed Jun 26, 2021
1 parent 6a7bdb5 commit 0a788de
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
22 changes: 12 additions & 10 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@

namespace Kiboko\Component\Promise;

use Kiboko\Contract\Promise\DeferredInterface;
use Kiboko\Contract\Promise\PromiseInterface;
use Kiboko\Contract\Promise as Contract;

/**
* @api
* @template Type
* @implements Contract\DeferredInterface<Type>
*/
final class Deferred implements DeferredInterface
final class Deferred implements Contract\DeferredInterface
{
public function __construct(private PromiseInterface $promise)
/** @param Contract\PromiseInterface<Type> $promise */
public function __construct(private Contract\PromiseInterface $promise)
{
}

/**
* @param callable<mixed> $callback
* @param callable(Type): Type $callback
*
* @return DeferredInterface
* @return Contract\DeferredInterface<Type>
*/
public function then(callable $callback): DeferredInterface
public function then(callable $callback): Contract\DeferredInterface
{
$this->promise->then($callback);
return $this;
}

/**
* @param callable<\Throwable> $callback
* @param callable(\Throwable):\Throwable $callback
*
* @return DeferredInterface
* @return Contract\DeferredInterface<Type>
*/
public function failure(callable $callback): DeferredInterface
public function failure(callable $callback): Contract\DeferredInterface
{
$this->promise->failure($callback);
return $this;
Expand Down
13 changes: 13 additions & 0 deletions src/FailedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/**
* @api
* @template Type
* @implements Contract\PromiseInterface<Type>
*/
final class FailedPromise implements Contract\PromiseInterface
{
Expand All @@ -16,17 +18,28 @@ public function __construct(\Throwable $exception)
$this->resolution = new Resolution\Failure($exception);
}

/**
* @param callable(Type): Type $callback
*
* @return Contract\PromiseInterface<Type>
*/
public function then(callable $callback): Contract\PromiseInterface
{
return $this;
}

/**
* @param callable(\Throwable): \Throwable $callback
*
* @return Contract\PromiseInterface<Type>
*/
public function failure(callable $callback): Contract\PromiseInterface
{
$this->resolution->apply($callback);
return $this;
}

/** @return Contract\DeferredInterface<Type> */
public function defer(): Contract\DeferredInterface
{
return new Deferred($this);
Expand Down
27 changes: 20 additions & 7 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

/**
* @api
* @template Type
* @implements Contract\ResolvablePromiseInterface<Type>
*/
final class Promise implements Contract\ResolvablePromiseInterface
{
/** @var callable */
/** @var array<callable(Type): Type> */
private $successCallbacks;
/** @var callable */
/** @var array<callable(\Throwable): \Throwable> */
private $failureCallbacks;
private Contract\Resolution\ResolutionInterface $resolution;

Expand All @@ -22,11 +24,11 @@ public function __construct()
$this->resolution = new Resolution\Pending();
}

public function defer(): Contract\DeferredInterface
{
return new Deferred($this);
}

/**
* @param callable(Type): Type $callback
*
* @return Contract\PromiseInterface<Type>
*/
public function then(callable $callback): Contract\PromiseInterface
{
if ($this->resolution instanceof Contract\Resolution\SuccessInterface) {
Expand All @@ -38,6 +40,11 @@ public function then(callable $callback): Contract\PromiseInterface
return $this;
}

/**
* @param callable(\Throwable): \Throwable $callback
*
* @return Contract\PromiseInterface<Type>
*/
public function failure(callable $callback): Contract\PromiseInterface
{
if ($this->resolution instanceof Contract\Resolution\FailureInterface) {
Expand All @@ -49,6 +56,12 @@ public function failure(callable $callback): Contract\PromiseInterface
return $this;
}

/** @return Contract\DeferredInterface<Type> */
public function defer(): Contract\DeferredInterface
{
return new Deferred($this);
}

public function resolve($value): void
{
if (!$this->resolution instanceof Resolution\Pending) {
Expand Down
5 changes: 5 additions & 0 deletions src/Resolution/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@

/**
* @internal
* @template Type
* @implements SuccessInterface<Type>
*/
final class Success implements SuccessInterface
{
/** @param Type $value */
public function __construct(private $value)
{
}

/** @return Type */
public function value()
{
return $this->value;
}

/** @var callable(Type) */
public function apply(callable $callback): void
{
if (($value = $callback($this->value)) !== null) {
Expand Down
17 changes: 17 additions & 0 deletions src/SucceededPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,44 @@

/**
* @api
* @template Type
* @implements Contract\PromiseInterface<Type>
*/
final class SucceededPromise implements Contract\PromiseInterface
{
/** @var Contract\Resolution\SuccessInterface<Type> */
private Contract\Resolution\SuccessInterface $resolution;

/**
* @param Type $value
*/
public function __construct($value)
{
$this->resolution = new Resolution\Success($value);
}

/**
* @param callable(Type): Type $callback
*
* @return Contract\PromiseInterface<Type>
*/
public function then(callable $callback): Contract\PromiseInterface
{
$this->resolution->apply($callback);
return $this;
}

/**
* @param callable(\Throwable): \Throwable $callback
*
* @return Contract\PromiseInterface<Type>
*/
public function failure(callable $callback): Contract\PromiseInterface
{
return $this;
}

/** @return Contract\DeferredInterface<Type> */
public function defer(): Contract\DeferredInterface
{
return new Deferred($this);
Expand Down

0 comments on commit 0a788de

Please sign in to comment.