From c4e614538c0a02e4b1ac0db1db47c8075e7ff3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 15 Jun 2023 22:15:33 +0200 Subject: [PATCH] Improve type definitions and update to PHPStan level `max` --- README.md | 2 +- phpstan.neon.dist | 2 +- src/Deferred.php | 8 +++ src/Exception/CompositeException.php | 4 +- src/Internal/CancellationQueue.php | 7 ++ src/Internal/FulfilledPromise.php | 5 ++ src/Internal/RejectedPromise.php | 4 ++ src/Promise.php | 9 ++- src/functions.php | 16 +++-- tests/DeferredTest.php | 8 +-- tests/FunctionAllTest.php | 20 +++--- tests/FunctionAnyTest.php | 28 ++++---- tests/FunctionCheckTypehintTest.php | 44 ++++++------ tests/FunctionRaceTest.php | 20 +++--- tests/FunctionRejectTest.php | 2 +- tests/FunctionResolveTest.php | 14 ++-- tests/Internal/CancellationQueueTest.php | 14 ++-- tests/Internal/FulfilledPromiseTest.php | 7 +- tests/Internal/RejectedPromiseTest.php | 3 +- .../PromiseAdapter/CallbackPromiseAdapter.php | 18 +++-- .../PromiseAdapterInterface.php | 8 +-- tests/PromiseTest.php | 36 +++++----- tests/PromiseTest/CancelTestTrait.php | 41 ++++++------ .../PromiseTest/PromiseFulfilledTestTrait.php | 57 ++++++---------- tests/PromiseTest/PromisePendingTestTrait.php | 25 ++----- .../PromiseTest/PromiseRejectedTestTrait.php | 67 ++++++++----------- tests/PromiseTest/PromiseSettledTestTrait.php | 25 ++----- tests/PromiseTest/RejectTestTrait.php | 23 +++---- tests/PromiseTest/ResolveTestTrait.php | 29 ++++---- tests/TestCase.php | 29 ++++---- .../fixtures/CallbackWithDNFTypehintClass.php | 9 ++- .../CallbackWithIntersectionTypehintClass.php | 9 ++- tests/fixtures/CallbackWithTypehintClass.php | 6 +- .../CallbackWithUnionTypehintClass.php | 9 ++- .../fixtures/CallbackWithoutTypehintClass.php | 6 +- tests/fixtures/IterableException.php | 1 + .../fixtures/SimpleFulfilledTestThenable.php | 18 ++--- tests/fixtures/SimpleTestCancellable.php | 3 +- .../SimpleTestCancellableThenable.php | 7 +- 39 files changed, 317 insertions(+), 326 deletions(-) diff --git a/README.md b/README.md index 79c05834..7bfb2382 100644 --- a/README.md +++ b/README.md @@ -602,7 +602,7 @@ To run the test suite, go to the project root and run: vendor/bin/phpunit ``` -On top of this, we use PHPStan on level 3 to ensure type safety across the project: +On top of this, we use PHPStan on max level to ensure type safety across the project: ```bash vendor/bin/phpstan diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 5b3540e9..895c8410 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 3 + level: max paths: - src/ diff --git a/src/Deferred.php b/src/Deferred.php index b1abfd3c..82f66dad 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -4,8 +4,13 @@ final class Deferred { + /** @var Promise */ private $promise; + + /** @var callable */ private $resolveCallback; + + /** @var callable */ private $rejectCallback; public function __construct(callable $canceller = null) @@ -21,6 +26,9 @@ public function promise(): PromiseInterface return $this->promise; } + /** + * @param mixed $value + */ public function resolve($value): void { ($this->resolveCallback)($value); diff --git a/src/Exception/CompositeException.php b/src/Exception/CompositeException.php index 022bd0ae..2e672a04 100644 --- a/src/Exception/CompositeException.php +++ b/src/Exception/CompositeException.php @@ -11,9 +11,11 @@ */ class CompositeException extends \Exception { + /** @var \Throwable[] */ private $throwables; - public function __construct(array $throwables, $message = '', $code = 0, $previous = null) + /** @param \Throwable[] $throwables */ + public function __construct(array $throwables, string $message = '', int $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); diff --git a/src/Internal/CancellationQueue.php b/src/Internal/CancellationQueue.php index 35fb1a8b..604b19c8 100644 --- a/src/Internal/CancellationQueue.php +++ b/src/Internal/CancellationQueue.php @@ -7,7 +7,10 @@ */ final class CancellationQueue { + /** @var bool */ private $started = false; + + /** @var object[] */ private $queue = []; public function __invoke(): void @@ -20,6 +23,9 @@ public function __invoke(): void $this->drain(); } + /** + * @param mixed $cancellable + */ public function enqueue($cancellable): void { if (!\is_object($cancellable) || !\method_exists($cancellable, 'then') || !\method_exists($cancellable, 'cancel')) { @@ -37,6 +43,7 @@ private function drain(): void { for ($i = \key($this->queue); isset($this->queue[$i]); $i++) { $cancellable = $this->queue[$i]; + assert(\method_exists($cancellable, 'cancel')); $exception = null; diff --git a/src/Internal/FulfilledPromise.php b/src/Internal/FulfilledPromise.php index d3e535e0..0712f763 100644 --- a/src/Internal/FulfilledPromise.php +++ b/src/Internal/FulfilledPromise.php @@ -10,8 +10,13 @@ */ final class FulfilledPromise implements PromiseInterface { + /** @var mixed */ private $value; + /** + * @param mixed $value + * @throws \InvalidArgumentException + */ public function __construct($value = null) { if ($value instanceof PromiseInterface) { diff --git a/src/Internal/RejectedPromise.php b/src/Internal/RejectedPromise.php index a8360e24..cbd8ef53 100644 --- a/src/Internal/RejectedPromise.php +++ b/src/Internal/RejectedPromise.php @@ -11,8 +11,12 @@ */ final class RejectedPromise implements PromiseInterface { + /** @var \Throwable */ private $reason; + /** + * @param \Throwable $reason + */ public function __construct(\Throwable $reason) { $this->reason = $reason; diff --git a/src/Promise.php b/src/Promise.php index 5f886c9e..a2d72b6d 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -6,11 +6,16 @@ final class Promise implements PromiseInterface { + /** @var ?callable */ private $canceller; + + /** @var ?PromiseInterface */ private $result; + /** @var callable[] */ private $handlers = []; + /** @var int */ private $requiredCancelRequests = 0; public function __construct(callable $resolver, callable $canceller = null) @@ -46,6 +51,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null): return new static( $this->resolver($onFulfilled, $onRejected), static function () use (&$parent) { + assert($parent instanceof self); --$parent->requiredCancelRequests; if ($parent->requiredCancelRequests <= 0) { @@ -187,7 +193,7 @@ private function settle(PromiseInterface $result): void } } - private function unwrap($promise): PromiseInterface + private function unwrap(PromiseInterface $promise): PromiseInterface { while ($promise instanceof self && null !== $promise->result) { $promise = $promise->result; @@ -213,6 +219,7 @@ private function call(callable $cb): void } elseif (\is_object($callback) && !$callback instanceof \Closure) { $ref = new \ReflectionMethod($callback, '__invoke'); } else { + assert($callback instanceof \Closure || \is_string($callback)); $ref = new \ReflectionFunction($callback); } $args = $ref->getNumberOfParameters(); diff --git a/src/functions.php b/src/functions.php index cf1587d5..c42b715e 100644 --- a/src/functions.php +++ b/src/functions.php @@ -68,7 +68,7 @@ function reject(\Throwable $reason): PromiseInterface * will be an array containing the resolution values of each of the items in * `$promisesOrValues`. * - * @param iterable $promisesOrValues + * @param iterable $promisesOrValues * @return PromiseInterface */ function all(iterable $promisesOrValues): PromiseInterface @@ -77,6 +77,7 @@ function all(iterable $promisesOrValues): PromiseInterface return new Promise(function ($resolve, $reject) use ($promisesOrValues, $cancellationQueue): void { $toResolve = 0; + /** @var bool */ $continue = true; $values = []; @@ -118,7 +119,7 @@ function (\Throwable $reason) use (&$continue, $reject): void { * The returned promise will become **infinitely pending** if `$promisesOrValues` * contains 0 items. * - * @param iterable $promisesOrValues + * @param iterable $promisesOrValues * @return PromiseInterface */ function race(iterable $promisesOrValues): PromiseInterface @@ -153,7 +154,7 @@ function race(iterable $promisesOrValues): PromiseInterface * The returned promise will also reject with a `React\Promise\Exception\LengthException` * if `$promisesOrValues` contains 0 items. * - * @param iterable $promisesOrValues + * @param iterable $promisesOrValues * @return PromiseInterface */ function any(iterable $promisesOrValues): PromiseInterface @@ -215,6 +216,7 @@ function _checkTypehint(callable $callback, \Throwable $reason): bool } elseif (\is_object($callback) && !$callback instanceof \Closure) { $callbackReflection = new \ReflectionMethod($callback, '__invoke'); } else { + assert($callback instanceof \Closure || \is_string($callback)); $callbackReflection = new \ReflectionFunction($callback); } @@ -257,16 +259,16 @@ function _checkTypehint(callable $callback, \Throwable $reason): bool if ($type instanceof \ReflectionIntersectionType) { foreach ($type->getTypes() as $typeToMatch) { assert($typeToMatch instanceof \ReflectionNamedType); - if (!($matches = ($typeToMatch->isBuiltin() && \gettype($reason) === $typeToMatch->getName()) - || (new \ReflectionClass($typeToMatch->getName()))->isInstance($reason))) { + $name = $typeToMatch->getName(); + if (!($matches = (!$typeToMatch->isBuiltin() && $reason instanceof $name))) { break; } } assert(isset($matches)); } else { assert($type instanceof \ReflectionNamedType); - $matches = ($type->isBuiltin() && \gettype($reason) === $type->getName()) - || (new \ReflectionClass($type->getName()))->isInstance($reason); + $name = $type->getName(); + $matches = !$type->isBuiltin() && $reason instanceof $name; } // If we look for a single match (union), we can return early on match diff --git a/tests/DeferredTest.php b/tests/DeferredTest.php index cd732702..bb8f08d6 100644 --- a/tests/DeferredTest.php +++ b/tests/DeferredTest.php @@ -8,7 +8,7 @@ class DeferredTest extends TestCase { use PromiseTest\FullTestTrait; - public function getPromiseTestAdapter(callable $canceller = null) + public function getPromiseTestAdapter(callable $canceller = null): CallbackPromiseAdapter { $d = new Deferred($canceller); @@ -21,7 +21,7 @@ public function getPromiseTestAdapter(callable $canceller = null) } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException() + public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException(): void { gc_collect_cycles(); $deferred = new Deferred(function ($resolve, $reject) { @@ -34,7 +34,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithEx } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException() + public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException(): void { gc_collect_cycles(); gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on @@ -49,7 +49,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejects } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndExplicitlyRejectWithException() + public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndExplicitlyRejectWithException(): void { gc_collect_cycles(); gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on diff --git a/tests/FunctionAllTest.php b/tests/FunctionAllTest.php index 4d91eb04..94b5c049 100644 --- a/tests/FunctionAllTest.php +++ b/tests/FunctionAllTest.php @@ -7,7 +7,7 @@ class FunctionAllTest extends TestCase { /** @test */ - public function shouldResolveEmptyInput() + public function shouldResolveEmptyInput(): void { $mock = $this->createCallableMock(); $mock @@ -20,7 +20,7 @@ public function shouldResolveEmptyInput() } /** @test */ - public function shouldResolveValuesArray() + public function shouldResolveValuesArray(): void { $mock = $this->createCallableMock(); $mock @@ -33,7 +33,7 @@ public function shouldResolveValuesArray() } /** @test */ - public function shouldResolvePromisesArray() + public function shouldResolvePromisesArray(): void { $mock = $this->createCallableMock(); $mock @@ -46,7 +46,7 @@ public function shouldResolvePromisesArray() } /** @test */ - public function shouldResolveSparseArrayInput() + public function shouldResolveSparseArrayInput(): void { $mock = $this->createCallableMock(); $mock @@ -59,7 +59,7 @@ public function shouldResolveSparseArrayInput() } /** @test */ - public function shouldResolveValuesGenerator() + public function shouldResolveValuesGenerator(): void { $mock = $this->createCallableMock(); $mock @@ -77,7 +77,7 @@ public function shouldResolveValuesGenerator() } /** @test */ - public function shouldResolveValuesGeneratorEmpty() + public function shouldResolveValuesGeneratorEmpty(): void { $mock = $this->createCallableMock(); $mock @@ -86,7 +86,7 @@ public function shouldResolveValuesGeneratorEmpty() ->with(self::identicalTo([])); $gen = (function () { - if (false) { + if (false) { // @phpstan-ignore-line yield; } })(); @@ -95,7 +95,7 @@ public function shouldResolveValuesGeneratorEmpty() } /** @test */ - public function shouldRejectIfAnyInputPromiseRejects() + public function shouldRejectIfAnyInputPromiseRejects(): void { $exception2 = new Exception(); $exception3 = new Exception(); @@ -111,7 +111,7 @@ public function shouldRejectIfAnyInputPromiseRejects() } /** @test */ - public function shouldRejectInfiteGeneratorOrRejectedPromises() + public function shouldRejectInfiteGeneratorOrRejectedPromises(): void { $mock = $this->createCallableMock(); $mock @@ -129,7 +129,7 @@ public function shouldRejectInfiteGeneratorOrRejectedPromises() } /** @test */ - public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises() + public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises(): void { $mock = $this->createCallableMock(); $mock diff --git a/tests/FunctionAnyTest.php b/tests/FunctionAnyTest.php index f0d1afeb..563f882e 100644 --- a/tests/FunctionAnyTest.php +++ b/tests/FunctionAnyTest.php @@ -9,7 +9,7 @@ class FunctionAnyTest extends TestCase { /** @test */ - public function shouldRejectWithLengthExceptionWithEmptyInputArray() + public function shouldRejectWithLengthExceptionWithEmptyInputArray(): void { $mock = $this->createCallableMock(); $mock @@ -27,7 +27,7 @@ public function shouldRejectWithLengthExceptionWithEmptyInputArray() } /** @test */ - public function shouldRejectWithLengthExceptionWithEmptyInputGenerator() + public function shouldRejectWithLengthExceptionWithEmptyInputGenerator(): void { $mock = $this->createCallableMock(); $mock @@ -36,7 +36,7 @@ public function shouldRejectWithLengthExceptionWithEmptyInputGenerator() ->with(new LengthException('Must contain at least 1 item but contains only 0 items.')); $gen = (function () { - if (false) { + if (false) { // @phpstan-ignore-line yield; } })(); @@ -45,7 +45,7 @@ public function shouldRejectWithLengthExceptionWithEmptyInputGenerator() } /** @test */ - public function shouldResolveWithAnInputValue() + public function shouldResolveWithAnInputValue(): void { $mock = $this->createCallableMock(); $mock @@ -58,7 +58,7 @@ public function shouldResolveWithAnInputValue() } /** @test */ - public function shouldResolveWithAPromisedInputValue() + public function shouldResolveWithAPromisedInputValue(): void { $mock = $this->createCallableMock(); $mock @@ -71,7 +71,7 @@ public function shouldResolveWithAPromisedInputValue() } /** @test */ - public function shouldResolveWithAnInputValueFromDeferred() + public function shouldResolveWithAnInputValueFromDeferred(): void { $mock = $this->createCallableMock(); $mock @@ -87,7 +87,7 @@ public function shouldResolveWithAnInputValueFromDeferred() } /** @test */ - public function shouldResolveValuesGenerator() + public function shouldResolveValuesGenerator(): void { $mock = $this->createCallableMock(); $mock @@ -105,7 +105,7 @@ public function shouldResolveValuesGenerator() } /** @test */ - public function shouldResolveValuesInfiniteGenerator() + public function shouldResolveValuesInfiniteGenerator(): void { $mock = $this->createCallableMock(); $mock @@ -123,7 +123,7 @@ public function shouldResolveValuesInfiniteGenerator() } /** @test */ - public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected() + public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected(): void { $exception1 = new Exception(); $exception2 = new Exception(); @@ -145,7 +145,7 @@ public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected() } /** @test */ - public function shouldRejectWithAllRejectedInputValuesIfInputIsRejectedFromDeferred() + public function shouldRejectWithAllRejectedInputValuesIfInputIsRejectedFromDeferred(): void { $exception = new Exception(); @@ -168,7 +168,7 @@ public function shouldRejectWithAllRejectedInputValuesIfInputIsRejectedFromDefer } /** @test */ - public function shouldResolveWhenFirstInputPromiseResolves() + public function shouldResolveWhenFirstInputPromiseResolves(): void { $exception2 = new Exception(); $exception3 = new Exception(); @@ -184,7 +184,7 @@ public function shouldResolveWhenFirstInputPromiseResolves() } /** @test */ - public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue() + public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue(): void { $mock = $this->createCallableMock(); $mock @@ -203,7 +203,7 @@ public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue } /** @test */ - public function shouldCancelInputArrayPromises() + public function shouldCancelInputArrayPromises(): void { $promise1 = new Promise(function () {}, $this->expectCallableOnce()); $promise2 = new Promise(function () {}, $this->expectCallableOnce()); @@ -212,7 +212,7 @@ public function shouldCancelInputArrayPromises() } /** @test */ - public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() + public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills(): void { $deferred = new Deferred($this->expectCallableNever()); $deferred->resolve(null); diff --git a/tests/FunctionCheckTypehintTest.php b/tests/FunctionCheckTypehintTest.php index a8e772a3..6f7fc0f9 100644 --- a/tests/FunctionCheckTypehintTest.php +++ b/tests/FunctionCheckTypehintTest.php @@ -8,35 +8,35 @@ class FunctionCheckTypehintTest extends TestCase { /** @test */ - public function shouldAcceptClosureCallbackWithTypehint() + public function shouldAcceptClosureCallbackWithTypehint(): void { self::assertTrue(_checkTypehint(function (InvalidArgumentException $e) {}, new InvalidArgumentException())); self::assertFalse(_checkTypehint(function (InvalidArgumentException $e) {}, new Exception())); } /** @test */ - public function shouldAcceptFunctionStringCallbackWithTypehint() + public function shouldAcceptFunctionStringCallbackWithTypehint(): void { self::assertTrue(_checkTypehint(new CallbackWithTypehintClass(), new InvalidArgumentException())); self::assertFalse(_checkTypehint(new CallbackWithTypehintClass(), new Exception())); } /** @test */ - public function shouldAcceptInvokableObjectCallbackWithTypehint() + public function shouldAcceptInvokableObjectCallbackWithTypehint(): void { self::assertTrue(_checkTypehint(new CallbackWithTypehintClass(), new InvalidArgumentException())); self::assertFalse(_checkTypehint(new CallbackWithTypehintClass(), new Exception())); } /** @test */ - public function shouldAcceptObjectMethodCallbackWithTypehint() + public function shouldAcceptObjectMethodCallbackWithTypehint(): void { self::assertTrue(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new InvalidArgumentException())); self::assertFalse(_checkTypehint([new CallbackWithTypehintClass(), 'testCallback'], new Exception())); } /** @test */ - public function shouldAcceptStaticClassCallbackWithTypehint() + public function shouldAcceptStaticClassCallbackWithTypehint(): void { self::assertTrue(_checkTypehint([CallbackWithTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException())); self::assertFalse(_checkTypehint([CallbackWithTypehintClass::class, 'testCallbackStatic'], new Exception())); @@ -46,7 +46,7 @@ public function shouldAcceptStaticClassCallbackWithTypehint() * @test * @requires PHP 8 */ - public function shouldAcceptClosureCallbackWithUnionTypehint() + public function shouldAcceptClosureCallbackWithUnionTypehint(): void { eval( 'namespace React\Promise;' . @@ -59,7 +59,7 @@ public function shouldAcceptClosureCallbackWithUnionTypehint() * @test * @requires PHP 8 */ - public function shouldAcceptInvokableObjectCallbackWithUnionTypehint() + public function shouldAcceptInvokableObjectCallbackWithUnionTypehint(): void { self::assertTrue(_checkTypehint(new CallbackWithUnionTypehintClass(), new InvalidArgumentException())); self::assertFalse(_checkTypehint(new CallbackWithUnionTypehintClass(), new Exception())); @@ -69,7 +69,7 @@ public function shouldAcceptInvokableObjectCallbackWithUnionTypehint() * @test * @requires PHP 8 */ - public function shouldAcceptObjectMethodCallbackWithUnionTypehint() + public function shouldAcceptObjectMethodCallbackWithUnionTypehint(): void { self::assertTrue(_checkTypehint([new CallbackWithUnionTypehintClass(), 'testCallback'], new InvalidArgumentException())); self::assertFalse(_checkTypehint([new CallbackWithUnionTypehintClass(), 'testCallback'], new Exception())); @@ -79,7 +79,7 @@ public function shouldAcceptObjectMethodCallbackWithUnionTypehint() * @test * @requires PHP 8 */ - public function shouldAcceptStaticClassCallbackWithUnionTypehint() + public function shouldAcceptStaticClassCallbackWithUnionTypehint(): void { self::assertTrue(_checkTypehint([CallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException())); self::assertFalse(_checkTypehint([CallbackWithUnionTypehintClass::class, 'testCallbackStatic'], new Exception())); @@ -89,7 +89,7 @@ public function shouldAcceptStaticClassCallbackWithUnionTypehint() * @test * @requires PHP 8.1 */ - public function shouldAcceptInvokableObjectCallbackWithIntersectionTypehint() + public function shouldAcceptInvokableObjectCallbackWithIntersectionTypehint(): void { self::assertFalse(_checkTypehint(new CallbackWithIntersectionTypehintClass(), new \RuntimeException())); self::assertTrue(_checkTypehint(new CallbackWithIntersectionTypehintClass(), new CountableException())); @@ -99,7 +99,7 @@ public function shouldAcceptInvokableObjectCallbackWithIntersectionTypehint() * @test * @requires PHP 8.1 */ - public function shouldAcceptObjectMethodCallbackWithIntersectionTypehint() + public function shouldAcceptObjectMethodCallbackWithIntersectionTypehint(): void { self::assertFalse(_checkTypehint([new CallbackWithIntersectionTypehintClass(), 'testCallback'], new \RuntimeException())); self::assertTrue(_checkTypehint([new CallbackWithIntersectionTypehintClass(), 'testCallback'], new CountableException())); @@ -109,7 +109,7 @@ public function shouldAcceptObjectMethodCallbackWithIntersectionTypehint() * @test * @requires PHP 8.1 */ - public function shouldAcceptStaticClassCallbackWithIntersectionTypehint() + public function shouldAcceptStaticClassCallbackWithIntersectionTypehint(): void { self::assertFalse(_checkTypehint([CallbackWithIntersectionTypehintClass::class, 'testCallbackStatic'], new \RuntimeException())); self::assertTrue(_checkTypehint([CallbackWithIntersectionTypehintClass::class, 'testCallbackStatic'], new CountableException())); @@ -119,7 +119,7 @@ public function shouldAcceptStaticClassCallbackWithIntersectionTypehint() * @test * @requires PHP 8.2 */ - public function shouldAcceptInvokableObjectCallbackWithDNFTypehint() + public function shouldAcceptInvokableObjectCallbackWithDNFTypehint(): void { self::assertFalse(_checkTypehint(new CallbackWithDNFTypehintClass(), new \RuntimeException())); self::assertTrue(_checkTypehint(new CallbackWithDNFTypehintClass(), new IterableException())); @@ -130,7 +130,7 @@ public function shouldAcceptInvokableObjectCallbackWithDNFTypehint() * @test * @requires PHP 8.2 */ - public function shouldAcceptObjectMethodCallbackWithDNFTypehint() + public function shouldAcceptObjectMethodCallbackWithDNFTypehint(): void { self::assertFalse(_checkTypehint([new CallbackWithDNFTypehintClass(), 'testCallback'], new \RuntimeException())); self::assertTrue(_checkTypehint([new CallbackWithDNFTypehintClass(), 'testCallback'], new CountableException())); @@ -141,7 +141,7 @@ public function shouldAcceptObjectMethodCallbackWithDNFTypehint() * @test * @requires PHP 8.2 */ - public function shouldAcceptStaticClassCallbackWithDNFTypehint() + public function shouldAcceptStaticClassCallbackWithDNFTypehint(): void { self::assertFalse(_checkTypehint([CallbackWithDNFTypehintClass::class, 'testCallbackStatic'], new \RuntimeException())); self::assertTrue(_checkTypehint([CallbackWithDNFTypehintClass::class, 'testCallbackStatic'], new CountableException())); @@ -149,41 +149,41 @@ public function shouldAcceptStaticClassCallbackWithDNFTypehint() } /** @test */ - public function shouldAcceptClosureCallbackWithoutTypehint() + public function shouldAcceptClosureCallbackWithoutTypehint(): void { self::assertTrue(_checkTypehint(function (InvalidArgumentException $e) { }, new InvalidArgumentException())); } /** @test */ - public function shouldAcceptFunctionStringCallbackWithoutTypehint() + public function shouldAcceptFunctionStringCallbackWithoutTypehint(): void { self::assertTrue(_checkTypehint(new CallbackWithoutTypehintClass(), new InvalidArgumentException())); } /** @test */ - public function shouldAcceptInvokableObjectCallbackWithoutTypehint() + public function shouldAcceptInvokableObjectCallbackWithoutTypehint(): void { self::assertTrue(_checkTypehint(new CallbackWithoutTypehintClass(), new InvalidArgumentException())); } /** @test */ - public function shouldAcceptObjectMethodCallbackWithoutTypehint() + public function shouldAcceptObjectMethodCallbackWithoutTypehint(): void { self::assertTrue(_checkTypehint([new CallbackWithoutTypehintClass(), 'testCallback'], new InvalidArgumentException())); } /** @test */ - public function shouldAcceptStaticClassCallbackWithoutTypehint() + public function shouldAcceptStaticClassCallbackWithoutTypehint(): void { self::assertTrue(_checkTypehint([CallbackWithoutTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException())); } } -function testCallbackWithTypehint(InvalidArgumentException $e) +function testCallbackWithTypehint(InvalidArgumentException $e): void { } -function testCallbackWithoutTypehint() +function testCallbackWithoutTypehint(): void { } diff --git a/tests/FunctionRaceTest.php b/tests/FunctionRaceTest.php index 6e69be07..a0ac05b8 100644 --- a/tests/FunctionRaceTest.php +++ b/tests/FunctionRaceTest.php @@ -7,7 +7,7 @@ class FunctionRaceTest extends TestCase { /** @test */ - public function shouldReturnForeverPendingPromiseForEmptyInput() + public function shouldReturnForeverPendingPromiseForEmptyInput(): void { race( [] @@ -15,7 +15,7 @@ public function shouldReturnForeverPendingPromiseForEmptyInput() } /** @test */ - public function shouldResolveValuesArray() + public function shouldResolveValuesArray(): void { $mock = $this->createCallableMock(); $mock @@ -29,7 +29,7 @@ public function shouldResolveValuesArray() } /** @test */ - public function shouldResolvePromisesArray() + public function shouldResolvePromisesArray(): void { $mock = $this->createCallableMock(); $mock @@ -52,7 +52,7 @@ public function shouldResolvePromisesArray() } /** @test */ - public function shouldResolveSparseArrayInput() + public function shouldResolveSparseArrayInput(): void { $mock = $this->createCallableMock(); $mock @@ -66,7 +66,7 @@ public function shouldResolveSparseArrayInput() } /** @test */ - public function shouldResolveValuesGenerator() + public function shouldResolveValuesGenerator(): void { $mock = $this->createCallableMock(); $mock @@ -84,7 +84,7 @@ public function shouldResolveValuesGenerator() } /** @test */ - public function shouldResolveValuesInfiniteGenerator() + public function shouldResolveValuesInfiniteGenerator(): void { $mock = $this->createCallableMock(); $mock @@ -102,7 +102,7 @@ public function shouldResolveValuesInfiniteGenerator() } /** @test */ - public function shouldRejectIfFirstSettledPromiseRejects() + public function shouldRejectIfFirstSettledPromiseRejects(): void { $exception = new Exception(); @@ -127,7 +127,7 @@ public function shouldRejectIfFirstSettledPromiseRejects() } /** @test */ - public function shouldCancelInputArrayPromises() + public function shouldCancelInputArrayPromises(): void { $promise1 = new Promise(function () {}, $this->expectCallableOnce()); $promise2 = new Promise(function () {}, $this->expectCallableOnce()); @@ -136,7 +136,7 @@ public function shouldCancelInputArrayPromises() } /** @test */ - public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() + public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills(): void { $deferred = new Deferred($this->expectCallableNever()); $deferred->resolve(null); @@ -147,7 +147,7 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfill } /** @test */ - public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects() + public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects(): void { $deferred = new Deferred($this->expectCallableNever()); $deferred->reject(new Exception()); diff --git a/tests/FunctionRejectTest.php b/tests/FunctionRejectTest.php index d867cab8..38b7cc68 100644 --- a/tests/FunctionRejectTest.php +++ b/tests/FunctionRejectTest.php @@ -7,7 +7,7 @@ class FunctionRejectTest extends TestCase { /** @test */ - public function shouldRejectAnException() + public function shouldRejectAnException(): void { $exception = new Exception(); diff --git a/tests/FunctionResolveTest.php b/tests/FunctionResolveTest.php index b313fbe9..a1637b53 100644 --- a/tests/FunctionResolveTest.php +++ b/tests/FunctionResolveTest.php @@ -9,7 +9,7 @@ class FunctionResolveTest extends TestCase { /** @test */ - public function shouldResolveAnImmediateValue() + public function shouldResolveAnImmediateValue(): void { $expected = 123; @@ -27,7 +27,7 @@ public function shouldResolveAnImmediateValue() } /** @test */ - public function shouldResolveAFulfilledPromise() + public function shouldResolveAFulfilledPromise(): void { $expected = 123; @@ -47,7 +47,7 @@ public function shouldResolveAFulfilledPromise() } /** @test */ - public function shouldResolveAThenable() + public function shouldResolveAThenable(): void { $thenable = new SimpleFulfilledTestThenable(); @@ -65,7 +65,7 @@ public function shouldResolveAThenable() } /** @test */ - public function shouldResolveACancellableThenable() + public function shouldResolveACancellableThenable(): void { $thenable = new SimpleTestCancellableThenable(); @@ -76,7 +76,7 @@ public function shouldResolveACancellableThenable() } /** @test */ - public function shouldRejectARejectedPromise() + public function shouldRejectARejectedPromise(): void { $exception = new Exception(); @@ -96,7 +96,7 @@ public function shouldRejectARejectedPromise() } /** @test */ - public function shouldSupportDeepNestingInPromiseChains() + public function shouldSupportDeepNestingInPromiseChains(): void { $d = new Deferred(); $d->resolve(false); @@ -126,7 +126,7 @@ function ($val) { } /** @test */ - public function shouldSupportVeryDeepNestedPromises() + public function shouldSupportVeryDeepNestedPromises(): void { $deferreds = []; diff --git a/tests/Internal/CancellationQueueTest.php b/tests/Internal/CancellationQueueTest.php index f168cb34..c2907f73 100644 --- a/tests/Internal/CancellationQueueTest.php +++ b/tests/Internal/CancellationQueueTest.php @@ -11,7 +11,7 @@ class CancellationQueueTest extends TestCase { /** @test */ - public function acceptsSimpleCancellableThenable() + public function acceptsSimpleCancellableThenable(): void { $p = new SimpleTestCancellableThenable(); @@ -24,7 +24,7 @@ public function acceptsSimpleCancellableThenable() } /** @test */ - public function ignoresSimpleCancellable() + public function ignoresSimpleCancellable(): void { $p = new SimpleTestCancellable(); @@ -37,7 +37,7 @@ public function ignoresSimpleCancellable() } /** @test */ - public function callsCancelOnPromisesEnqueuedBeforeStart() + public function callsCancelOnPromisesEnqueuedBeforeStart(): void { $d1 = $this->getCancellableDeferred(); $d2 = $this->getCancellableDeferred(); @@ -50,7 +50,7 @@ public function callsCancelOnPromisesEnqueuedBeforeStart() } /** @test */ - public function callsCancelOnPromisesEnqueuedAfterStart() + public function callsCancelOnPromisesEnqueuedAfterStart(): void { $d1 = $this->getCancellableDeferred(); $d2 = $this->getCancellableDeferred(); @@ -64,7 +64,7 @@ public function callsCancelOnPromisesEnqueuedAfterStart() } /** @test */ - public function doesNotCallCancelTwiceWhenStartedTwice() + public function doesNotCallCancelTwiceWhenStartedTwice(): void { $d = $this->getCancellableDeferred(); @@ -78,7 +78,7 @@ public function doesNotCallCancelTwiceWhenStartedTwice() /** * @test */ - public function rethrowsExceptionsThrownFromCancel() + public function rethrowsExceptionsThrownFromCancel(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('test'); @@ -96,7 +96,7 @@ public function rethrowsExceptionsThrownFromCancel() $cancellationQueue(); } - private function getCancellableDeferred() + private function getCancellableDeferred(): Deferred { return new Deferred($this->expectCallableOnce()); } diff --git a/tests/Internal/FulfilledPromiseTest.php b/tests/Internal/FulfilledPromiseTest.php index 3928b273..073b9d7a 100644 --- a/tests/Internal/FulfilledPromiseTest.php +++ b/tests/Internal/FulfilledPromiseTest.php @@ -14,8 +14,9 @@ class FulfilledPromiseTest extends TestCase use PromiseSettledTestTrait, PromiseFulfilledTestTrait; - public function getPromiseTestAdapter(callable $canceller = null) + public function getPromiseTestAdapter(callable $canceller = null): CallbackPromiseAdapter { + /** @var ?FulfilledPromise */ $promise = null; return new CallbackPromiseAdapter([ @@ -45,9 +46,9 @@ public function getPromiseTestAdapter(callable $canceller = null) /** * @test */ - public function shouldThrowExceptionIfConstructedWithAPromise() + public function shouldThrowExceptionIfConstructedWithAPromise(): void { $this->expectException(InvalidArgumentException::class); - return new FulfilledPromise(new FulfilledPromise()); + new FulfilledPromise(new FulfilledPromise()); } } diff --git a/tests/Internal/RejectedPromiseTest.php b/tests/Internal/RejectedPromiseTest.php index e2e7a15f..72cef091 100644 --- a/tests/Internal/RejectedPromiseTest.php +++ b/tests/Internal/RejectedPromiseTest.php @@ -14,8 +14,9 @@ class RejectedPromiseTest extends TestCase use PromiseSettledTestTrait, PromiseRejectedTestTrait; - public function getPromiseTestAdapter(callable $canceller = null) + public function getPromiseTestAdapter(callable $canceller = null): CallbackPromiseAdapter { + /** @var ?RejectedPromise */ $promise = null; return new CallbackPromiseAdapter([ diff --git a/tests/PromiseAdapter/CallbackPromiseAdapter.php b/tests/PromiseAdapter/CallbackPromiseAdapter.php index ec7ebab4..14a0acd4 100644 --- a/tests/PromiseAdapter/CallbackPromiseAdapter.php +++ b/tests/PromiseAdapter/CallbackPromiseAdapter.php @@ -6,30 +6,34 @@ class CallbackPromiseAdapter implements PromiseAdapterInterface { + /** @var callable[] */ private $callbacks; + /** + * @param callable[] $callbacks + */ public function __construct(array $callbacks) { $this->callbacks = $callbacks; } - public function promise(): ?PromiseInterface + public function promise(): PromiseInterface { return ($this->callbacks['promise'])(...func_get_args()); } - public function resolve(): ?PromiseInterface + public function resolve(): void { - return ($this->callbacks['resolve'])(...func_get_args()); + ($this->callbacks['resolve'])(...func_get_args()); } - public function reject(): ?PromiseInterface + public function reject(): void { - return ($this->callbacks['reject'])(...func_get_args()); + ($this->callbacks['reject'])(...func_get_args()); } - public function settle(): ?PromiseInterface + public function settle(): void { - return ($this->callbacks['settle'])(...func_get_args()); + ($this->callbacks['settle'])(...func_get_args()); } } diff --git a/tests/PromiseAdapter/PromiseAdapterInterface.php b/tests/PromiseAdapter/PromiseAdapterInterface.php index 8581f303..727fd514 100644 --- a/tests/PromiseAdapter/PromiseAdapterInterface.php +++ b/tests/PromiseAdapter/PromiseAdapterInterface.php @@ -6,8 +6,8 @@ interface PromiseAdapterInterface { - public function promise(): ?PromiseInterface; - public function resolve(): ?PromiseInterface; - public function reject(): ?PromiseInterface; - public function settle(): ?PromiseInterface; + public function promise(): PromiseInterface; + public function resolve(): void; + public function reject(): void; + public function settle(): void; } diff --git a/tests/PromiseTest.php b/tests/PromiseTest.php index 15a3528d..ae6b92bc 100644 --- a/tests/PromiseTest.php +++ b/tests/PromiseTest.php @@ -9,7 +9,7 @@ class PromiseTest extends TestCase { use PromiseTest\FullTestTrait; - public function getPromiseTestAdapter(callable $canceller = null) + public function getPromiseTestAdapter(callable $canceller = null): CallbackPromiseAdapter { $resolveCallback = $rejectCallback = null; @@ -29,7 +29,7 @@ public function getPromiseTestAdapter(callable $canceller = null) } /** @test */ - public function shouldRejectIfResolverThrowsException() + public function shouldRejectIfResolverThrowsException(): void { $exception = new Exception('foo'); @@ -48,7 +48,7 @@ public function shouldRejectIfResolverThrowsException() } /** @test */ - public function shouldResolveWithoutCreatingGarbageCyclesIfResolverResolvesWithException() + public function shouldResolveWithoutCreatingGarbageCyclesIfResolverResolvesWithException(): void { gc_collect_cycles(); $promise = new Promise(function ($resolve) { @@ -60,7 +60,7 @@ public function shouldResolveWithoutCreatingGarbageCyclesIfResolverResolvesWithE } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptionWithoutResolver() + public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptionWithoutResolver(): void { gc_collect_cycles(); $promise = new Promise(function () { @@ -72,7 +72,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptio } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfResolverRejectsWithException() + public function shouldRejectWithoutCreatingGarbageCyclesIfResolverRejectsWithException(): void { gc_collect_cycles(); $promise = new Promise(function ($resolve, $reject) { @@ -84,7 +84,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfResolverRejectsWithExc } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException() + public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException(): void { gc_collect_cycles(); $promise = new Promise(function ($resolve, $reject) { }, function ($resolve, $reject) { @@ -97,7 +97,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithEx } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException() + public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException(): void { gc_collect_cycles(); $promise = new Promise(function ($resolve, $reject) { }, function ($resolve, $reject) { @@ -110,7 +110,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejects } /** @test */ - public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsException() + public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsException(): void { gc_collect_cycles(); $promise = new Promise(function ($resolve, $reject) { @@ -134,7 +134,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptio * @test * @requires PHP 7 */ - public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException() + public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException(): void { gc_collect_cycles(); $promise = new Promise(function () {}, function () use (&$promise) { @@ -152,7 +152,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReference * @requires PHP 7 * @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException */ - public function shouldRejectWithoutCreatingGarbageCyclesIfResolverWithReferenceThrowsException() + public function shouldRejectWithoutCreatingGarbageCyclesIfResolverWithReferenceThrowsException(): void { gc_collect_cycles(); $promise = new Promise(function () use (&$promise) { @@ -169,7 +169,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfResolverWithReferenceT * @requires PHP 7 * @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException */ - public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndResolverThrowsException() + public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndResolverThrowsException(): void { gc_collect_cycles(); $promise = new Promise(function () { @@ -183,7 +183,7 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenc } /** @test */ - public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromise() + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromise(): void { gc_collect_cycles(); $promise = new Promise(function () { }); @@ -193,7 +193,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro } /** @test */ - public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithThenFollowers() + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithThenFollowers(): void { gc_collect_cycles(); $promise = new Promise(function () { }); @@ -204,7 +204,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro } /** @test */ - public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithCatchFollowers() + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithCatchFollowers(): void { gc_collect_cycles(); $promise = new Promise(function () { }); @@ -215,7 +215,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro } /** @test */ - public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithFinallyFollowers() + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithFinallyFollowers(): void { gc_collect_cycles(); $promise = new Promise(function () { }); @@ -229,7 +229,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro * @test * @deprecated */ - public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithOtherwiseFollowers() + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithOtherwiseFollowers(): void { gc_collect_cycles(); $promise = new Promise(function () { }); @@ -243,7 +243,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro * @test * @deprecated */ - public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithAlwaysFollowers() + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithAlwaysFollowers(): void { gc_collect_cycles(); $promise = new Promise(function () { }); @@ -254,7 +254,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro } /** @test */ - public function shouldFulfillIfFullfilledWithSimplePromise() + public function shouldFulfillIfFullfilledWithSimplePromise(): void { gc_collect_cycles(); $promise = new Promise(function () { diff --git a/tests/PromiseTest/CancelTestTrait.php b/tests/PromiseTest/CancelTestTrait.php index 00c1931e..14def13a 100644 --- a/tests/PromiseTest/CancelTestTrait.php +++ b/tests/PromiseTest/CancelTestTrait.php @@ -8,15 +8,12 @@ trait CancelTestTrait { - /** - * @return PromiseAdapterInterface - */ - abstract public function getPromiseTestAdapter(callable $canceller = null); + abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface; /** @test */ - public function cancelShouldCallCancellerWithResolverArguments() + public function cancelShouldCallCancellerWithResolverArguments(): void { - $args = null; + $args = []; $adapter = $this->getPromiseTestAdapter(function ($resolve, $reject) use (&$args) { $args = func_get_args(); }); @@ -29,7 +26,7 @@ public function cancelShouldCallCancellerWithResolverArguments() } /** @test */ - public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed() + public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed(): void { $args = null; $adapter = $this->getPromiseTestAdapter(function () use (&$args) { @@ -42,7 +39,7 @@ public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed() } /** @test */ - public function cancelShouldFulfillPromiseIfCancellerFulfills() + public function cancelShouldFulfillPromiseIfCancellerFulfills(): void { $adapter = $this->getPromiseTestAdapter(function ($resolve) { $resolve(1); @@ -61,7 +58,7 @@ public function cancelShouldFulfillPromiseIfCancellerFulfills() } /** @test */ - public function cancelShouldRejectPromiseIfCancellerRejects() + public function cancelShouldRejectPromiseIfCancellerRejects(): void { $exception = new Exception(); @@ -82,7 +79,7 @@ public function cancelShouldRejectPromiseIfCancellerRejects() } /** @test */ - public function cancelShouldRejectPromiseWithExceptionIfCancellerThrows() + public function cancelShouldRejectPromiseWithExceptionIfCancellerThrows(): void { $e = new Exception(); @@ -103,7 +100,7 @@ public function cancelShouldRejectPromiseWithExceptionIfCancellerThrows() } /** @test */ - public function cancelShouldCallCancellerOnlyOnceIfCancellerResolves() + public function cancelShouldCallCancellerOnlyOnceIfCancellerResolves(): void { $mock = $this->createCallableMock(); $mock @@ -120,7 +117,7 @@ public function cancelShouldCallCancellerOnlyOnceIfCancellerResolves() } /** @test */ - public function cancelShouldHaveNoEffectIfCancellerDoesNothing() + public function cancelShouldHaveNoEffectIfCancellerDoesNothing(): void { $adapter = $this->getPromiseTestAdapter(function () {}); @@ -132,7 +129,7 @@ public function cancelShouldHaveNoEffectIfCancellerDoesNothing() } /** @test */ - public function cancelShouldCallCancellerFromDeepNestedPromiseChain() + public function cancelShouldCallCancellerFromDeepNestedPromiseChain(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce()); @@ -153,7 +150,7 @@ public function cancelShouldCallCancellerFromDeepNestedPromiseChain() } /** @test */ - public function cancelCalledOnChildrenSouldOnlyCancelWhenAllChildrenCancelled() + public function cancelCalledOnChildrenSouldOnlyCancelWhenAllChildrenCancelled(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); @@ -168,7 +165,7 @@ public function cancelCalledOnChildrenSouldOnlyCancelWhenAllChildrenCancelled() } /** @test */ - public function cancelShouldTriggerCancellerWhenAllChildrenCancel() + public function cancelShouldTriggerCancellerWhenAllChildrenCancel(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce()); @@ -184,7 +181,7 @@ public function cancelShouldTriggerCancellerWhenAllChildrenCancel() } /** @test */ - public function cancelShouldNotTriggerCancellerWhenCancellingOneChildrenMultipleTimes() + public function cancelShouldNotTriggerCancellerWhenCancellingOneChildrenMultipleTimes(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); @@ -200,7 +197,7 @@ public function cancelShouldNotTriggerCancellerWhenCancellingOneChildrenMultiple } /** @test */ - public function cancelShouldTriggerCancellerOnlyOnceWhenCancellingMultipleTimes() + public function cancelShouldTriggerCancellerOnlyOnceWhenCancellingMultipleTimes(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce()); @@ -209,7 +206,7 @@ public function cancelShouldTriggerCancellerOnlyOnceWhenCancellingMultipleTimes( } /** @test */ - public function cancelShouldAlwaysTriggerCancellerWhenCalledOnRootPromise() + public function cancelShouldAlwaysTriggerCancellerWhenCalledOnRootPromise(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce()); @@ -224,7 +221,7 @@ public function cancelShouldAlwaysTriggerCancellerWhenCalledOnRootPromise() } /** @test */ - public function cancelShouldTriggerCancellerWhenFollowerCancels() + public function cancelShouldTriggerCancellerWhenFollowerCancels(): void { $adapter1 = $this->getPromiseTestAdapter($this->expectCallableOnce()); @@ -239,7 +236,7 @@ public function cancelShouldTriggerCancellerWhenFollowerCancels() } /** @test */ - public function cancelShouldNotTriggerCancellerWhenCancellingOnlyOneFollower() + public function cancelShouldNotTriggerCancellerWhenCancellingOnlyOneFollower(): void { $adapter1 = $this->getPromiseTestAdapter($this->expectCallableNever()); @@ -257,7 +254,7 @@ public function cancelShouldNotTriggerCancellerWhenCancellingOnlyOneFollower() } /** @test */ - public function cancelCalledOnFollowerShouldOnlyCancelWhenAllChildrenAndFollowerCancelled() + public function cancelCalledOnFollowerShouldOnlyCancelWhenAllChildrenAndFollowerCancelled(): void { $adapter1 = $this->getPromiseTestAdapter($this->expectCallableOnce()); @@ -275,7 +272,7 @@ public function cancelCalledOnFollowerShouldOnlyCancelWhenAllChildrenAndFollower } /** @test */ - public function cancelShouldNotTriggerCancellerWhenCancellingFollowerButNotChildren() + public function cancelShouldNotTriggerCancellerWhenCancellingFollowerButNotChildren(): void { $adapter1 = $this->getPromiseTestAdapter($this->expectCallableNever()); diff --git a/tests/PromiseTest/PromiseFulfilledTestTrait.php b/tests/PromiseTest/PromiseFulfilledTestTrait.php index 331c8d96..d982214a 100644 --- a/tests/PromiseTest/PromiseFulfilledTestTrait.php +++ b/tests/PromiseTest/PromiseFulfilledTestTrait.php @@ -10,13 +10,10 @@ trait PromiseFulfilledTestTrait { - /** - * @return PromiseAdapterInterface - */ - abstract public function getPromiseTestAdapter(callable $canceller = null); + abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface; /** @test */ - public function fulfilledPromiseShouldBeImmutable() + public function fulfilledPromiseShouldBeImmutable(): void { $adapter = $this->getPromiseTestAdapter(); @@ -37,7 +34,7 @@ public function fulfilledPromiseShouldBeImmutable() } /** @test */ - public function fulfilledPromiseShouldInvokeNewlyAddedCallback() + public function fulfilledPromiseShouldInvokeNewlyAddedCallback(): void { $adapter = $this->getPromiseTestAdapter(); @@ -54,7 +51,7 @@ public function fulfilledPromiseShouldInvokeNewlyAddedCallback() } /** @test */ - public function thenShouldForwardResultWhenCallbackIsNull() + public function thenShouldForwardResultWhenCallbackIsNull(): void { $adapter = $this->getPromiseTestAdapter(); @@ -77,7 +74,7 @@ public function thenShouldForwardResultWhenCallbackIsNull() } /** @test */ - public function thenShouldForwardCallbackResultToNextCallback() + public function thenShouldForwardCallbackResultToNextCallback(): void { $adapter = $this->getPromiseTestAdapter(); @@ -102,7 +99,7 @@ function ($val) { } /** @test */ - public function thenShouldForwardPromisedCallbackResultValueToNextCallback() + public function thenShouldForwardPromisedCallbackResultValueToNextCallback(): void { $adapter = $this->getPromiseTestAdapter(); @@ -127,7 +124,7 @@ function ($val) { } /** @test */ - public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackReturnsARejection() + public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackReturnsARejection(): void { $adapter = $this->getPromiseTestAdapter(); @@ -154,7 +151,7 @@ function () use ($exception) { } /** @test */ - public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackThrows() + public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackThrows(): void { $adapter = $this->getPromiseTestAdapter(); @@ -188,7 +185,7 @@ public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackThrows() * @test * @requires PHP 8.1 */ - public function thenShouldContinueToExecuteCallbacksWhenPriorCallbackSuspendsFiber() + public function thenShouldContinueToExecuteCallbacksWhenPriorCallbackSuspendsFiber(): void { $adapter = $this->getPromiseTestAdapter(); $adapter->resolve(42); @@ -212,17 +209,7 @@ public function thenShouldContinueToExecuteCallbacksWhenPriorCallbackSuspendsFib } /** @test */ - public function cancelShouldReturnNullForFulfilledPromise() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->resolve(null); - - self::assertNull($adapter->promise()->cancel()); - } - - /** @test */ - public function cancelShouldHaveNoEffectForFulfilledPromise() + public function cancelShouldHaveNoEffectForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); @@ -232,7 +219,7 @@ public function cancelShouldHaveNoEffectForFulfilledPromise() } /** @test */ - public function catchShouldNotInvokeRejectionHandlerForFulfilledPromise() + public function catchShouldNotInvokeRejectionHandlerForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -241,7 +228,7 @@ public function catchShouldNotInvokeRejectionHandlerForFulfilledPromise() } /** @test */ - public function finallyShouldNotSuppressValueForFulfilledPromise() + public function finallyShouldNotSuppressValueForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -260,7 +247,7 @@ public function finallyShouldNotSuppressValueForFulfilledPromise() } /** @test */ - public function finallyShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise() + public function finallyShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -281,7 +268,7 @@ public function finallyShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFul } /** @test */ - public function finallyShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise() + public function finallyShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -302,7 +289,7 @@ public function finallyShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfil } /** @test */ - public function finallyShouldRejectWhenHandlerThrowsForFulfilledPromise() + public function finallyShouldRejectWhenHandlerThrowsForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -323,7 +310,7 @@ public function finallyShouldRejectWhenHandlerThrowsForFulfilledPromise() } /** @test */ - public function finallyShouldRejectWhenHandlerRejectsForFulfilledPromise() + public function finallyShouldRejectWhenHandlerRejectsForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -347,7 +334,7 @@ public function finallyShouldRejectWhenHandlerRejectsForFulfilledPromise() * @test * @deprecated */ - public function otherwiseShouldNotInvokeRejectionHandlerForFulfilledPromise() + public function otherwiseShouldNotInvokeRejectionHandlerForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -359,7 +346,7 @@ public function otherwiseShouldNotInvokeRejectionHandlerForFulfilledPromise() * @test * @deprecated */ - public function alwaysShouldNotSuppressValueForFulfilledPromise() + public function alwaysShouldNotSuppressValueForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -381,7 +368,7 @@ public function alwaysShouldNotSuppressValueForFulfilledPromise() * @test * @deprecated */ - public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise() + public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -405,7 +392,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulf * @test * @deprecated */ - public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise() + public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -429,7 +416,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfill * @test * @deprecated */ - public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise() + public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -453,7 +440,7 @@ public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise() * @test * @deprecated */ - public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise() + public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise(): void { $adapter = $this->getPromiseTestAdapter(); diff --git a/tests/PromiseTest/PromisePendingTestTrait.php b/tests/PromiseTest/PromisePendingTestTrait.php index e7ed2881..a5268794 100644 --- a/tests/PromiseTest/PromisePendingTestTrait.php +++ b/tests/PromiseTest/PromisePendingTestTrait.php @@ -7,13 +7,10 @@ trait PromisePendingTestTrait { - /** - * @return PromiseAdapterInterface - */ - abstract public function getPromiseTestAdapter(callable $canceller = null); + abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface; /** @test */ - public function thenShouldReturnAPromiseForPendingPromise() + public function thenShouldReturnAPromiseForPendingPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -21,7 +18,7 @@ public function thenShouldReturnAPromiseForPendingPromise() } /** @test */ - public function thenShouldReturnAllowNullForPendingPromise() + public function thenShouldReturnAllowNullForPendingPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -29,15 +26,7 @@ public function thenShouldReturnAllowNullForPendingPromise() } /** @test */ - public function cancelShouldReturnNullForPendingPromise() - { - $adapter = $this->getPromiseTestAdapter(); - - self::assertNull($adapter->promise()->cancel()); - } - - /** @test */ - public function catchShouldNotInvokeRejectionHandlerForPendingPromise() + public function catchShouldNotInvokeRejectionHandlerForPendingPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -46,7 +35,7 @@ public function catchShouldNotInvokeRejectionHandlerForPendingPromise() } /** @test */ - public function finallyShouldReturnAPromiseForPendingPromise() + public function finallyShouldReturnAPromiseForPendingPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -57,7 +46,7 @@ public function finallyShouldReturnAPromiseForPendingPromise() * @test * @deprecated */ - public function otherwiseShouldNotInvokeRejectionHandlerForPendingPromise() + public function otherwiseShouldNotInvokeRejectionHandlerForPendingPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -69,7 +58,7 @@ public function otherwiseShouldNotInvokeRejectionHandlerForPendingPromise() * @test * @deprecated */ - public function alwaysShouldReturnAPromiseForPendingPromise() + public function alwaysShouldReturnAPromiseForPendingPromise(): void { $adapter = $this->getPromiseTestAdapter(); diff --git a/tests/PromiseTest/PromiseRejectedTestTrait.php b/tests/PromiseTest/PromiseRejectedTestTrait.php index b18baef6..af8bcaf7 100644 --- a/tests/PromiseTest/PromiseRejectedTestTrait.php +++ b/tests/PromiseTest/PromiseRejectedTestTrait.php @@ -10,13 +10,10 @@ trait PromiseRejectedTestTrait { - /** - * @return PromiseAdapterInterface - */ - abstract public function getPromiseTestAdapter(callable $canceller = null); + abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface; /** @test */ - public function rejectedPromiseShouldBeImmutable() + public function rejectedPromiseShouldBeImmutable(): void { $adapter = $this->getPromiseTestAdapter(); @@ -40,7 +37,7 @@ public function rejectedPromiseShouldBeImmutable() } /** @test */ - public function rejectedPromiseShouldInvokeNewlyAddedCallback() + public function rejectedPromiseShouldInvokeNewlyAddedCallback(): void { $adapter = $this->getPromiseTestAdapter(); @@ -59,7 +56,7 @@ public function rejectedPromiseShouldInvokeNewlyAddedCallback() } /** @test */ - public function shouldForwardUndefinedRejectionValue() + public function shouldForwardUndefinedRejectionValue(): void { $adapter = $this->getPromiseTestAdapter(); @@ -87,7 +84,7 @@ function () { } /** @test */ - public function shouldSwitchFromErrbacksToCallbacksWhenErrbackDoesNotExplicitlyPropagate() + public function shouldSwitchFromErrbacksToCallbacksWhenErrbackDoesNotExplicitlyPropagate(): void { $adapter = $this->getPromiseTestAdapter(); @@ -112,7 +109,7 @@ function () { } /** @test */ - public function shouldSwitchFromErrbacksToCallbacksWhenErrbackReturnsAResolution() + public function shouldSwitchFromErrbacksToCallbacksWhenErrbackReturnsAResolution(): void { $adapter = $this->getPromiseTestAdapter(); @@ -137,7 +134,7 @@ function () { } /** @test */ - public function shouldPropagateRejectionsWhenErrbackThrows() + public function shouldPropagateRejectionsWhenErrbackThrows(): void { $adapter = $this->getPromiseTestAdapter(); @@ -168,7 +165,7 @@ public function shouldPropagateRejectionsWhenErrbackThrows() } /** @test */ - public function shouldPropagateRejectionsWhenErrbackReturnsARejection() + public function shouldPropagateRejectionsWhenErrbackReturnsARejection(): void { $adapter = $this->getPromiseTestAdapter(); @@ -195,7 +192,7 @@ function () use ($exception) { } /** @test */ - public function catchShouldInvokeRejectionHandlerForRejectedPromise() + public function catchShouldInvokeRejectionHandlerForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -212,7 +209,7 @@ public function catchShouldInvokeRejectionHandlerForRejectedPromise() } /** @test */ - public function catchShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnExceptionForRejectedPromise() + public function catchShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnExceptionForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -232,7 +229,7 @@ public function catchShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnExcept } /** @test */ - public function catchShouldInvokeRejectionHandlerIfReasonMatchesTypehintForRejectedPromise() + public function catchShouldInvokeRejectionHandlerIfReasonMatchesTypehintForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -252,7 +249,7 @@ public function catchShouldInvokeRejectionHandlerIfReasonMatchesTypehintForRejec } /** @test */ - public function catchShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchTypehintForRejectedPromise() + public function catchShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchTypehintForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -268,7 +265,7 @@ public function catchShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchTypehint } /** @test */ - public function finallyShouldNotSuppressRejectionForRejectedPromise() + public function finallyShouldNotSuppressRejectionForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -287,7 +284,7 @@ public function finallyShouldNotSuppressRejectionForRejectedPromise() } /** @test */ - public function finallyShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseForRejectedPromise() + public function finallyShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -308,7 +305,7 @@ public function finallyShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseFo } /** @test */ - public function finallyShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRejectedPromise() + public function finallyShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -329,7 +326,7 @@ public function finallyShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRe } /** @test */ - public function finallyShouldRejectWhenHandlerThrowsForRejectedPromise() + public function finallyShouldRejectWhenHandlerThrowsForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -351,7 +348,7 @@ public function finallyShouldRejectWhenHandlerThrowsForRejectedPromise() } /** @test */ - public function finallyShouldRejectWhenHandlerRejectsForRejectedPromise() + public function finallyShouldRejectWhenHandlerRejectsForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -373,17 +370,7 @@ public function finallyShouldRejectWhenHandlerRejectsForRejectedPromise() } /** @test */ - public function cancelShouldReturnNullForRejectedPromise() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->reject(new Exception()); - - self::assertNull($adapter->promise()->cancel()); - } - - /** @test */ - public function cancelShouldHaveNoEffectForRejectedPromise() + public function cancelShouldHaveNoEffectForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); @@ -396,7 +383,7 @@ public function cancelShouldHaveNoEffectForRejectedPromise() * @test * @deprecated */ - public function otherwiseShouldInvokeRejectionHandlerForRejectedPromise() + public function otherwiseShouldInvokeRejectionHandlerForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -416,7 +403,7 @@ public function otherwiseShouldInvokeRejectionHandlerForRejectedPromise() * @test * @deprecated */ - public function otherwiseShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnExceptionForRejectedPromise() + public function otherwiseShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnExceptionForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -439,7 +426,7 @@ public function otherwiseShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnEx * @test * @deprecated */ - public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForRejectedPromise() + public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -462,7 +449,7 @@ public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForR * @test * @deprecated */ - public function otherwiseShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchTypehintForRejectedPromise() + public function otherwiseShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchTypehintForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -481,7 +468,7 @@ public function otherwiseShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchType * @test * @deprecated */ - public function alwaysShouldNotSuppressRejectionForRejectedPromise() + public function alwaysShouldNotSuppressRejectionForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -503,7 +490,7 @@ public function alwaysShouldNotSuppressRejectionForRejectedPromise() * @test * @deprecated */ - public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseForRejectedPromise() + public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -527,7 +514,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseFor * @test * @deprecated */ - public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRejectedPromise() + public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -551,7 +538,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRej * @test * @deprecated */ - public function alwaysShouldRejectWhenHandlerThrowsForRejectedPromise() + public function alwaysShouldRejectWhenHandlerThrowsForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -576,7 +563,7 @@ public function alwaysShouldRejectWhenHandlerThrowsForRejectedPromise() * @test * @deprecated */ - public function alwaysShouldRejectWhenHandlerRejectsForRejectedPromise() + public function alwaysShouldRejectWhenHandlerRejectsForRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); diff --git a/tests/PromiseTest/PromiseSettledTestTrait.php b/tests/PromiseTest/PromiseSettledTestTrait.php index 5d489a59..03ded7e0 100644 --- a/tests/PromiseTest/PromiseSettledTestTrait.php +++ b/tests/PromiseTest/PromiseSettledTestTrait.php @@ -7,13 +7,10 @@ trait PromiseSettledTestTrait { - /** - * @return PromiseAdapterInterface - */ - abstract public function getPromiseTestAdapter(callable $canceller = null); + abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface; /** @test */ - public function thenShouldReturnAPromiseForSettledPromise() + public function thenShouldReturnAPromiseForSettledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -22,7 +19,7 @@ public function thenShouldReturnAPromiseForSettledPromise() } /** @test */ - public function thenShouldReturnAllowNullForSettledPromise() + public function thenShouldReturnAllowNullForSettledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -31,17 +28,7 @@ public function thenShouldReturnAllowNullForSettledPromise() } /** @test */ - public function cancelShouldReturnNullForSettledPromise() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->settle(null); - - self::assertNull($adapter->promise()->cancel()); - } - - /** @test */ - public function cancelShouldHaveNoEffectForSettledPromise() + public function cancelShouldHaveNoEffectForSettledPromise(): void { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); @@ -51,7 +38,7 @@ public function cancelShouldHaveNoEffectForSettledPromise() } /** @test */ - public function finallyShouldReturnAPromiseForSettledPromise() + public function finallyShouldReturnAPromiseForSettledPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -63,7 +50,7 @@ public function finallyShouldReturnAPromiseForSettledPromise() * @test * @deprecated */ - public function alwaysShouldReturnAPromiseForSettledPromise() + public function alwaysShouldReturnAPromiseForSettledPromise(): void { $adapter = $this->getPromiseTestAdapter(); diff --git a/tests/PromiseTest/RejectTestTrait.php b/tests/PromiseTest/RejectTestTrait.php index 7e9dbac8..ad55ca28 100644 --- a/tests/PromiseTest/RejectTestTrait.php +++ b/tests/PromiseTest/RejectTestTrait.php @@ -11,13 +11,10 @@ trait RejectTestTrait { - /** - * @return PromiseAdapterInterface - */ - abstract public function getPromiseTestAdapter(callable $canceller = null); + abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface; /** @test */ - public function rejectShouldRejectWithAnException() + public function rejectShouldRejectWithAnException(): void { $adapter = $this->getPromiseTestAdapter(); @@ -36,7 +33,7 @@ public function rejectShouldRejectWithAnException() } /** @test */ - public function rejectShouldForwardReasonWhenCallbackIsNull() + public function rejectShouldForwardReasonWhenCallbackIsNull(): void { $adapter = $this->getPromiseTestAdapter(); @@ -61,7 +58,7 @@ public function rejectShouldForwardReasonWhenCallbackIsNull() } /** @test */ - public function rejectShouldMakePromiseImmutable() + public function rejectShouldMakePromiseImmutable(): void { $adapter = $this->getPromiseTestAdapter(); @@ -91,7 +88,7 @@ public function rejectShouldMakePromiseImmutable() } /** @test */ - public function rejectShouldInvokeCatchHandler() + public function rejectShouldInvokeCatchHandler(): void { $adapter = $this->getPromiseTestAdapter(); @@ -110,7 +107,7 @@ public function rejectShouldInvokeCatchHandler() } /** @test */ - public function finallyShouldNotSuppressRejection() + public function finallyShouldNotSuppressRejection(): void { $adapter = $this->getPromiseTestAdapter(); @@ -130,7 +127,7 @@ public function finallyShouldNotSuppressRejection() } /** @test */ - public function finallyShouldNotSuppressRejectionWhenHandlerReturnsANonPromise() + public function finallyShouldNotSuppressRejectionWhenHandlerReturnsANonPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -152,7 +149,7 @@ public function finallyShouldNotSuppressRejectionWhenHandlerReturnsANonPromise() } /** @test */ - public function finallyShouldNotSuppressRejectionWhenHandlerReturnsAPromise() + public function finallyShouldNotSuppressRejectionWhenHandlerReturnsAPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -174,7 +171,7 @@ public function finallyShouldNotSuppressRejectionWhenHandlerReturnsAPromise() } /** @test */ - public function finallyShouldRejectWhenHandlerThrowsForRejection() + public function finallyShouldRejectWhenHandlerThrowsForRejection(): void { $adapter = $this->getPromiseTestAdapter(); @@ -196,7 +193,7 @@ public function finallyShouldRejectWhenHandlerThrowsForRejection() } /** @test */ - public function finallyShouldRejectWhenHandlerRejectsForRejection() + public function finallyShouldRejectWhenHandlerRejectsForRejection(): void { $adapter = $this->getPromiseTestAdapter(); diff --git a/tests/PromiseTest/ResolveTestTrait.php b/tests/PromiseTest/ResolveTestTrait.php index 98bcc295..357fdedc 100644 --- a/tests/PromiseTest/ResolveTestTrait.php +++ b/tests/PromiseTest/ResolveTestTrait.php @@ -12,13 +12,10 @@ trait ResolveTestTrait { - /** - * @return PromiseAdapterInterface - */ - abstract public function getPromiseTestAdapter(callable $canceller = null); + abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface; /** @test */ - public function resolveShouldResolve() + public function resolveShouldResolve(): void { $adapter = $this->getPromiseTestAdapter(); @@ -35,7 +32,7 @@ public function resolveShouldResolve() } /** @test */ - public function resolveShouldResolveWithPromisedValue() + public function resolveShouldResolveWithPromisedValue(): void { $adapter = $this->getPromiseTestAdapter(); @@ -52,7 +49,7 @@ public function resolveShouldResolveWithPromisedValue() } /** @test */ - public function resolveShouldRejectWhenResolvedWithRejectedPromise() + public function resolveShouldRejectWhenResolvedWithRejectedPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -71,7 +68,7 @@ public function resolveShouldRejectWhenResolvedWithRejectedPromise() } /** @test */ - public function resolveShouldForwardValueWhenCallbackIsNull() + public function resolveShouldForwardValueWhenCallbackIsNull(): void { $adapter = $this->getPromiseTestAdapter(); @@ -95,7 +92,7 @@ public function resolveShouldForwardValueWhenCallbackIsNull() } /** @test */ - public function resolveShouldMakePromiseImmutable() + public function resolveShouldMakePromiseImmutable(): void { $adapter = $this->getPromiseTestAdapter(); @@ -123,7 +120,7 @@ public function resolveShouldMakePromiseImmutable() /** * @test */ - public function resolveShouldRejectWhenResolvedWithItself() + public function resolveShouldRejectWhenResolvedWithItself(): void { $adapter = $this->getPromiseTestAdapter(); @@ -145,7 +142,7 @@ public function resolveShouldRejectWhenResolvedWithItself() /** * @test */ - public function resolveShouldRejectWhenResolvedWithAPromiseWhichFollowsItself() + public function resolveShouldRejectWhenResolvedWithAPromiseWhichFollowsItself(): void { $adapter1 = $this->getPromiseTestAdapter(); $adapter2 = $this->getPromiseTestAdapter(); @@ -170,7 +167,7 @@ public function resolveShouldRejectWhenResolvedWithAPromiseWhichFollowsItself() } /** @test */ - public function finallyShouldNotSuppressValue() + public function finallyShouldNotSuppressValue(): void { $adapter = $this->getPromiseTestAdapter(); @@ -190,7 +187,7 @@ public function finallyShouldNotSuppressValue() } /** @test */ - public function finallyShouldNotSuppressValueWhenHandlerReturnsANonPromise() + public function finallyShouldNotSuppressValueWhenHandlerReturnsANonPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -212,7 +209,7 @@ public function finallyShouldNotSuppressValueWhenHandlerReturnsANonPromise() } /** @test */ - public function finallyShouldNotSuppressValueWhenHandlerReturnsAPromise() + public function finallyShouldNotSuppressValueWhenHandlerReturnsAPromise(): void { $adapter = $this->getPromiseTestAdapter(); @@ -234,7 +231,7 @@ public function finallyShouldNotSuppressValueWhenHandlerReturnsAPromise() } /** @test */ - public function finallyShouldRejectWhenHandlerThrowsForFulfillment() + public function finallyShouldRejectWhenHandlerThrowsForFulfillment(): void { $adapter = $this->getPromiseTestAdapter(); @@ -256,7 +253,7 @@ public function finallyShouldRejectWhenHandlerThrowsForFulfillment() } /** @test */ - public function finallyShouldRejectWhenHandlerRejectsForFulfillment() + public function finallyShouldRejectWhenHandlerRejectsForFulfillment(): void { $adapter = $this->getPromiseTestAdapter(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 1de48086..108e8efb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,16 +2,17 @@ namespace React\Promise; +use PHPUnit\Framework\MockObject\MockBuilder; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase as BaseTestCase; class TestCase extends BaseTestCase { - public function expectCallableExactly($amount): callable + public function expectCallableExactly(int $amount): callable { $mock = $this->createCallableMock(); - $mock - ->expects(self::exactly($amount)) - ->method('__invoke'); + $mock->expects(self::exactly($amount))->method('__invoke'); + assert(is_callable($mock)); return $mock; } @@ -19,9 +20,8 @@ public function expectCallableExactly($amount): callable public function expectCallableOnce(): callable { $mock = $this->createCallableMock(); - $mock - ->expects(self::once()) - ->method('__invoke'); + $mock->expects(self::once())->method('__invoke'); + assert(is_callable($mock)); return $mock; } @@ -29,22 +29,25 @@ public function expectCallableOnce(): callable public function expectCallableNever(): callable { $mock = $this->createCallableMock(); - $mock - ->expects(self::never()) - ->method('__invoke'); + $mock->expects(self::never())->method('__invoke'); + assert(is_callable($mock)); return $mock; } - protected function createCallableMock() + /** @return MockObject&callable */ + protected function createCallableMock(): MockObject { $builder = $this->getMockBuilder(\stdClass::class); if (method_exists($builder, 'addMethods')) { // PHPUnit 9+ - return $builder->addMethods(['__invoke'])->getMock(); + $mock = $builder->addMethods(['__invoke'])->getMock(); } else { // legacy PHPUnit 4 - PHPUnit 9 - return $builder->setMethods(['__invoke'])->getMock(); + $mock = $builder->setMethods(['__invoke'])->getMock(); } + assert($mock instanceof MockObject && is_callable($mock)); + + return $mock; } } diff --git a/tests/fixtures/CallbackWithDNFTypehintClass.php b/tests/fixtures/CallbackWithDNFTypehintClass.php index 81857fc6..b7d773bc 100644 --- a/tests/fixtures/CallbackWithDNFTypehintClass.php +++ b/tests/fixtures/CallbackWithDNFTypehintClass.php @@ -7,9 +7,12 @@ class CallbackWithDNFTypehintClass { - #[PHP8] public function __invoke((RuntimeException&Countable)|(RuntimeException&\IteratorAggregate) $e) { } + #[PHP8] public function __invoke((RuntimeException&Countable)|(RuntimeException&\IteratorAggregate) $e): void { } /* + public function __invoke(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ - #[PHP8] public function testCallback((RuntimeException&Countable)|(RuntimeException&\IteratorAggregate) $e) { } + #[PHP8] public function testCallback((RuntimeException&Countable)|(RuntimeException&\IteratorAggregate) $e): void { } /* + public function testCallback(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ - #[PHP8] public static function testCallbackStatic((RuntimeException&Countable)|(RuntimeException&\IteratorAggregate) $e) { } + #[PHP8] public static function testCallbackStatic((RuntimeException&Countable)|(RuntimeException&\IteratorAggregate) $e): void { }/* + public static function testCallbackStatic(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ } diff --git a/tests/fixtures/CallbackWithIntersectionTypehintClass.php b/tests/fixtures/CallbackWithIntersectionTypehintClass.php index f0ee8c1f..5bdb6694 100644 --- a/tests/fixtures/CallbackWithIntersectionTypehintClass.php +++ b/tests/fixtures/CallbackWithIntersectionTypehintClass.php @@ -7,9 +7,12 @@ class CallbackWithIntersectionTypehintClass { - #[PHP8] public function __invoke(RuntimeException&Countable $e) { } + #[PHP8] public function __invoke(RuntimeException&Countable $e): void { }/* + public function __invoke(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ - #[PHP8] public function testCallback(RuntimeException&Countable $e) { } + #[PHP8] public function testCallback(RuntimeException&Countable $e): void { }/* + public function testCallback(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ - #[PHP8] public static function testCallbackStatic(RuntimeException&Countable $e) { } + #[PHP8] public static function testCallbackStatic(RuntimeException&Countable $e): void { }/* + public static function testCallbackStatic(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ } diff --git a/tests/fixtures/CallbackWithTypehintClass.php b/tests/fixtures/CallbackWithTypehintClass.php index 12dd0da4..454eb1cd 100644 --- a/tests/fixtures/CallbackWithTypehintClass.php +++ b/tests/fixtures/CallbackWithTypehintClass.php @@ -6,15 +6,15 @@ class CallbackWithTypehintClass { - public function __invoke(InvalidArgumentException $e) + public function __invoke(InvalidArgumentException $e): void { } - public function testCallback(InvalidArgumentException $e) + public function testCallback(InvalidArgumentException $e): void { } - public static function testCallbackStatic(InvalidArgumentException $e) + public static function testCallbackStatic(InvalidArgumentException $e): void { } } diff --git a/tests/fixtures/CallbackWithUnionTypehintClass.php b/tests/fixtures/CallbackWithUnionTypehintClass.php index 1d885dc1..61643b88 100644 --- a/tests/fixtures/CallbackWithUnionTypehintClass.php +++ b/tests/fixtures/CallbackWithUnionTypehintClass.php @@ -7,9 +7,12 @@ class CallbackWithUnionTypehintClass { - #[PHP8] public function __invoke(RuntimeException|InvalidArgumentException $e) { } + #[PHP8] public function __invoke(RuntimeException|InvalidArgumentException $e): void { }/* + public function __invoke(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ - #[PHP8] public function testCallback(RuntimeException|InvalidArgumentException $e) { } + #[PHP8] public function testCallback(RuntimeException|InvalidArgumentException $e): void { }/* + public function testCallback(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ - #[PHP8] public static function testCallbackStatic(RuntimeException|InvalidArgumentException $e) { } + #[PHP8] public static function testCallbackStatic(RuntimeException|InvalidArgumentException $e): void { }/* + public static function testCallbackStatic(bool $unusedOnPhp8ButRequiredToMakePhpstanWorkOnLegacyPhp = true): void { } // */ } diff --git a/tests/fixtures/CallbackWithoutTypehintClass.php b/tests/fixtures/CallbackWithoutTypehintClass.php index 618aeebe..bdf25424 100644 --- a/tests/fixtures/CallbackWithoutTypehintClass.php +++ b/tests/fixtures/CallbackWithoutTypehintClass.php @@ -4,15 +4,15 @@ class CallbackWithoutTypehintClass { - public function __invoke() + public function __invoke(): void { } - public function testCallback() + public function testCallback(): void { } - public static function testCallbackStatic() + public static function testCallbackStatic(): void { } } diff --git a/tests/fixtures/IterableException.php b/tests/fixtures/IterableException.php index 5b93f49f..7224b39a 100644 --- a/tests/fixtures/IterableException.php +++ b/tests/fixtures/IterableException.php @@ -2,6 +2,7 @@ namespace React\Promise; +/** @implements \IteratorAggregate */ class IterableException extends \RuntimeException implements \IteratorAggregate { public function getIterator(): \Traversable diff --git a/tests/fixtures/SimpleFulfilledTestThenable.php b/tests/fixtures/SimpleFulfilledTestThenable.php index b95ec81c..d9a093f7 100644 --- a/tests/fixtures/SimpleFulfilledTestThenable.php +++ b/tests/fixtures/SimpleFulfilledTestThenable.php @@ -2,22 +2,14 @@ namespace React\Promise; -use React\Promise\Internal\RejectedPromise; - class SimpleFulfilledTestThenable { - public function then(callable $onFulfilled = null, callable $onRejected = null) + public function then(callable $onFulfilled = null, callable $onRejected = null): self { - try { - if ($onFulfilled) { - $onFulfilled('foo'); - } - - return new self(); - } catch (\Throwable $exception) { - return new RejectedPromise($exception); - } catch (\Exception $exception) { - return new RejectedPromise($exception); + if ($onFulfilled) { + $onFulfilled('foo'); } + + return new self(); } } diff --git a/tests/fixtures/SimpleTestCancellable.php b/tests/fixtures/SimpleTestCancellable.php index f232a68f..ae7de2ea 100644 --- a/tests/fixtures/SimpleTestCancellable.php +++ b/tests/fixtures/SimpleTestCancellable.php @@ -4,9 +4,10 @@ class SimpleTestCancellable { + /** @var bool */ public $cancelCalled = false; - public function cancel() + public function cancel(): void { $this->cancelCalled = true; } diff --git a/tests/fixtures/SimpleTestCancellableThenable.php b/tests/fixtures/SimpleTestCancellableThenable.php index 10957690..a4e9d642 100644 --- a/tests/fixtures/SimpleTestCancellableThenable.php +++ b/tests/fixtures/SimpleTestCancellableThenable.php @@ -4,7 +4,10 @@ class SimpleTestCancellableThenable { + /** @var bool */ public $cancelCalled = false; + + /** @var ?callable */ public $onCancel; public function __construct(callable $onCancel = null) @@ -12,12 +15,12 @@ public function __construct(callable $onCancel = null) $this->onCancel = $onCancel; } - public function then(callable $onFulfilled = null, callable $onRejected = null) + public function then(callable $onFulfilled = null, callable $onRejected = null): self { return new self(); } - public function cancel() + public function cancel(): void { $this->cancelCalled = true;