From 3e78ae0f711249a2d3bdda09720961b528f916ab Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 31 Mar 2024 10:55:42 +0100 Subject: [PATCH] Fixes for PHP 8.4 deprecation --- .php-cs-fixer.dist.php | 1 + src/Coroutine.php | 4 ++-- src/Each.php | 10 +++++----- src/FulfilledPromise.php | 4 ++-- src/Promise.php | 8 ++++---- src/PromiseInterface.php | 4 ++-- src/RejectedPromise.php | 4 ++-- src/RejectionException.php | 2 +- src/Utils.php | 2 +- tests/CoroutineTest.php | 11 +++++------ tests/NotPromiseInstance.php | 4 ++-- tests/PromiseTest.php | 8 ++++---- tests/Thennable.php | 2 +- tests/UtilsTest.php | 6 +++--- vendor-bin/php-cs-fixer/composer.json | 2 +- 15 files changed, 36 insertions(+), 36 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index b267c7f..77b0684 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -5,6 +5,7 @@ ->setRules([ '@PHP71Migration:risky' => true, '@PHPUnit75Migration:risky' => true, + '@PSR12:risky' => true, '@Symfony' => true, 'global_namespace_import' => false, 'no_superfluous_phpdoc_tags' => [ diff --git a/src/Coroutine.php b/src/Coroutine.php index 0b5b9c0..0da0228 100644 --- a/src/Coroutine.php +++ b/src/Coroutine.php @@ -84,8 +84,8 @@ public static function of(callable $generatorFn): self } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { return $this->result->then($onFulfilled, $onRejected); } diff --git a/src/Each.php b/src/Each.php index c09d23c..dd72c83 100644 --- a/src/Each.php +++ b/src/Each.php @@ -23,8 +23,8 @@ final class Each */ public static function of( $iterable, - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { return (new EachPromise($iterable, [ 'fulfilled' => $onFulfilled, @@ -46,8 +46,8 @@ public static function of( public static function ofLimit( $iterable, $concurrency, - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { return (new EachPromise($iterable, [ 'fulfilled' => $onFulfilled, @@ -67,7 +67,7 @@ public static function ofLimit( public static function ofLimitAll( $iterable, $concurrency, - callable $onFulfilled = null + ?callable $onFulfilled = null ): PromiseInterface { return self::ofLimit( $iterable, diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index ab71296..727ec31 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -31,8 +31,8 @@ public function __construct($value) } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { // Return itself if there is no onFulfilled function. if (!$onFulfilled) { diff --git a/src/Promise.php b/src/Promise.php index 1b07bdc..c0c5be2 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -25,16 +25,16 @@ class Promise implements PromiseInterface * @param callable $cancelFn Fn that when invoked cancels the promise. */ public function __construct( - callable $waitFn = null, - callable $cancelFn = null + ?callable $waitFn = null, + ?callable $cancelFn = null ) { $this->waitFn = $waitFn; $this->cancelFn = $cancelFn; } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { if ($this->state === self::PENDING) { $p = new Promise(null, [$this, 'cancel']); diff --git a/src/PromiseInterface.php b/src/PromiseInterface.php index 2824802..c11721e 100644 --- a/src/PromiseInterface.php +++ b/src/PromiseInterface.php @@ -27,8 +27,8 @@ interface PromiseInterface * @param callable $onRejected Invoked when the promise is rejected. */ public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface; /** diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index d947da1..1ebf0b2 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -31,8 +31,8 @@ public function __construct($reason) } public function then( - callable $onFulfilled = null, - callable $onRejected = null + ?callable $onFulfilled = null, + ?callable $onRejected = null ): PromiseInterface { // If there's no onRejected callback then just return self. if (!$onRejected) { diff --git a/src/RejectionException.php b/src/RejectionException.php index 72a81ba..47dca86 100644 --- a/src/RejectionException.php +++ b/src/RejectionException.php @@ -18,7 +18,7 @@ class RejectionException extends \RuntimeException * @param mixed $reason Rejection reason. * @param string|null $description Optional description. */ - public function __construct($reason, string $description = null) + public function __construct($reason, ?string $description = null) { $this->reason = $reason; diff --git a/src/Utils.php b/src/Utils.php index e1570d7..45b0893 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -21,7 +21,7 @@ final class Utils * * @param TaskQueueInterface|null $assign Optionally specify a new queue instance. */ - public static function queue(TaskQueueInterface $assign = null): TaskQueueInterface + public static function queue(?TaskQueueInterface $assign = null): TaskQueueInterface { static $queue; diff --git a/tests/CoroutineTest.php b/tests/CoroutineTest.php index f046e15..af60639 100644 --- a/tests/CoroutineTest.php +++ b/tests/CoroutineTest.php @@ -4,7 +4,6 @@ namespace GuzzleHttp\Promise\Tests; -use GuzzleHttp\Promise as P; use GuzzleHttp\Promise\Coroutine; use GuzzleHttp\Promise\Promise; use GuzzleHttp\Promise\PromiseInterface; @@ -16,7 +15,7 @@ class CoroutineTest extends TestCase public function testReturnsCoroutine(): void { $fn = function () { yield 'foo'; }; - $this->assertInstanceOf(P\Coroutine::class, P\Coroutine::of($fn)); + $this->assertInstanceOf(Coroutine::class, Coroutine::of($fn)); } /** @@ -77,7 +76,7 @@ public function testShouldCancelResultPromiseAndOutsideCurrentPromise(): void public function testWaitShouldResolveChainedCoroutines(): void { $promisor = function () { - return P\Coroutine::of(function () { + return Coroutine::of(function () { yield $promise = new Promise(function () use (&$promise): void { $promise->resolve(1); }); @@ -91,19 +90,19 @@ public function testWaitShouldResolveChainedCoroutines(): void public function testWaitShouldHandleIntermediateErrors(): void { - $promise = P\Coroutine::of(function () { + $promise = Coroutine::of(function () { yield $promise = new Promise(function () use (&$promise): void { $promise->resolve(1); }); }) ->then(function () { - return P\Coroutine::of(function () { + return Coroutine::of(function () { yield $promise = new Promise(function () use (&$promise): void { $promise->reject(new \Exception()); }); }); }) - ->otherwise(function (\Exception $error = null) { + ->otherwise(function (?\Exception $error = null) { if (!$error) { self::fail('Error did not propagate.'); } diff --git a/tests/NotPromiseInstance.php b/tests/NotPromiseInstance.php index dc6c2be..e4c9d34 100644 --- a/tests/NotPromiseInstance.php +++ b/tests/NotPromiseInstance.php @@ -16,7 +16,7 @@ public function __construct() $this->nextPromise = new Promise(); } - public function then(callable $res = null, callable $rej = null): PromiseInterface + public function then(?callable $res = null, ?callable $rej = null): PromiseInterface { return $this->nextPromise->then($res, $rej); } @@ -36,7 +36,7 @@ public function reject($reason): void $this->nextPromise->reject($reason); } - public function wait(bool $unwrap = true, bool $defaultResolution = null): void + public function wait(bool $unwrap = true, ?bool $defaultResolution = null): void { } diff --git a/tests/PromiseTest.php b/tests/PromiseTest.php index 255bafa..52ba353 100644 --- a/tests/PromiseTest.php +++ b/tests/PromiseTest.php @@ -75,7 +75,7 @@ public function testInvokesWaitFunction(): void public function testRejectsAndThrowsWhenWaitFailsToResolve(): void { - $this->expectException(\GuzzleHttp\Promise\RejectionException::class); + $this->expectException(RejectionException::class); $this->expectExceptionMessage('The promise was rejected with reason: Invoking the wait callback did not resolve the promise'); $p = new Promise(function (): void {}); @@ -84,7 +84,7 @@ public function testRejectsAndThrowsWhenWaitFailsToResolve(): void public function testThrowsWhenUnwrapIsRejectedWithNonException(): void { - $this->expectException(\GuzzleHttp\Promise\RejectionException::class); + $this->expectException(RejectionException::class); $this->expectExceptionMessage('The promise was rejected with reason: foo'); $p = new Promise(function () use (&$p): void { @@ -145,7 +145,7 @@ public function testWaitsOnNestedPromises(): void public function testThrowsWhenWaitingOnPromiseWithNoWaitFunction(): void { - $this->expectException(\GuzzleHttp\Promise\RejectionException::class); + $this->expectException(RejectionException::class); $p = new Promise(); $p->wait(); @@ -219,7 +219,7 @@ public function testCannotCancelNonPending(): void public function testCancelsPromiseWhenNoCancelFunction(): void { - $this->expectException(\GuzzleHttp\Promise\CancellationException::class); + $this->expectException(CancellationException::class); $p = new Promise(); $p->cancel(); diff --git a/tests/Thennable.php b/tests/Thennable.php index 2484148..fb1e496 100644 --- a/tests/Thennable.php +++ b/tests/Thennable.php @@ -15,7 +15,7 @@ public function __construct() $this->nextPromise = new Promise(); } - public function then(callable $res = null, callable $rej = null) + public function then(?callable $res = null, ?callable $rej = null) { return $this->nextPromise->then($res, $rej); } diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 511cb4c..526a3e7 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -32,7 +32,7 @@ public function testWaitsOnAllPromisesIntoArray(): void public function testUnwrapsPromisesWithNoDefaultAndFailure(): void { - $this->expectException(\GuzzleHttp\Promise\RejectionException::class); + $this->expectException(RejectionException::class); $promises = [new FulfilledPromise('a'), new Promise()]; P\Utils::unwrap($promises); @@ -151,7 +151,7 @@ public function testCanWaitUntilSomeCountIsSatisfied(): void public function testThrowsIfImpossibleToWaitForSomeCount(): void { - $this->expectException(\GuzzleHttp\Promise\AggregateException::class); + $this->expectException(AggregateException::class); $this->expectExceptionMessage('Not enough promises to fulfill count'); $a = new Promise(function () use (&$a): void { $a->resolve('a'); }); @@ -161,7 +161,7 @@ public function testThrowsIfImpossibleToWaitForSomeCount(): void public function testThrowsIfResolvedWithoutCountTotalResults(): void { - $this->expectException(\GuzzleHttp\Promise\AggregateException::class); + $this->expectException(AggregateException::class); $this->expectExceptionMessage('Not enough promises to fulfill count'); $a = new Promise(); diff --git a/vendor-bin/php-cs-fixer/composer.json b/vendor-bin/php-cs-fixer/composer.json index 3e8c9df..049eacf 100644 --- a/vendor-bin/php-cs-fixer/composer.json +++ b/vendor-bin/php-cs-fixer/composer.json @@ -1,7 +1,7 @@ { "require": { "php": "^7.4 || ^8.0", - "friendsofphp/php-cs-fixer": "3.40.2" + "friendsofphp/php-cs-fixer": "3.52.1" }, "config": { "preferred-install": "dist"