From 89fab3503a920a3142274c78310b0fa56b57dc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 21 Feb 2022 09:10:57 +0100 Subject: [PATCH] Require argument for `resolve()` function --- README.md | 4 ++-- src/Deferred.php | 2 +- src/Promise.php | 2 +- src/functions.php | 5 ++--- tests/FunctionAnyTest.php | 2 +- tests/FunctionRaceTest.php | 2 +- tests/FunctionSomeTest.php | 2 +- tests/PromiseTest/CancelTestTrait.php | 2 +- tests/PromiseTest/PromiseFulfilledTestTrait.php | 4 ++-- tests/PromiseTest/PromisePendingTestTrait.php | 4 ++-- tests/PromiseTest/PromiseRejectedTestTrait.php | 4 ++-- tests/PromiseTest/PromiseSettledTestTrait.php | 16 ++++++++-------- tests/PromiseTest/RejectTestTrait.php | 4 ++-- 13 files changed, 26 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index c2e1dfb7..b64e1a31 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ $deferred = new React\Promise\Deferred(); $promise = $deferred->promise(); -$deferred->resolve(mixed $value = null); +$deferred->resolve(mixed $value); $deferred->reject(\Throwable $reason); ``` @@ -119,7 +119,7 @@ keeping the authority to modify its state to yourself. #### Deferred::resolve() ```php -$deferred->resolve(mixed $value = null); +$deferred->resolve(mixed $value); ``` Resolves the promise returned by `promise()`. All consumers are notified by diff --git a/src/Deferred.php b/src/Deferred.php index 563d18f9..ee4fdfa9 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -21,7 +21,7 @@ public function promise(): PromiseInterface return $this->promise; } - public function resolve($value = null): void + public function resolve($value): void { ($this->resolveCallback)($value); } diff --git a/src/Promise.php b/src/Promise.php index b5633e3d..865249de 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -239,7 +239,7 @@ private function call(callable $cb): void $target =& $this; $callback( - static function ($value = null) use (&$target) { + static function ($value) use (&$target) { if ($target !== null) { $target->settle(resolve($value)); $target = null; diff --git a/src/functions.php b/src/functions.php index 96106016..38b036a9 100644 --- a/src/functions.php +++ b/src/functions.php @@ -20,8 +20,7 @@ * @param mixed $promiseOrValue * @return PromiseInterface */ - -function resolve($promiseOrValue = null): PromiseInterface +function resolve($promiseOrValue): PromiseInterface { if ($promiseOrValue instanceof PromiseInterface) { return $promiseOrValue; @@ -346,7 +345,7 @@ function _checkTypehint(callable $callback, \Throwable $reason): bool // Extract the type of the argument and handle different possibilities $type = $expectedException->getType(); - + $isTypeUnion = true; $types = []; diff --git a/tests/FunctionAnyTest.php b/tests/FunctionAnyTest.php index 38ffb28e..71c8aba5 100644 --- a/tests/FunctionAnyTest.php +++ b/tests/FunctionAnyTest.php @@ -122,7 +122,7 @@ public function shouldCancelInputArrayPromises() public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() { $deferred = new Deferred($this->expectCallableNever()); - $deferred->resolve(); + $deferred->resolve(null); $promise2 = new Promise(function () {}, $this->expectCallableNever()); diff --git a/tests/FunctionRaceTest.php b/tests/FunctionRaceTest.php index 83649173..40200256 100644 --- a/tests/FunctionRaceTest.php +++ b/tests/FunctionRaceTest.php @@ -103,7 +103,7 @@ public function shouldCancelInputArrayPromises() public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() { $deferred = new Deferred($this->expectCallableNever()); - $deferred->resolve(); + $deferred->resolve(null); $promise2 = new Promise(function () {}, $this->expectCallableNever()); diff --git a/tests/FunctionSomeTest.php b/tests/FunctionSomeTest.php index 9af1c1e0..f5c55df9 100644 --- a/tests/FunctionSomeTest.php +++ b/tests/FunctionSomeTest.php @@ -148,7 +148,7 @@ public function shouldCancelInputArrayPromises() public function shouldCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfill() { $deferred = new Deferred($this->expectCallableNever()); - $deferred->resolve(); + $deferred->resolve(null); $promise2 = new Promise(function () {}, $this->expectCallableNever()); diff --git a/tests/PromiseTest/CancelTestTrait.php b/tests/PromiseTest/CancelTestTrait.php index d6626655..00c1931e 100644 --- a/tests/PromiseTest/CancelTestTrait.php +++ b/tests/PromiseTest/CancelTestTrait.php @@ -110,7 +110,7 @@ public function cancelShouldCallCancellerOnlyOnceIfCancellerResolves() ->expects($this->once()) ->method('__invoke') ->will($this->returnCallback(function ($resolve) { - $resolve(); + $resolve(null); })); $adapter = $this->getPromiseTestAdapter($mock); diff --git a/tests/PromiseTest/PromiseFulfilledTestTrait.php b/tests/PromiseTest/PromiseFulfilledTestTrait.php index d58ab58a..bcbe5c02 100644 --- a/tests/PromiseTest/PromiseFulfilledTestTrait.php +++ b/tests/PromiseTest/PromiseFulfilledTestTrait.php @@ -190,7 +190,7 @@ public function cancelShouldReturnNullForFulfilledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->resolve(); + $adapter->resolve(null); self::assertNull($adapter->promise()->cancel()); } @@ -200,7 +200,7 @@ public function cancelShouldHaveNoEffectForFulfilledPromise() { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); - $adapter->resolve(); + $adapter->resolve(null); $adapter->promise()->cancel(); } diff --git a/tests/PromiseTest/PromisePendingTestTrait.php b/tests/PromiseTest/PromisePendingTestTrait.php index e4750197..11f0e993 100644 --- a/tests/PromiseTest/PromisePendingTestTrait.php +++ b/tests/PromiseTest/PromisePendingTestTrait.php @@ -57,7 +57,7 @@ public function catchShouldNotInvokeRejectionHandlerForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); $adapter->promise()->catch($this->expectCallableNever()); } @@ -77,7 +77,7 @@ public function otherwiseShouldNotInvokeRejectionHandlerForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); $adapter->promise()->otherwise($this->expectCallableNever()); } diff --git a/tests/PromiseTest/PromiseRejectedTestTrait.php b/tests/PromiseTest/PromiseRejectedTestTrait.php index 84922f64..cd85c2b1 100644 --- a/tests/PromiseTest/PromiseRejectedTestTrait.php +++ b/tests/PromiseTest/PromiseRejectedTestTrait.php @@ -279,11 +279,11 @@ public function doneShouldTriggerFatalErrorWithDeepNestingPromiseChainsForReject $exception = new Exception('UnhandledRejectionException'); $d = new Deferred(); - $d->resolve(); + $d->resolve(null); $result = resolve(resolve($d->promise()->then(function () use ($exception) { $d = new Deferred(); - $d->resolve(); + $d->resolve(null); return resolve($d->promise()->then(function () {}))->then( function () use ($exception) { diff --git a/tests/PromiseTest/PromiseSettledTestTrait.php b/tests/PromiseTest/PromiseSettledTestTrait.php index bf2b6f52..87879e91 100644 --- a/tests/PromiseTest/PromiseSettledTestTrait.php +++ b/tests/PromiseTest/PromiseSettledTestTrait.php @@ -17,7 +17,7 @@ public function thenShouldReturnAPromiseForSettledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->then()); } @@ -26,7 +26,7 @@ public function thenShouldReturnAllowNullForSettledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->then(null, null)); } @@ -35,7 +35,7 @@ public function cancelShouldReturnNullForSettledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); self::assertNull($adapter->promise()->cancel()); } @@ -45,7 +45,7 @@ public function cancelShouldHaveNoEffectForSettledPromise() { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); - $adapter->settle(); + $adapter->settle(null); $adapter->promise()->cancel(); } @@ -55,7 +55,7 @@ public function doneShouldReturnNullForSettledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); self::assertNull($adapter->promise()->done(null, function () {})); } @@ -64,7 +64,7 @@ public function doneShouldReturnAllowNullForSettledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); self::assertNull($adapter->promise()->done(null, function () {}, null)); } @@ -73,7 +73,7 @@ public function finallyShouldReturnAPromiseForSettledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->finally(function () {})); } @@ -85,7 +85,7 @@ public function alwaysShouldReturnAPromiseForSettledPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->settle(); + $adapter->settle(null); self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->always(function () {})); } } diff --git a/tests/PromiseTest/RejectTestTrait.php b/tests/PromiseTest/RejectTestTrait.php index 8b4017aa..c6015812 100644 --- a/tests/PromiseTest/RejectTestTrait.php +++ b/tests/PromiseTest/RejectTestTrait.php @@ -216,7 +216,7 @@ public function doneShouldTriggerFatalErrorWithDeepNestingPromiseChains() $result = resolve(resolve($d->promise()->then(function () use ($exception) { $d = new Deferred(); - $d->resolve(); + $d->resolve(null); return resolve($d->promise()->then(function () {}))->then( function () use ($exception) { @@ -227,7 +227,7 @@ function () use ($exception) { $result->done(); - $d->resolve(); + $d->resolve(null); $errors = $errorCollector->stop();