diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..5c98b42
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,2 @@
+# Default ignored files
+/workspace.xml
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..28a804d
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..2b3828e
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml
new file mode 100644
index 0000000..40c9ece
--- /dev/null
+++ b/.idea/php-test-framework.xml
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/php.xml b/.idea/php.xml
new file mode 100644
index 0000000..7eee1da
--- /dev/null
+++ b/.idea/php.xml
@@ -0,0 +1,143 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ /usr/local/etc/php/conf.d/docker-php-ext-intl.ini, /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini, /usr/local/etc/php/conf.d/docker-php-ext-pcntl.ini, /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini, /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini, /usr/local/etc/php/conf.d/memory.ini, /usr/local/etc/php/conf.d/security.ini, /usr/local/etc/php/conf.d/xdebug.ini
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/promise.iml b/.idea/promise.iml
new file mode 100644
index 0000000..eccf802
--- /dev/null
+++ b/.idea/promise.iml
@@ -0,0 +1,68 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/bin/php-parse b/bin/php-parse
new file mode 120000
index 0000000..726f634
--- /dev/null
+++ b/bin/php-parse
@@ -0,0 +1 @@
+../vendor/nikic/php-parser/bin/php-parse
\ No newline at end of file
diff --git a/bin/phpspec b/bin/phpspec
new file mode 120000
index 0000000..22e0618
--- /dev/null
+++ b/bin/phpspec
@@ -0,0 +1 @@
+../vendor/phpspec/phpspec/bin/phpspec
\ No newline at end of file
diff --git a/bin/phpunit b/bin/phpunit
new file mode 120000
index 0000000..4ba3256
--- /dev/null
+++ b/bin/phpunit
@@ -0,0 +1 @@
+../vendor/phpunit/phpunit/phpunit
\ No newline at end of file
diff --git a/bin/yaml-lint b/bin/yaml-lint
new file mode 120000
index 0000000..73c0353
--- /dev/null
+++ b/bin/yaml-lint
@@ -0,0 +1 @@
+../vendor/symfony/yaml/Resources/bin/yaml-lint
\ No newline at end of file
diff --git a/composer.lock b/composer.lock
index dcaa09a..c84cc75 100644
--- a/composer.lock
+++ b/composer.lock
@@ -12,12 +12,12 @@
"source": {
"type": "git",
"url": "https://github.com/php-etl/promise-contracts.git",
- "reference": "810165f8ff78d788e4aed36d02e375e06ab86dcb"
+ "reference": "2e04532bcb95896031bc51f18aa8a803df9ee6c5"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/php-etl/promise-contracts/zipball/810165f8ff78d788e4aed36d02e375e06ab86dcb",
- "reference": "810165f8ff78d788e4aed36d02e375e06ab86dcb",
+ "url": "https://api.github.com/repos/php-etl/promise-contracts/zipball/2e04532bcb95896031bc51f18aa8a803df9ee6c5",
+ "reference": "2e04532bcb95896031bc51f18aa8a803df9ee6c5",
"shasum": ""
},
"require": {
@@ -54,7 +54,7 @@
"issues": "https://github.com/php-etl/promise-contracts/issues",
"source": "https://github.com/php-etl/promise-contracts/tree/master"
},
- "time": "2021-06-26T09:04:59+00:00"
+ "time": "2021-06-28T20:30:41+00:00"
}
],
"packages-dev": [
diff --git a/src/Deferred.php b/src/Deferred.php
index 3db3525..ebb8735 100644
--- a/src/Deferred.php
+++ b/src/Deferred.php
@@ -6,20 +6,21 @@
/**
* @api
- * @template Type
- * @implements Contract\DeferredInterface
+ * @template ExpectationType
+ * @template ExceptionType of \Throwable
+ * @implements Contract\DeferredInterface
*/
final class Deferred implements Contract\DeferredInterface
{
- /** @param Contract\PromiseInterface $promise */
+ /** @param Contract\PromiseInterface $promise */
public function __construct(private Contract\PromiseInterface $promise)
{
}
/**
- * @param callable(Type): Type $callback
+ * @param callable(ExpectationType): void $callback
*
- * @return Contract\DeferredInterface
+ * @return Contract\DeferredInterface
*/
public function then(callable $callback): Contract\DeferredInterface
{
@@ -28,9 +29,9 @@ public function then(callable $callback): Contract\DeferredInterface
}
/**
- * @param callable(\Throwable):\Throwable $callback
+ * @param callable(ExceptionType): void $callback
*
- * @return Contract\DeferredInterface
+ * @return Contract\DeferredInterface
*/
public function failure(callable $callback): Contract\DeferredInterface
{
diff --git a/src/FailedPromise.php b/src/FailedPromise.php
index ed462df..78823c8 100644
--- a/src/FailedPromise.php
+++ b/src/FailedPromise.php
@@ -6,22 +6,25 @@
/**
* @api
- * @template Type
- * @implements Contract\PromiseInterface
+ * @template ExpectationType
+ * @template ExceptionType of \Throwable
+ * @implements Contract\PromiseInterface
*/
final class FailedPromise implements Contract\PromiseInterface
{
+ /** @var Contract\Resolution\FailureInterface */
private Contract\Resolution\FailureInterface $resolution;
+ /** @param ExceptionType $exception */
public function __construct(\Throwable $exception)
{
$this->resolution = new Resolution\Failure($exception);
}
/**
- * @param callable(Type): Type $callback
+ * @param callable(ExpectationType): void $callback
*
- * @return Contract\PromiseInterface
+ * @return Contract\PromiseInterface
*/
public function then(callable $callback): Contract\PromiseInterface
{
@@ -29,9 +32,9 @@ public function then(callable $callback): Contract\PromiseInterface
}
/**
- * @param callable(\Throwable): \Throwable $callback
+ * @param callable(\Throwable): void $callback
*
- * @return Contract\PromiseInterface
+ * @return Contract\PromiseInterface
*/
public function failure(callable $callback): Contract\PromiseInterface
{
@@ -39,10 +42,12 @@ public function failure(callable $callback): Contract\PromiseInterface
return $this;
}
- /** @return Contract\DeferredInterface */
+ /** @return Contract\DeferredInterface */
public function defer(): Contract\DeferredInterface
{
- return new Deferred($this);
+ /** @var Deferred $deferred */
+ $deferred = new Deferred($this);
+ return $deferred;
}
public function isResolved(): bool
@@ -60,6 +65,7 @@ public function isFailure(): bool
return true;
}
+ /** @return Contract\Resolution\ResolutionInterface|Contract\Resolution\ResolvedInterface */
public function resolution(): Contract\Resolution\ResolutionInterface
{
return $this->resolution;
diff --git a/src/Promise.php b/src/Promise.php
index fab9e62..beac275 100644
--- a/src/Promise.php
+++ b/src/Promise.php
@@ -6,15 +6,17 @@
/**
* @api
- * @template Type
- * @implements Contract\ResolvablePromiseInterface
+ * @template ExpectationType
+ * @template ExceptionType of \Throwable
+ * @implements Contract\ResolvablePromiseInterface
*/
final class Promise implements Contract\ResolvablePromiseInterface
{
- /** @var array */
+ /** @var array */
private $successCallbacks;
- /** @var array */
+ /** @var array */
private $failureCallbacks;
+ /** @var Resolution\Pending|Resolution\Success|Resolution\Failure */
private Contract\Resolution\ResolutionInterface $resolution;
public function __construct()
@@ -25,46 +27,51 @@ public function __construct()
}
/**
- * @param callable(Type): Type $callback
+ * @param callable(ExpectationType): void $callback
*
- * @return Contract\PromiseInterface
+ * @return Contract\PromiseInterface
*/
public function then(callable $callback): Contract\PromiseInterface
{
if ($this->resolution instanceof Contract\Resolution\SuccessInterface) {
$this->resolution->apply($callback);
}
-
- $this->successCallbacks[] = $callback;
+ if (!$this->resolution instanceof Contract\Resolution\ResolvedInterface) {
+ $this->successCallbacks[] = $callback;
+ }
return $this;
}
/**
- * @param callable(\Throwable): \Throwable $callback
+ * @param callable(ExceptionType): void $callback
*
- * @return Contract\PromiseInterface
+ * @return Contract\PromiseInterface
*/
public function failure(callable $callback): Contract\PromiseInterface
{
if ($this->resolution instanceof Contract\Resolution\FailureInterface) {
$this->resolution->apply($callback);
}
-
- $this->failureCallbacks[] = $callback;
+ if (!$this->resolution instanceof Contract\Resolution\ResolvedInterface) {
+ $this->failureCallbacks[] = $callback;
+ }
return $this;
}
- /** @return Contract\DeferredInterface */
+ /** @return Contract\DeferredInterface */
public function defer(): Contract\DeferredInterface
{
- return new Deferred($this);
+ /** @var Deferred $deferred */
+ $deferred = new Deferred($this);
+ return $deferred;
}
+ /** @param ExpectationType $value */
public function resolve($value): void
{
- if (!$this->resolution instanceof Resolution\Pending) {
+ if ($this->resolution instanceof Contract\Resolution\ResolvedInterface) {
throw new AlreadyResolvedPromise('The promise was already resolved, cannot resolve again.');
}
@@ -78,9 +85,10 @@ public function resolve($value): void
}
}
+ /** @param ExceptionType $failure */
public function fail(\Throwable $failure): void
{
- if (!$this->resolution instanceof Resolution\Pending) {
+ if ($this->resolution instanceof Contract\Resolution\ResolvedInterface) {
throw new AlreadyResolvedPromise('The promise was already resolved, cannot resolve again.');
}
@@ -109,6 +117,7 @@ public function isFailure(): bool
return $this->resolution instanceof Contract\Resolution\FailureInterface;
}
+ /** @return Resolution\Pending|Resolution\Success|Resolution\Failure */
public function resolution(): Contract\Resolution\ResolutionInterface
{
return $this->resolution;
diff --git a/src/Resolution/Failure.php b/src/Resolution/Failure.php
index 0061a5f..01082d9 100644
--- a/src/Resolution/Failure.php
+++ b/src/Resolution/Failure.php
@@ -2,26 +2,29 @@
namespace Kiboko\Component\Promise\Resolution;
-use Kiboko\Contract\Promise\Resolution\FailureInterface;
+use Kiboko\Contract\Promise as Contract;
/**
* @internal
+ * @template Type of \Throwable
+ * @implements Contract\Resolution\FailureInterface
*/
-final class Failure implements FailureInterface
+final class Failure implements Contract\Resolution\FailureInterface
{
+ /** @param Type $error */
public function __construct(private \Throwable $error)
{
}
+ /** @return Type */
public function error(): \Throwable
{
return $this->error;
}
+ /** @var callable(Type): void */
public function apply(callable $callback): void
{
- if (($error = $callback($this->error)) !== null) {
- $this->error = $error;
- }
+ $callback($this->error);
}
}
diff --git a/src/Resolution/Success.php b/src/Resolution/Success.php
index 258b769..119c432 100644
--- a/src/Resolution/Success.php
+++ b/src/Resolution/Success.php
@@ -2,14 +2,14 @@
namespace Kiboko\Component\Promise\Resolution;
-use Kiboko\Contract\Promise\Resolution\SuccessInterface;
+use Kiboko\Contract\Promise as Contract;
/**
* @internal
* @template Type
- * @implements SuccessInterface
+ * @implements Contract\Resolution\SuccessInterface
*/
-final class Success implements SuccessInterface
+final class Success implements Contract\Resolution\SuccessInterface
{
/** @param Type $value */
public function __construct(private $value)
@@ -22,11 +22,9 @@ public function value()
return $this->value;
}
- /** @var callable(Type) */
+ /** @var callable(Type): void */
public function apply(callable $callback): void
{
- if (($value = $callback($this->value)) !== null) {
- $this->value = $value;
- }
+ $callback($this->value);
}
}
diff --git a/src/SucceededPromise.php b/src/SucceededPromise.php
index 372579b..0fbafb5 100644
--- a/src/SucceededPromise.php
+++ b/src/SucceededPromise.php
@@ -6,26 +6,25 @@
/**
* @api
- * @template Type
- * @implements Contract\PromiseInterface
+ * @template ExpectationType
+ * @template ExceptionType of \Throwable
+ * @implements Contract\PromiseInterface
*/
final class SucceededPromise implements Contract\PromiseInterface
{
- /** @var Contract\Resolution\SuccessInterface */
+ /** @var Contract\Resolution\SuccessInterface */
private Contract\Resolution\SuccessInterface $resolution;
- /**
- * @param Type $value
- */
+ /** @param ExpectationType $value */
public function __construct($value)
{
$this->resolution = new Resolution\Success($value);
}
/**
- * @param callable(Type): Type $callback
+ * @param callable(ExpectationType): void $callback
*
- * @return Contract\PromiseInterface
+ * @return Contract\PromiseInterface
*/
public function then(callable $callback): Contract\PromiseInterface
{
@@ -34,19 +33,21 @@ public function then(callable $callback): Contract\PromiseInterface
}
/**
- * @param callable(\Throwable): \Throwable $callback
+ * @param callable(ExceptionType): void $callback
*
- * @return Contract\PromiseInterface
+ * @return Contract\PromiseInterface
*/
public function failure(callable $callback): Contract\PromiseInterface
{
return $this;
}
- /** @return Contract\DeferredInterface */
+ /** @return Contract\DeferredInterface */
public function defer(): Contract\DeferredInterface
{
- return new Deferred($this);
+ /** @var Deferred $deferred */
+ $deferred = new Deferred($this);
+ return $deferred;
}
public function isResolved(): bool
@@ -64,6 +65,7 @@ public function isFailure(): bool
return false;
}
+ /** @return Contract\Resolution\SuccessInterface */
public function resolution(): Contract\Resolution\ResolutionInterface
{
return $this->resolution;