Skip to content

Commit 1800e2e

Browse files
authored
Leverage latest PHP capabilities (#63)
1 parent 206f593 commit 1800e2e

31 files changed

Lines changed: 222 additions & 639 deletions

doc/5-events.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,23 @@ class LogSecurityTokenErrors implements EventSubscriberInterface
6969
{
7070
$this->logger->warning(
7171
'Security token was not found',
72-
['purpose' => $event->getPurpose(), 'value' => $event->getValue()]
72+
['purpose' => $event->purpose, 'value' => $event->value]
7373
);
7474
}
7575

7676
public function onTokenExpired(SecurityTokenEvents\TokenExpiredEvent $event): void
7777
{
7878
$this->logger->warning(
7979
'Security token was expired',
80-
['purpose' => $event->getPurpose(), 'value' => $event->getValue()]
80+
['purpose' => $event->purpose, 'value' => $event->value]
8181
);
8282
}
8383

8484
public function onTokenConsumed(SecurityTokenEvents\TokenAlreadyConsumedEvent $event): void
8585
{
8686
$this->logger->warning(
8787
'Security token was already consumed',
88-
['purpose' => $event->getPurpose(), 'value' => $event->getValue()]
88+
['purpose' => $event->purpose, 'value' => $event->value]
8989
);
9090
}
9191
}

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\Token\:\:\$id \(int\|null\) is never assigned int so it can be removed from the property type\.$#'
5-
identifier: property.unusedType
6-
count: 1
7-
path: src/Entity/Token.php
8-
93
-
104
message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\Token\:\:\$id is never written, only read\.$#'
115
identifier: property.onlyRead
126
count: 1
137
path: src/Entity/Token.php
148

15-
-
16-
message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\TokenUsage\:\:\$id \(int\|null\) is never assigned int so it can be removed from the property type\.$#'
17-
identifier: property.unusedType
18-
count: 1
19-
path: src/Entity/TokenUsage.php
20-
219
-
2210
message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\TokenUsage\:\:\$id is never written, only read\.$#'
2311
identifier: property.onlyRead

src/Archive/ArchivistInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface ArchivistInterface
1616
*
1717
* @param string|null $purpose The token purpose
1818
*
19-
* @return integer
19+
* @return int Count archived tokens
2020
*/
2121
public function archive(string|null $purpose = null): int;
2222
}

src/Archive/DeleteArchivist.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@
1515
*/
1616
final class DeleteArchivist implements ArchivistInterface
1717
{
18-
/**
19-
* @var EntityRepository<Token>
20-
*/
21-
private $tokenRepository;
22-
23-
/**
24-
* @param EntityRepository<Token> $tokenRepository The token entity repository
25-
*/
26-
public function __construct(EntityRepository $tokenRepository)
27-
{
28-
$this->tokenRepository = $tokenRepository;
18+
public function __construct(
19+
/**
20+
* @var EntityRepository<Token> The token entity repository
21+
*/
22+
private readonly EntityRepository $tokenRepository,
23+
) {
2924
}
3025

3126
public function archive(string|null $purpose = null): int

src/Command/ArchiveTokenCommand.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515
*/
1616
final class ArchiveTokenCommand extends Command
1717
{
18-
/**
19-
* @var ArchivistInterface
20-
*/
21-
private $archivist;
22-
23-
public function __construct(ArchivistInterface $archivist)
24-
{
25-
$this->archivist = $archivist;
18+
public function __construct(
19+
private readonly ArchivistInterface $archivist,
20+
) {
2621
parent::__construct();
2722
}
2823

@@ -45,6 +40,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4540
\sprintf('<info>Successfully archived <comment>%d</comment> security token(s).</info>', $count),
4641
);
4742

48-
return 0;
43+
return self::SUCCESS;
4944
}
5045
}

src/Configuration/TokenConfiguration.php

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,79 +11,13 @@
1111
*/
1212
final class TokenConfiguration
1313
{
14-
/**
15-
* @var string
16-
*/
17-
private $purpose;
18-
19-
/**
20-
* @var TokenGeneratorInterface
21-
*/
22-
private $generator;
23-
24-
/**
25-
* @var string
26-
*/
27-
private $duration;
28-
29-
/**
30-
* @var int
31-
*/
32-
private $usages;
33-
34-
/**
35-
* @var string
36-
*/
37-
private $keep;
38-
39-
/**
40-
* @var bool
41-
*/
42-
private $unique;
43-
4414
public function __construct(
45-
string $purpose,
46-
TokenGeneratorInterface $generator,
47-
string $duration,
48-
int $usages,
49-
string $keep,
50-
bool $unique,
15+
public readonly string $purpose,
16+
public readonly TokenGeneratorInterface $generator,
17+
public readonly string $duration,
18+
public readonly int $usages,
19+
public readonly string $keep,
20+
public readonly bool $unique,
5121
) {
52-
$this->purpose = $purpose;
53-
$this->generator = $generator;
54-
$this->duration = $duration;
55-
$this->usages = $usages;
56-
$this->keep = $keep;
57-
$this->unique = $unique;
58-
}
59-
60-
public function getPurpose(): string
61-
{
62-
return $this->purpose;
63-
}
64-
65-
public function getGenerator(): TokenGeneratorInterface
66-
{
67-
return $this->generator;
68-
}
69-
70-
public function getDuration(): string
71-
{
72-
return $this->duration;
73-
}
74-
75-
public function getUsages(): int
76-
{
77-
return $this->usages;
78-
}
79-
80-
public function getKeep(): string
81-
{
82-
return $this->keep;
83-
}
84-
85-
public function isUnique(): bool
86-
{
87-
return $this->unique;
8822
}
8923
}

src/Configuration/TokenConfigurationRegistry.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
final class TokenConfigurationRegistry
1313
{
1414
/**
15-
* @var array<TokenConfiguration>
15+
* @var array<string, TokenConfiguration>
1616
*/
17-
private $configurations;
17+
private readonly array $configurations;
1818

1919
/**
20-
* @param array<TokenConfiguration> $configurations
20+
* @param list<TokenConfiguration> $configurations
2121
*/
2222
public function __construct(array $configurations)
2323
{
24-
$this->configurations = [];
24+
$indexedConfigurations = [];
2525
foreach ($configurations as $configuration) {
26-
$this->configurations[$configuration->getPurpose()] = $configuration;
26+
$indexedConfigurations[$configuration->purpose] = $configuration;
2727
}
28+
$this->configurations = $indexedConfigurations;
2829
}
2930

3031
/**
@@ -36,12 +37,9 @@ public function __construct(array $configurations)
3637
*/
3738
public function get(string $purpose): TokenConfiguration
3839
{
39-
if (!isset($this->configurations[$purpose])) {
40-
throw new BadMethodCallException(
40+
return $this->configurations[$purpose]
41+
?? throw new BadMethodCallException(
4142
\sprintf('There is no configured security token on "%s" purpose.', $purpose),
4243
);
43-
}
44-
45-
return $this->configurations[$purpose];
4644
}
4745
}

src/Entity/Token.php

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,41 @@
1414
*/
1515
class Token
1616
{
17-
/**
18-
* @var int|null
19-
*/
20-
private $id;
17+
private int|null $id;
2118

2219
/**
2320
* @var class-string
2421
*/
25-
private $userClass;
22+
private string $userClass;
2623

27-
/**
28-
* @var string
29-
*/
30-
private $userId;
24+
private string $userId;
3125

32-
/**
33-
* @var string
34-
*/
35-
private $value;
26+
private string $value;
3627

37-
/**
38-
* @var string
39-
*/
40-
private $purpose;
28+
private string $purpose;
4129

4230
/**
4331
* @var array<string, mixed>
4432
*/
45-
private $payload = [];
33+
private array $payload;
4634

47-
/**
48-
* @var DateTime
49-
*/
50-
private $createdAt;
35+
private DateTime $createdAt;
5136

5237
/**
5338
* @var array<string, mixed>
5439
*/
55-
private $createdInformation = [];
40+
private array $createdInformation;
5641

57-
/**
58-
* @var integer
59-
*/
60-
private $allowedUsages;
42+
private int $allowedUsages;
6143

62-
/**
63-
* @var DateTime
64-
*/
65-
private $expiresAt;
44+
private DateTime $expiresAt;
6645

67-
/**
68-
* @var DateTime
69-
*/
70-
private $keepUntil;
46+
private DateTime $keepUntil;
7147

7248
/**
7349
* @var Collection<int, TokenUsage>
7450
*/
75-
private $usages;
51+
private Collection $usages;
7652

7753
/**
7854
* @param class-string $userClass
@@ -188,7 +164,7 @@ public function getCountUsages(): int
188164
}
189165

190166
/**
191-
* @return array<TokenUsage>
167+
* @return array<int, TokenUsage>
192168
*/
193169
public function getUsages(): array
194170
{

src/Entity/TokenUsage.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,16 @@
88

99
class TokenUsage
1010
{
11-
/**
12-
* @var int|null
13-
*/
14-
private $id;
11+
private int|null $id;
1512

16-
/**
17-
* @var Token
18-
*/
19-
private $token;
13+
private Token $token;
2014

21-
/**
22-
* @var DateTime
23-
*/
24-
private $createdAt;
15+
private DateTime $createdAt;
2516

2617
/**
2718
* @var array<string, mixed>
2819
*/
29-
private $information = [];
20+
private array $information = [];
3021

3122
/**
3223
* @param array<string, mixed> $information

0 commit comments

Comments
 (0)