From fed07f34f7ef0e82b406ded4faf970449dbc0e21 Mon Sep 17 00:00:00 2001 From: Marko Ivancic Date: Tue, 14 Jan 2025 13:17:16 +0100 Subject: [PATCH] Apply initial rector suggestions --- composer.json | 6 +-- rector.php | 17 ++++++++ src/Algorithms/SignatureAlgorithmBag.php | 4 +- src/Decorators/HttpClientDecorator.php | 4 +- src/Factories/HttpClientDecoratorFactory.php | 2 +- src/Federation/TrustChainBag.php | 7 ++-- src/Jws/ParsedJws.php | 2 +- src/Serializers/JwsSerializerBag.php | 4 +- .../src/Federation/TrustChainResolverTest.php | 42 ++++++++----------- tests/src/Jws/ParsedJwsTest.php | 2 +- 10 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 6916315..53e40c0 100644 --- a/composer.json +++ b/composer.json @@ -33,10 +33,10 @@ }, "require-dev": { "phpunit/phpunit": "^10", + "rector/rector": "^2.0", + "simplesamlphp/simplesamlphp-test-framework": "^1", "squizlabs/php_codesniffer": "^3", - "vimeo/psalm": "^5", - "rector/rector": "^1 || ^2", - "simplesamlphp/simplesamlphp-test-framework": "^1" + "vimeo/psalm": "^5" }, "config": { "sort-packages": true, diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..8a4ffc1 --- /dev/null +++ b/rector.php @@ -0,0 +1,17 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + // uncomment to reach your current PHP version + ->withPhpSets() + ->withTypeCoverageLevel(0) + ->withDeadCodeLevel(0) + ->withCodeQualityLevel(0) + ; diff --git a/src/Algorithms/SignatureAlgorithmBag.php b/src/Algorithms/SignatureAlgorithmBag.php index ee4a692..e61a5c2 100644 --- a/src/Algorithms/SignatureAlgorithmBag.php +++ b/src/Algorithms/SignatureAlgorithmBag.php @@ -33,9 +33,7 @@ public function getAll(): array public function getAllInstances(): array { return array_map( - function (SignatureAlgorithmEnum $signatureAlgorithmEnum) { - return $signatureAlgorithmEnum->instance(); - }, + fn(SignatureAlgorithmEnum $signatureAlgorithmEnum) => $signatureAlgorithmEnum->instance(), $this->getAll(), ); } diff --git a/src/Decorators/HttpClientDecorator.php b/src/Decorators/HttpClientDecorator.php index ef8fc97..046b1ba 100644 --- a/src/Decorators/HttpClientDecorator.php +++ b/src/Decorators/HttpClientDecorator.php @@ -14,11 +14,9 @@ class HttpClientDecorator { public const DEFAULT_HTTP_CLIENT_CONFIG = [RequestOptions::ALLOW_REDIRECTS => true,]; - public readonly Client $client; - public function __construct(?Client $client = null) + public function __construct(public readonly Client $client = new Client(self::DEFAULT_HTTP_CLIENT_CONFIG)) { - $this->client = $client ?? new Client(self::DEFAULT_HTTP_CLIENT_CONFIG); } /** diff --git a/src/Factories/HttpClientDecoratorFactory.php b/src/Factories/HttpClientDecoratorFactory.php index 9eea140..b47e7c0 100644 --- a/src/Factories/HttpClientDecoratorFactory.php +++ b/src/Factories/HttpClientDecoratorFactory.php @@ -11,6 +11,6 @@ class HttpClientDecoratorFactory { public function build(?Client $client = null): HttpClientDecorator { - return new HttpClientDecorator($client); + return is_null($client) ? new HttpClientDecorator() : new HttpClientDecorator($client); } } diff --git a/src/Federation/TrustChainBag.php b/src/Federation/TrustChainBag.php index b823d04..a5b30b7 100644 --- a/src/Federation/TrustChainBag.php +++ b/src/Federation/TrustChainBag.php @@ -22,9 +22,10 @@ public function add(TrustChain $trustChain, TrustChain ...$trustChains): void $this->trustChains = array_merge($this->trustChains, $trustChains); // Order the chains from shortest to longest one. - usort($this->trustChains, function (TrustChain $a, TrustChain $b) { - return $a->getResolvedLength() <=> $b->getResolvedLength(); - }); + usort( + $this->trustChains, + fn(TrustChain $a, TrustChain $b) => $a->getResolvedLength() <=> $b->getResolvedLength(), + ); } /** diff --git a/src/Jws/ParsedJws.php b/src/Jws/ParsedJws.php index 979351c..240b278 100644 --- a/src/Jws/ParsedJws.php +++ b/src/Jws/ParsedJws.php @@ -53,7 +53,7 @@ protected function validateByCallbacks(callable ...$calls): void try { call_user_func($call); } catch (Throwable $exception) { - $errors[] = sprintf('%s: %s', get_class($exception), $exception->getMessage()); + $errors[] = sprintf('%s: %s', $exception::class, $exception->getMessage()); } } diff --git a/src/Serializers/JwsSerializerBag.php b/src/Serializers/JwsSerializerBag.php index d9ed919..7ca8264 100644 --- a/src/Serializers/JwsSerializerBag.php +++ b/src/Serializers/JwsSerializerBag.php @@ -35,9 +35,7 @@ public function getAll(): array public function getAllInstances(): array { return array_map( - function (JwsSerializerEnum $jwsSerializerEnum) { - return $jwsSerializerEnum->instance(); - }, + fn(JwsSerializerEnum $jwsSerializerEnum) => $jwsSerializerEnum->instance(), $this->getAll(), ); } diff --git a/tests/src/Federation/TrustChainResolverTest.php b/tests/src/Federation/TrustChainResolverTest.php index 59fee27..3368bc2 100644 --- a/tests/src/Federation/TrustChainResolverTest.php +++ b/tests/src/Federation/TrustChainResolverTest.php @@ -97,9 +97,10 @@ public function testCanGetConfigurationChains(): void $this->entityStatementFetcherMock ->expects($this->exactly(3)) ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback( + fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.') + ); $this->leafEntityConfigurationMock ->expects($this->once()) @@ -136,9 +137,8 @@ public function testCanLimitMaximumConfigurationChainDepth(): void $this->entityStatementFetcherMock ->expects($this->exactly(2)) ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback(fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.')); $this->leafEntityConfigurationMock ->method('getAuthorityHints') @@ -162,9 +162,8 @@ public function testCanDetectLoopInConfigurationChains(): void { $this->entityStatementFetcherMock ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback(fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.')); $this->leafEntityConfigurationMock ->method('getAuthorityHints') @@ -192,9 +191,8 @@ public function testCanBailOnMaxAuthorityHintsRule(): void $this->entityStatementFetcherMock ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback(fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.')); $this->loggerMock ->expects($this->atLeastOnce()) @@ -212,9 +210,8 @@ public function testCanResolveTrustChain(): void { $this->entityStatementFetcherMock ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback(fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.')); $this->leafEntityConfigurationMock ->expects($this->once()) @@ -238,9 +235,8 @@ public function testCanResolveMultipleTrustChains(): void { $this->entityStatementFetcherMock ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback(fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.')); $this->leafEntityConfigurationMock ->expects($this->once()) @@ -299,9 +295,8 @@ public function testCanWarnOnTrustChainResolutionSubordinateStatementFetchError( { $this->entityStatementFetcherMock ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback(fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.')); $this->entityStatementFetcherMock ->method('fromCacheOrFetchEndpoint') @@ -329,9 +324,8 @@ public function testTrustChainResolveThrowsOnTrustChainBagFactoryError(): void { $this->entityStatementFetcherMock ->method('fromCacheOrWellKnownEndpoint') - ->willReturnCallback(function (string $entityId) { - return $this->configChainSample[$entityId] ?? throw new \Exception('No entity.'); - }); + ->willReturnCallback(fn(string $entityId) => + $this->configChainSample[$entityId] ?? throw new \Exception('No entity.')); $this->leafEntityConfigurationMock ->expects($this->once()) diff --git a/tests/src/Jws/ParsedJwsTest.php b/tests/src/Jws/ParsedJwsTest.php index b52d425..a0d9836 100644 --- a/tests/src/Jws/ParsedJwsTest.php +++ b/tests/src/Jws/ParsedJwsTest.php @@ -187,7 +187,7 @@ protected function validate(): void $this->validateByCallbacks($this->simulateError(...)); } - protected function simulateError(): void + protected function simulateError(): never { throw new \Exception('Error'); }