Skip to content

Commit 2fed59e

Browse files
authored
Merge pull request #32 from facile-it/add-rector
Add Rector
2 parents 586b706 + 941f6c9 commit 2fed59e

34 files changed

Lines changed: 164 additions & 287 deletions

.github/workflows/continuous-integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
sarif_file: psalm.sarif
4545

4646
codestyle:
47-
name: "Code Style"
47+
name: "Rector + Code Style"
4848
runs-on: ubuntu-latest
4949

5050
strategy:
@@ -71,7 +71,7 @@ jobs:
7171
- uses: ramsey/composer-install@v4
7272

7373
- name: "Run PHPCS check"
74-
run: "composer cs-check"
74+
run: "composer rector-check && composer cs-check"
7575

7676
phpunit:
7777
name: "PHPUnit"

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,21 @@
6464
"friendsofphp/php-cs-fixer": "^3.92.3",
6565
"phpspec/prophecy-phpunit": "^2.0.1",
6666
"phpunit/phpunit": "^10.5.60 || ^11.5.46 || ^12.0.0",
67+
"rector/rector": "2.3.9",
6768
"spomky-labs/aes-key-wrap": "^7.0",
6869
"vimeo/psalm": "^6.14.3"
6970
},
7071
"scripts": {
72+
"rector-check": "rector --dry-run",
73+
"rector-fix": "rector",
7174
"cs-check": "php-cs-fixer fix --dry-run --diff --allow-risky=yes",
7275
"cs-fix": "php-cs-fixer fix --diff --allow-risky=yes",
7376
"psalm": "psalm",
7477
"test": "phpunit",
7578
"test-coverage": "phpunit --coverage-text",
7679
"check": [
77-
"@cs-check",
80+
"@rector-check",
81+
"@cs-fix",
7882
"@psalm",
7983
"@test"
8084
]

rector.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withPaths([
9+
__DIR__ . '/src',
10+
__DIR__ . '/tests',
11+
])
12+
// uncomment to reach your current PHP version
13+
->withPhpSets()
14+
->withComposerBased(
15+
phpunit: true,
16+
)
17+
->withPreparedSets(
18+
deadCode: true,
19+
codeQuality: true,
20+
typeDeclarations: true
21+
)
22+
;

src/AbstractTokenVerifier.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,6 @@
3838
*/
3939
abstract class AbstractTokenVerifier implements TokenVerifierInterface
4040
{
41-
protected string $issuer;
42-
43-
protected string $clientId;
44-
45-
protected ?string $clientSecret;
46-
47-
protected ?string $expectedAzp;
48-
49-
protected ?string $expectedAlg;
50-
51-
protected int $clockTolerance;
52-
53-
protected bool $authTimeRequired;
54-
55-
protected bool $aadIssValidation;
56-
5741
protected JwksProviderInterface $jwksProvider;
5842

5943
protected TokenDecrypterInterface $decrypter;
@@ -70,26 +54,18 @@ abstract class AbstractTokenVerifier implements TokenVerifierInterface
7054
* @psalm-internal \Facile\JoseVerifier
7155
*/
7256
final public function __construct(
73-
string $issuer,
74-
string $clientId,
75-
?string $clientSecret = null,
76-
bool $authTimeRequired = false,
77-
int $clockTolerance = 0,
78-
bool $aadIssValidation = false,
79-
?string $expectedAzp = null,
80-
?string $expectedAlg = null,
57+
protected string $issuer,
58+
protected string $clientId,
59+
protected ?string $clientSecret = null,
60+
protected bool $authTimeRequired = false,
61+
protected int $clockTolerance = 0,
62+
protected bool $aadIssValidation = false,
63+
protected ?string $expectedAzp = null,
64+
protected ?string $expectedAlg = null,
8165
?JwksProviderInterface $jwksProvider = null,
8266
?TokenDecrypterInterface $decrypter = null,
8367
?ClockInterface $clock = null,
8468
) {
85-
$this->issuer = $issuer;
86-
$this->clientId = $clientId;
87-
$this->clientSecret = $clientSecret;
88-
$this->authTimeRequired = $authTimeRequired;
89-
$this->clockTolerance = $clockTolerance;
90-
$this->aadIssValidation = $aadIssValidation;
91-
$this->expectedAzp = $expectedAzp;
92-
$this->expectedAlg = $expectedAlg;
9369
$this->jwksProvider = $jwksProvider ?? new MemoryJwksProvider();
9470
$this->decrypter = $decrypter ?? new NullTokenDecrypter();
9571
$this->clock = $clock ?? new InternalClock();
@@ -238,12 +214,12 @@ private function getJWKFromKid(string $kid): JWK
238214
$jwks = JWKSet::createFromKeyData($this->jwksProvider->getJwks());
239215
$jwk = $jwks->selectKey('sig', null, ['kid' => $kid]);
240216

241-
if (null === $jwk) {
217+
if (! $jwk instanceof JWK) {
242218
$jwks = JWKSet::createFromKeyData($this->jwksProvider->reload()->getJwks());
243219
$jwk = $jwks->selectKey('sig', null, ['kid' => $kid]);
244220
}
245221

246-
if (null === $jwk) {
222+
if (! $jwk instanceof JWK) {
247223
throw new InvalidTokenException('Unable to find the jwk with the provided kid: ' . $kid);
248224
}
249225

src/Builder/AbstractTokenVerifierBuilder.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@
2828
*/
2929
abstract class AbstractTokenVerifierBuilder implements TokenVerifierBuilderInterface
3030
{
31-
/**
32-
* @var array<string, mixed>
33-
*
34-
* @psalm-var ClientMetadataType
35-
*/
36-
protected array $clientMetadata;
37-
38-
/**
39-
* @var array<string, mixed>
40-
*
41-
* @psalm-var IssuerMetadataType
42-
*/
43-
protected array $issuerMetadata;
44-
4531
protected int $clockTolerance = 0;
4632

4733
protected bool $aadIssValidation = false;
@@ -55,14 +41,16 @@ abstract class AbstractTokenVerifierBuilder implements TokenVerifierBuilderInter
5541
protected ?JwksProviderBuilder $jwksProviderBuilder = null;
5642

5743
/**
44+
* @param array<string, mixed> $issuerMetadata
45+
* @param array<string, mixed> $clientMetadata
46+
*
5847
* @psalm-param IssuerMetadataType $issuerMetadata
5948
* @psalm-param ClientMetadataType $clientMetadata
6049
*/
61-
protected function __construct(array $issuerMetadata, array $clientMetadata)
62-
{
63-
$this->issuerMetadata = $issuerMetadata;
64-
$this->clientMetadata = $clientMetadata;
65-
}
50+
protected function __construct(
51+
protected array $issuerMetadata,
52+
protected array $clientMetadata,
53+
) {}
6654

6755
public function withClockTolerance(int $clockTolerance): static
6856
{
@@ -111,11 +99,7 @@ public function withClientJwksProvider(JwksProviderInterface $clientJwksProvider
11199

112100
protected function getJwksProvider(): JwksProviderInterface
113101
{
114-
if ($this->jwksProvider) {
115-
return $this->jwksProvider;
116-
}
117-
118-
return $this->jwksProvider = $this->buildJwksProvider();
102+
return $this->jwksProvider ??= $this->buildJwksProvider();
119103
}
120104

121105
/**
@@ -136,11 +120,7 @@ protected function buildJwksProvider(): JwksProviderInterface
136120

137121
protected function getClientJwksProvider(): JwksProviderInterface
138122
{
139-
if ($this->clientJwksProvider) {
140-
return $this->clientJwksProvider;
141-
}
142-
143-
return $this->clientJwksProvider = $this->buildClientJwksProvider();
123+
return $this->clientJwksProvider ??= $this->buildClientJwksProvider();
144124
}
145125

146126
protected function buildClientJwksProvider(): JwksProviderInterface

src/Decrypter/TokenDecrypter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct()
7979
if (class_exists($algorithmClass)) {
8080
try {
8181
$this->algorithms[] = new $algorithmClass();
82-
} catch (Throwable $throwable) {
82+
} catch (Throwable) {
8383
// does nothing
8484
}
8585
}
@@ -158,7 +158,7 @@ protected function getAlgorithmMap(): array
158158
KeyEncryption\A128GCMKW::class,
159159
KeyEncryption\A192GCMKW::class,
160160
KeyEncryption\A256GCMKW::class,
161-
...(class_exists('AESKW\Wrapper') ? [
161+
...(class_exists(\AESKW\Wrapper::class) ? [
162162
KeyEncryption\A128KW::class,
163163
KeyEncryption\A192KW::class,
164164
KeyEncryption\A256KW::class,

src/Exception/InvalidTokenClaimException.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@
1111
*/
1212
class InvalidTokenClaimException extends RuntimeException implements InvalidTokenExceptionInterface
1313
{
14-
private string $claim;
15-
16-
/** @var mixed */
17-
private $value;
18-
1914
/**
2015
* @param mixed $value
2116
*/
22-
public function __construct(string $message, string $claim, $value, ?Throwable $previous = null)
23-
{
17+
public function __construct(
18+
string $message,
19+
private readonly string $claim,
20+
private $value,
21+
?Throwable $previous = null,
22+
) {
2423
parent::__construct($message, 0, $previous);
25-
$this->claim = $claim;
26-
$this->value = $value;
2724
}
2825

2926
public function getClaim(): string

src/Internal/Checker/AbstractHashChecker.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,20 @@
1919
*/
2020
abstract class AbstractHashChecker implements ClaimChecker
2121
{
22-
private string $valueToCheck;
23-
24-
private string $alg;
25-
26-
/**
27-
* SHashChecker constructor.
28-
*/
29-
public function __construct(string $valueToCheck, string $alg)
30-
{
31-
$this->valueToCheck = $valueToCheck;
32-
$this->alg = $alg;
33-
}
22+
public function __construct(
23+
private readonly string $valueToCheck,
24+
private readonly string $alg,
25+
) {}
3426

3527
private function getShaSize(string $alg): string
3628
{
3729
$size = substr($alg, -3);
3830

39-
switch ($size) {
40-
case '512':
41-
return 'sha512';
42-
case '384':
43-
return 'sha384';
44-
default:
45-
return 'sha256';
46-
}
31+
return match ($size) {
32+
'512' => 'sha512',
33+
'384' => 'sha384',
34+
default => 'sha256',
35+
};
4736
}
4837

4938
/**

src/Internal/Checker/AuthTimeChecker.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@
1414
/**
1515
* @internal
1616
*/
17-
final class AuthTimeChecker implements ClaimChecker
17+
final readonly class AuthTimeChecker implements ClaimChecker
1818
{
1919
private const CLAIM_NAME = 'auth_time';
2020

21-
private int $maxAge;
22-
23-
private int $allowedTimeDrift;
24-
25-
private ClockInterface $clock;
26-
27-
public function __construct(int $maxAge, int $allowedTimeDrift = 0, ?ClockInterface $clock = null)
28-
{
29-
$this->maxAge = $maxAge;
30-
$this->allowedTimeDrift = $allowedTimeDrift;
31-
$this->clock = $clock ?? new InternalClock();
32-
}
21+
public function __construct(
22+
private int $maxAge,
23+
private int $allowedTimeDrift = 0,
24+
private ClockInterface $clock = new InternalClock(),
25+
) {}
3326

3427
/**
3528
* @throws InvalidClaimException

src/Internal/Checker/AzpChecker.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212
/**
1313
* @internal
1414
*/
15-
final class AzpChecker implements ClaimChecker
15+
final readonly class AzpChecker implements ClaimChecker
1616
{
1717
private const CLAIM_NAME = 'azp';
1818

19-
private string $azp;
20-
21-
public function __construct(string $azp)
22-
{
23-
$this->azp = $azp;
24-
}
19+
public function __construct(
20+
private string $azp,
21+
) {}
2522

2623
/**
2724
* @param mixed $value

0 commit comments

Comments
 (0)