From 7d78ce11feabbb715d614904b36cf388343f1fc1 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Tue, 27 Aug 2024 15:47:36 +0545 Subject: [PATCH] chore: allow php-cs-fixer major version 3 --- .github/workflows/ci.yml | 5 ++++- .gitignore | 1 + .php-cs-fixer.dist.php | 17 +++++++++++++++++ composer.json | 4 ++-- examples/curl.php | 10 ++++++---- examples/promise.php | 9 +++++++-- examples/tail.php | 4 +++- lib/Loop/Loop.php | 2 +- lib/Promise.php | 18 ++++++------------ lib/Promise/functions.php | 5 +---- lib/Version.php | 2 +- lib/coroutine.php | 13 +++++++------ tests/Event/CoroutineTest.php | 4 +--- tests/Event/Promise/FunctionsTest.php | 15 +++++++-------- tests/Event/Promise/PromiseTest.php | 15 +++++++-------- 15 files changed, 71 insertions(+), 53 deletions(-) create mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0f77d09..2844d22 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,13 +14,16 @@ jobs: matrix: php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] coverage: ['pcov'] + code-style: ['yes'] code-analysis: ['no'] include: - php-versions: '7.1' coverage: 'none' + code-style: 'yes' code-analysis: 'yes' - php-versions: '8.4' coverage: 'pcov' + code-style: 'yes' code-analysis: 'yes' steps: - name: Checkout @@ -51,7 +54,7 @@ jobs: run: composer install --no-progress --prefer-dist --optimize-autoloader - name: Code Analysis (PHP CS-Fixer) - if: matrix.code-analysis == 'yes' + if: matrix.code-style == 'yes' run: PHP_CS_FIXER_IGNORE_ENV=true php vendor/bin/php-cs-fixer fix --dry-run --diff - name: Code Analysis (PHPStan) diff --git a/.gitignore b/.gitignore index adaa449..e9573ce 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ composer.lock tests/cov tests/.phpunit.result.cache .php_cs.cache +.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..f9d4b7a --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,17 @@ +exclude('vendor') + ->in(__DIR__); + +$config = new PhpCsFixer\Config(); +$config->setRules([ + '@PSR1' => true, + '@Symfony' => true, + 'nullable_type_declaration' => [ + 'syntax' => 'question_mark', + ], + 'nullable_type_declaration_for_default_null_value' => true, +]); +$config->setFinder($finder); +return $config; \ No newline at end of file diff --git a/composer.json b/composer.json index 3c31181..0d3ec06 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ } }, "require-dev": { - "friendsofphp/php-cs-fixer": "~2.17.1", + "friendsofphp/php-cs-fixer": "~2.17.1||^3.63", "phpstan/phpstan": "^0.12", "phpunit/phpunit" : "^7.5 || ^8.5 || ^9.6" }, @@ -55,7 +55,7 @@ "phpstan analyse lib tests" ], "cs-fixer": [ - "php-cs-fixer fix" + "PHP_CS_FIXER_IGNORE_ENV=true php-cs-fixer fix" ], "phpunit": [ "phpunit --configuration tests/phpunit.xml" diff --git a/examples/curl.php b/examples/curl.php index bfec865..639b258 100644 --- a/examples/curl.php +++ b/examples/curl.php @@ -1,5 +1,7 @@ #!/usr/bin/env php -then(function ($value) { echo "Step 2\n"; + // Immediately returning a new value. return $value.' world'; }) @@ -42,6 +46,7 @@ }) ->then(function ($value) { echo "Step 4\n"; + // This is the final event handler. return $value.' you rock!'; }) diff --git a/examples/tail.php b/examples/tail.php index e1d45f3..8a5b2f7 100755 --- a/examples/tail.php +++ b/examples/tail.php @@ -1,5 +1,7 @@ #!/usr/bin/env php -timers) { // Special case when the timers array was empty. diff --git a/lib/Promise.php b/lib/Promise.php index 70921d0..66903fb 100644 --- a/lib/Promise.php +++ b/lib/Promise.php @@ -5,7 +5,6 @@ namespace Sabre\Event; use Exception; -use Throwable; /** * An implementation of the Promise pattern. @@ -30,17 +29,17 @@ class Promise /** * The asynchronous operation is pending. */ - const PENDING = 0; + public const PENDING = 0; /** * The asynchronous operation has completed, and has a result. */ - const FULFILLED = 1; + public const FULFILLED = 1; /** * The asynchronous operation has completed with an error. */ - const REJECTED = 2; + public const REJECTED = 2; /** * The current state of this promise. @@ -128,8 +127,6 @@ public function otherwise(callable $onRejected): Promise /** * Marks this promise as fulfilled and sets its return value. - * - * @param mixed $value */ public function fulfill($value = null) { @@ -146,7 +143,7 @@ public function fulfill($value = null) /** * Marks this promise as rejected, and set its rejection reason. */ - public function reject(Throwable $reason) + public function reject(\Throwable $reason) { if (self::PENDING !== $this->state) { throw new PromiseAlreadyResolvedException('This promise is already resolved, and you\'re not allowed to resolve a promise more than once'); @@ -169,7 +166,6 @@ public function reject(Throwable $reason) * one. In PHP it might be useful to call this on the last promise in a * chain. * - * @return mixed * @psalm-return TReturn */ public function wait() @@ -208,10 +204,8 @@ public function wait() * * If the promise was fulfilled, this will be the result value. If the * promise was rejected, this property hold the rejection reason. - * - * @var mixed */ - protected $value = null; + protected $value; /** * This method is used to call either an onFulfilled or onRejected callback. @@ -242,7 +236,7 @@ private function invokeCallback(Promise $subPromise, ?callable $callBack = null) // immediately fulfill the chained promise. $subPromise->fulfill($result); } - } catch (Throwable $e) { + } catch (\Throwable $e) { // If the event handler threw an exception, we need to make sure that // the chained promise is rejected as well. $subPromise->reject($e); diff --git a/lib/Promise/functions.php b/lib/Promise/functions.php index fbed634..67e80cb 100644 --- a/lib/Promise/functions.php +++ b/lib/Promise/functions.php @@ -5,7 +5,6 @@ namespace Sabre\Event\Promise; use Sabre\Event\Promise; -use Throwable; /** * This file contains a set of functions that are useful for dealing with the @@ -101,8 +100,6 @@ function ($reason) use ($fail, &$alreadyDone) { * * If the value is a promise, the returned promise will attach itself to that * promise and eventually get the same state as the followed promise. - * - * @param mixed $value */ function resolve($value): Promise { @@ -119,7 +116,7 @@ function resolve($value): Promise /** * Returns a Promise that will reject with the given reason. */ -function reject(Throwable $reason): Promise +function reject(\Throwable $reason): Promise { $promise = new Promise(); $promise->reject($reason); diff --git a/lib/Version.php b/lib/Version.php index e1bdac3..8cb9454 100644 --- a/lib/Version.php +++ b/lib/Version.php @@ -16,5 +16,5 @@ class Version /** * Full version number. */ - const VERSION = '5.1.6'; + public const VERSION = '5.1.6'; } diff --git a/lib/coroutine.php b/lib/coroutine.php index fa4cdf2..f664efa 100644 --- a/lib/coroutine.php +++ b/lib/coroutine.php @@ -5,7 +5,6 @@ namespace Sabre\Event; use Generator; -use Throwable; /** * Turn asynchronous promise-based code into something that looks synchronous @@ -43,7 +42,9 @@ * }); * * @psalm-template TReturn + * * @psalm-param callable():\Generator $gen + * * @psalm-return Promise * * @copyright Copyright (C) fruux GmbH (https://fruux.com/) @@ -53,7 +54,7 @@ function coroutine(callable $gen): Promise { $generator = $gen(); - if (!$generator instanceof Generator) { + if (!$generator instanceof \Generator) { throw new \InvalidArgumentException('You must pass a generator function'); } @@ -73,11 +74,11 @@ function ($value) use ($generator, &$advanceGenerator) { $generator->send($value); $advanceGenerator(); }, - function (Throwable $reason) use ($generator, $advanceGenerator) { + function (\Throwable $reason) use ($generator, $advanceGenerator) { $generator->throw($reason); $advanceGenerator(); } - )->otherwise(function (Throwable $reason) use ($promise) { + )->otherwise(function (\Throwable $reason) use ($promise) { // This error handler would be called, if something in the // generator throws an exception, and it's not caught // locally. @@ -102,7 +103,7 @@ function (Throwable $reason) use ($generator, $advanceGenerator) { if ($returnValue instanceof Promise) { $returnValue->then(function ($value) use ($promise) { $promise->fulfill($value); - }, function (Throwable $reason) use ($promise) { + }, function (\Throwable $reason) use ($promise) { $promise->reject($reason); }); } else { @@ -113,7 +114,7 @@ function (Throwable $reason) use ($generator, $advanceGenerator) { try { $advanceGenerator(); - } catch (Throwable $e) { + } catch (\Throwable $e) { $promise->reject($e); } diff --git a/tests/Event/CoroutineTest.php b/tests/Event/CoroutineTest.php index 54b6e02..fe217ff 100644 --- a/tests/Event/CoroutineTest.php +++ b/tests/Event/CoroutineTest.php @@ -4,8 +4,6 @@ namespace Sabre\Event; -use Exception; - class CoroutineTest extends \PHPUnit\Framework\TestCase { public function testNonGenerator() @@ -46,7 +44,7 @@ public function testRejectedPromise() { $start = 0; $promise = new Promise(function ($fulfill, $reject) { - $reject(new Exception('2')); + $reject(new \Exception('2')); }); coroutine(function () use (&$start, $promise) { diff --git a/tests/Event/Promise/FunctionsTest.php b/tests/Event/Promise/FunctionsTest.php index 34e0b77..5cb90e2 100644 --- a/tests/Event/Promise/FunctionsTest.php +++ b/tests/Event/Promise/FunctionsTest.php @@ -4,7 +4,6 @@ namespace Sabre\Event\Promise; -use Exception; use Sabre\Event\Loop; use Sabre\Event\Promise; @@ -53,10 +52,10 @@ function ($value) use (&$finalValue) { } ); - $promise1->reject(new Exception('1')); + $promise1->reject(new \Exception('1')); Loop\run(); $this->assertEquals('1', $finalValue->getMessage()); - $promise2->reject(new Exception('2')); + $promise2->reject(new \Exception('2')); Loop\run(); $this->assertEquals(1, $finalValue->getMessage()); } @@ -78,10 +77,10 @@ function ($value) use (&$finalValue) { } ); - $promise1->reject(new Exception('1')); + $promise1->reject(new \Exception('1')); Loop\run(); $this->assertEquals(1, $finalValue->getMessage()); - $promise2->fulfill(new Exception('2')); + $promise2->fulfill(new \Exception('2')); Loop\run(); $this->assertEquals(1, $finalValue->getMessage()); } @@ -124,10 +123,10 @@ function ($value) use (&$finalValue) { } ); - $promise1->reject(new Exception('1')); + $promise1->reject(new \Exception('1')); Loop\run(); $this->assertEquals(1, $finalValue->getMessage()); - $promise2->reject(new Exception('2')); + $promise2->reject(new \Exception('2')); Loop\run(); $this->assertEquals(1, $finalValue->getMessage()); } @@ -162,7 +161,7 @@ public function testReject() { $finalValue = 0; - $promise = reject(new Exception('1')); + $promise = reject(new \Exception('1')); $promise->then(function ($value) use (&$finalValue) { $finalValue = 'im broken'; }, function ($reason) use (&$finalValue) { diff --git a/tests/Event/Promise/PromiseTest.php b/tests/Event/Promise/PromiseTest.php index 6b2835d..c86fa4f 100644 --- a/tests/Event/Promise/PromiseTest.php +++ b/tests/Event/Promise/PromiseTest.php @@ -4,7 +4,6 @@ namespace Sabre\Event\Promise; -use Exception; use Sabre\Event\Loop; use Sabre\Event\Promise; use Sabre\Event\PromiseAlreadyResolvedException; @@ -29,7 +28,7 @@ public function testFail() { $finalValue = 0; $promise = new Promise(); - $promise->reject(new Exception('1')); + $promise->reject(new \Exception('1')); $promise->then(null, function ($value) use (&$finalValue) { $finalValue = $value->getMessage() + 2; @@ -105,7 +104,7 @@ public function testPendingFail() $finalValue = $value->getMessage() + 2; }); - $promise->reject(new Exception('4')); + $promise->reject(new \Exception('4')); Loop\run(); $this->assertEquals(6, $finalValue); @@ -126,7 +125,7 @@ public function testExecutorSuccess() public function testExecutorFail() { $promise = (new Promise(function ($success, $fail) { - $fail(new Exception('hi')); + $fail(new \Exception('hi')); }))->then(function ($result) use (&$realResult) { $realResult = 'incorrect'; })->otherwise(function ($reason) use (&$realResult) { @@ -149,8 +148,8 @@ public function testRejectTwice() { $this->expectException(PromiseAlreadyResolvedException::class); $promise = new Promise(); - $promise->reject(new Exception('1')); - $promise->reject(new Exception('1')); + $promise->reject(new \Exception('1')); + $promise->reject(new \Exception('1')); } public function testFromFailureHandler() @@ -167,7 +166,7 @@ public function testFromFailureHandler() }); $this->assertEquals(0, $ok); - $promise->reject(new Exception('foo')); + $promise->reject(new \Exception('foo')); Loop\run(); $this->assertEquals(1, $ok); @@ -211,7 +210,7 @@ public function testWaitRejectedScalar() { $promise = new Promise(); Loop\nextTick(function () use ($promise) { - $promise->reject(new Exception('foo')); + $promise->reject(new \Exception('foo')); }); try { $promise->wait();