From 5ec5c7b93753b8f0e25311f20b0885c784dcc011 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 20 Mar 2024 16:08:44 +0100 Subject: [PATCH] Make implicit nullable types explicit --- .php-cs-fixer.dist.php | 1 + src/Coroutine.php | 4 ++-- src/Each.php | 10 +++++----- src/FulfilledPromise.php | 4 ++-- src/Promise.php | 8 ++++---- src/PromiseInterface.php | 4 ++-- src/RejectedPromise.php | 4 ++-- src/RejectionException.php | 2 +- src/Utils.php | 2 +- tests/CoroutineTest.php | 2 +- tests/NotPromiseInstance.php | 4 ++-- tests/Thennable.php | 2 +- 12 files changed, 24 insertions(+), 23 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index b267c7f..5f1ea7b 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -10,6 +10,7 @@ 'no_superfluous_phpdoc_tags' => [ 'allow_mixed' => true, ], + 'nullable_type_declaration_for_default_null_value' => true, 'phpdoc_annotation_without_dot' => false, 'phpdoc_summary' => false, 'phpdoc_to_comment' => false, diff --git a/src/Coroutine.php b/src/Coroutine.php index 0b5b9c0..0da0228 100644 --- a/src/Coroutine.php +++ b/src/Coroutine.php @@ -84,8 +84,8 @@ public static function of(callable $generatorFn): self } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { return $this->result->then($onFulfilled, $onRejected); } diff --git a/src/Each.php b/src/Each.php index c09d23c..dd72c83 100644 --- a/src/Each.php +++ b/src/Each.php @@ -23,8 +23,8 @@ final class Each */ public static function of( $iterable, - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { return (new EachPromise($iterable, [ 'fulfilled' => $onFulfilled, @@ -46,8 +46,8 @@ public static function of( public static function ofLimit( $iterable, $concurrency, - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { return (new EachPromise($iterable, [ 'fulfilled' => $onFulfilled, @@ -67,7 +67,7 @@ public static function ofLimit( public static function ofLimitAll( $iterable, $concurrency, - callable $onFulfilled = null + ?callable $onFulfilled = null ): PromiseInterface { return self::ofLimit( $iterable, diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index ab71296..727ec31 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -31,8 +31,8 @@ public function __construct($value) } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { // Return itself if there is no onFulfilled function. if (!$onFulfilled) { diff --git a/src/Promise.php b/src/Promise.php index 1b07bdc..c0c5be2 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -25,16 +25,16 @@ class Promise implements PromiseInterface * @param callable $cancelFn Fn that when invoked cancels the promise. */ public function __construct( - callable $waitFn = null, - callable $cancelFn = null + ?callable $waitFn = null, + ?callable $cancelFn = null ) { $this->waitFn = $waitFn; $this->cancelFn = $cancelFn; } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { if ($this->state === self::PENDING) { $p = new Promise(null, [$this, 'cancel']); diff --git a/src/PromiseInterface.php b/src/PromiseInterface.php index 2824802..c11721e 100644 --- a/src/PromiseInterface.php +++ b/src/PromiseInterface.php @@ -27,8 +27,8 @@ interface PromiseInterface * @param callable $onRejected Invoked when the promise is rejected. */ public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface; /** diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index d947da1..1ebf0b2 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -31,8 +31,8 @@ public function __construct($reason) } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { // If there's no onRejected callback then just return self. if (!$onRejected) { diff --git a/src/RejectionException.php b/src/RejectionException.php index 72a81ba..47dca86 100644 --- a/src/RejectionException.php +++ b/src/RejectionException.php @@ -18,7 +18,7 @@ class RejectionException extends \RuntimeException * @param mixed $reason Rejection reason. * @param string|null $description Optional description. */ - public function __construct($reason, string $description = null) + public function __construct($reason, ?string $description = null) { $this->reason = $reason; diff --git a/src/Utils.php b/src/Utils.php index e1570d7..45b0893 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -21,7 +21,7 @@ final class Utils * * @param TaskQueueInterface|null $assign Optionally specify a new queue instance. */ - public static function queue(TaskQueueInterface $assign = null): TaskQueueInterface + public static function queue(?TaskQueueInterface $assign = null): TaskQueueInterface { static $queue; diff --git a/tests/CoroutineTest.php b/tests/CoroutineTest.php index f046e15..046a45b 100644 --- a/tests/CoroutineTest.php +++ b/tests/CoroutineTest.php @@ -103,7 +103,7 @@ public function testWaitShouldHandleIntermediateErrors(): void }); }); }) - ->otherwise(function (\Exception $error = null) { + ->otherwise(function (?\Exception $error = null) { if (!$error) { self::fail('Error did not propagate.'); } diff --git a/tests/NotPromiseInstance.php b/tests/NotPromiseInstance.php index dc6c2be..e4c9d34 100644 --- a/tests/NotPromiseInstance.php +++ b/tests/NotPromiseInstance.php @@ -16,7 +16,7 @@ public function __construct() $this->nextPromise = new Promise(); } - public function then(callable $res = null, callable $rej = null): PromiseInterface + public function then(?callable $res = null, ?callable $rej = null): PromiseInterface { return $this->nextPromise->then($res, $rej); } @@ -36,7 +36,7 @@ public function reject($reason): void $this->nextPromise->reject($reason); } - public function wait(bool $unwrap = true, bool $defaultResolution = null): void + public function wait(bool $unwrap = true, ?bool $defaultResolution = null): void { } diff --git a/tests/Thennable.php b/tests/Thennable.php index 2484148..fb1e496 100644 --- a/tests/Thennable.php +++ b/tests/Thennable.php @@ -15,7 +15,7 @@ public function __construct() $this->nextPromise = new Promise(); } - public function then(callable $res = null, callable $rej = null) + public function then(?callable $res = null, ?callable $rej = null) { return $this->nextPromise->then($res, $rej); }