Skip to content

Commit daebae8

Browse files
authored
feature #307 [token] add types and constructor property promotion
1 parent c5102c8 commit daebae8

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

UPGRADING.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ trait has been dropped - attribute mapping is required.
6767
no longer potentially throw a `LogicException`. They now throw a `ResetPasswordRuntimeException`
6868
if an invalid `$generatedAt` timestamp is provided to the class constructor.
6969

70+
- Passing `null` for the `$generatedAt` argument when instantiating a new token object
71+
is no longer allowed. The argument is also now mandatory.
72+
73+
```diff
74+
- public function __construct(string $token, \DateTimeInterface $expiresAt, ?int $generatedAt = null)
75+
+ public function __construct(string $token, \DateTimeInterface $expiresAt, int $generatedAt)
76+
```
77+
78+
- Property types were added to `token`, `expiresAt`, `generatedAt`.
79+
80+
```diff
81+
- private $token;
82+
+ private ?string $token;
83+
84+
- private $expiresAt;
85+
+ private \DateTimeInterface $expiresAt;
86+
87+
- private $generatedAt;
88+
+ private int $generatedAt;
89+
```
90+
91+
_Note: When calling `ResetPasswordToken::clearToken()`, the value of `$token` is set to `null`. It is not possible to
92+
instantiate a token object with a `null` `$token` value. This is intentional._
93+
7094
## ResetPasswordTokenGenerator
7195

7296
- Type added for `createToken()`'s `$userId` argument

src/Model/ResetPasswordToken.php

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,23 @@
1717
*/
1818
final class ResetPasswordToken
1919
{
20-
/**
21-
* @var string|null selector + non-hashed verifier token
22-
*/
23-
private $token;
24-
25-
/**
26-
* @var \DateTimeInterface
27-
*/
28-
private $expiresAt;
20+
private ?string $token;
2921

3022
/**
31-
* @var int|null timestamp when the token was created
23+
* @var int expiresAt translator interval
3224
*/
33-
private $generatedAt;
25+
private int $transInterval = 0;
3426

3527
/**
36-
* @var int expiresAt translator interval
28+
* @param string $token selector + non-hashed verifier token
29+
* @param int $generatedAt timestamp when the token was created
3730
*/
38-
private $transInterval = 0;
39-
40-
public function __construct(string $token, \DateTimeInterface $expiresAt, ?int $generatedAt = null)
41-
{
31+
public function __construct(
32+
string $token,
33+
private \DateTimeInterface $expiresAt,
34+
private int $generatedAt
35+
) {
4236
$this->token = $token;
43-
$this->expiresAt = $expiresAt;
44-
$this->generatedAt = $generatedAt;
45-
46-
if (null === $generatedAt) {
47-
$this->triggerDeprecation();
48-
}
4937
}
5038

5139
/**
@@ -138,10 +126,6 @@ public function getExpirationMessageData(): array
138126
*/
139127
public function getExpiresAtIntervalInstance(): \DateInterval
140128
{
141-
if (null === $this->generatedAt) {
142-
throw new \LogicException(sprintf('%s initialized without setting the $generatedAt timestamp.', self::class));
143-
}
144-
145129
$createdAtTime = \DateTimeImmutable::createFromFormat('U', (string) $this->generatedAt);
146130

147131
if (false === $createdAtTime) {
@@ -150,17 +134,4 @@ public function getExpiresAtIntervalInstance(): \DateInterval
150134

151135
return $this->expiresAt->diff($createdAtTime);
152136
}
153-
154-
/**
155-
* @psalm-suppress UndefinedFunction
156-
*/
157-
private function triggerDeprecation(): void
158-
{
159-
trigger_deprecation(
160-
'symfonycasts/reset-password-bundle',
161-
'1.2',
162-
'Initializing the %s without setting the "$generatedAt" constructor argument is deprecated. The default "null" will be removed in the future.',
163-
self::class
164-
);
165-
}
166137
}

0 commit comments

Comments
 (0)