Skip to content

Commit 776a559

Browse files
authored
minor #292 use constructor property promotion where possible
1 parent 36f261d commit 776a559

7 files changed

+34
-106
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,17 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Property SymfonyCasts\\\\Bundle\\\\ResetPassword\\\\Command\\\\ResetPasswordRemoveExpiredCommand\\:\\:\\$cleaner has no type specified\\.$#"
5-
count: 1
6-
path: src/Command/ResetPasswordRemoveExpiredCommand.php
7-
83
-
94
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\:\\:integerNode\\(\\)\\.$#"
105
count: 1
116
path: src/DependencyInjection/Configuration.php
127

13-
-
14-
message: "#^Property SymfonyCasts\\\\Bundle\\\\ResetPassword\\\\Exception\\\\TooManyPasswordRequestsException\\:\\:\\$availableAt has no type specified\\.$#"
15-
count: 1
16-
path: src/Exception/TooManyPasswordRequestsException.php
17-
188
-
199
message: "#^Method SymfonyCasts\\\\Bundle\\\\ResetPassword\\\\Model\\\\ResetPasswordToken\\:\\:getExpirationMessageData\\(\\) return type has no value type specified in iterable type array\\.$#"
2010
count: 1
2111
path: src/Model/ResetPasswordToken.php
2212

23-
-
24-
message: "#^Property SymfonyCasts\\\\Bundle\\\\ResetPassword\\\\ResetPasswordHelper\\:\\:\\$repository has no type specified\\.$#"
25-
count: 1
26-
path: src/ResetPasswordHelper.php
27-
28-
-
29-
message: "#^Property SymfonyCasts\\\\Bundle\\\\ResetPassword\\\\ResetPasswordHelper\\:\\:\\$resetPasswordCleaner has no type specified\\.$#"
30-
count: 1
31-
path: src/ResetPasswordHelper.php
32-
33-
-
34-
message: "#^Property SymfonyCasts\\\\Bundle\\\\ResetPassword\\\\ResetPasswordHelper\\:\\:\\$tokenGenerator has no type specified\\.$#"
35-
count: 1
36-
path: src/ResetPasswordHelper.php
37-
3813
-
3914
message: "#^PHPDoc tag @param references unknown parameter\\: \\$resetRequestLifetime$#"
4015
count: 1
4116
path: src/ResetPasswordHelperInterface.php
4217

43-
-
44-
message: "#^Property SymfonyCasts\\\\Bundle\\\\ResetPassword\\\\Util\\\\ResetPasswordCleaner\\:\\:\\$repository has no type specified\\.$#"
45-
count: 1
46-
path: src/Util/ResetPasswordCleaner.php

src/Command/ResetPasswordRemoveExpiredCommand.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
*/
2121
final class ResetPasswordRemoveExpiredCommand extends Command
2222
{
23-
private $cleaner;
24-
25-
public function __construct(ResetPasswordCleaner $cleaner)
26-
{
27-
$this->cleaner = $cleaner;
28-
23+
public function __construct(
24+
private ResetPasswordCleaner $cleaner
25+
) {
2926
parent::__construct('reset-password:remove-expired');
3027
}
3128

src/Exception/TooManyPasswordRequestsException.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@
1414
*/
1515
final class TooManyPasswordRequestsException extends \Exception implements ResetPasswordExceptionInterface
1616
{
17-
private $availableAt;
18-
19-
public function __construct(\DateTimeInterface $availableAt, string $message = '', int $code = 0, ?\Throwable $previous = null)
17+
public function __construct(private \DateTimeInterface $availableAt, string $message = '', int $code = 0, ?\Throwable $previous = null)
2018
{
2119
parent::__construct($message, $code, $previous);
22-
23-
$this->availableAt = $availableAt;
2420
}
2521

2622
public function getAvailableAt(): \DateTimeInterface

src/Generator/ResetPasswordTokenGenerator.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,12 @@
2222
class ResetPasswordTokenGenerator
2323
{
2424
/**
25-
* @var string Unique, random, cryptographically secure string
25+
* @param string $signingKey Unique, random, cryptographically secure string
2626
*/
27-
private $signingKey;
28-
29-
/**
30-
* @var ResetPasswordRandomGenerator
31-
*/
32-
private $randomGenerator;
33-
34-
public function __construct(string $signingKey, ResetPasswordRandomGenerator $generator)
35-
{
36-
$this->signingKey = $signingKey;
37-
$this->randomGenerator = $generator;
27+
public function __construct(
28+
private string $signingKey,
29+
private ResetPasswordRandomGenerator $generator
30+
) {
3831
}
3932

4033
/**
@@ -46,10 +39,10 @@ public function __construct(string $signingKey, ResetPasswordRandomGenerator $ge
4639
public function createToken(\DateTimeInterface $expiresAt, $userId, ?string $verifier = null): ResetPasswordTokenComponents
4740
{
4841
if (null === $verifier) {
49-
$verifier = $this->randomGenerator->getRandomAlphaNumStr();
42+
$verifier = $this->generator->getRandomAlphaNumStr();
5043
}
5144

52-
$selector = $this->randomGenerator->getRandomAlphaNumStr();
45+
$selector = $this->generator->getRandomAlphaNumStr();
5346

5447
$encodedData = json_encode([$verifier, $userId, $expiresAt->getTimestamp()]);
5548

src/Model/ResetPasswordTokenComponents.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,11 @@
1919
*/
2020
class ResetPasswordTokenComponents
2121
{
22-
/**
23-
* @var string
24-
*/
25-
private $selector;
26-
27-
/**
28-
* @var string
29-
*/
30-
private $verifier;
31-
32-
/**
33-
* @var string
34-
*/
35-
private $hashedToken;
36-
37-
public function __construct(string $selector, string $verifier, string $hashedToken)
38-
{
39-
$this->selector = $selector;
40-
$this->verifier = $verifier;
41-
$this->hashedToken = $hashedToken;
22+
public function __construct(
23+
private string $selector,
24+
private string $verifier,
25+
private string $hashedToken
26+
) {
4227
}
4328

4429
/**

src/ResetPasswordHelper.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,17 @@ final class ResetPasswordHelper implements ResetPasswordHelperInterface
2929
*/
3030
private const SELECTOR_LENGTH = 20;
3131

32-
private $tokenGenerator;
33-
private $resetPasswordCleaner;
34-
private $repository;
35-
36-
/**
37-
* @var int How long a token is valid in seconds
38-
*/
39-
private $resetRequestLifetime;
40-
4132
/**
42-
* @var int Another password reset cannot be made faster than this throttle time in seconds
33+
* @param int $resetRequestLifetime How long a token is valid in seconds
34+
* @param int $requestThrottleTime Another password reset cannot be made faster than this throttle time in seconds
4335
*/
44-
private $requestThrottleTime;
45-
46-
public function __construct(ResetPasswordTokenGenerator $generator, ResetPasswordCleaner $cleaner, ResetPasswordRequestRepositoryInterface $repository, int $resetRequestLifetime, int $requestThrottleTime)
47-
{
48-
$this->tokenGenerator = $generator;
49-
$this->resetPasswordCleaner = $cleaner;
50-
$this->repository = $repository;
51-
$this->resetRequestLifetime = $resetRequestLifetime;
52-
$this->requestThrottleTime = $requestThrottleTime;
36+
public function __construct(
37+
private ResetPasswordTokenGenerator $generator,
38+
private ResetPasswordCleaner $cleaner,
39+
private ResetPasswordRequestRepositoryInterface $repository,
40+
private int $resetRequestLifetime,
41+
private int $requestThrottleTime,
42+
) {
5343
}
5444

5545
/**
@@ -62,7 +52,7 @@ public function __construct(ResetPasswordTokenGenerator $generator, ResetPasswor
6252
*/
6353
public function generateResetToken(object $user, ?int $resetRequestLifetime = null): ResetPasswordToken
6454
{
65-
$this->resetPasswordCleaner->handleGarbageCollection();
55+
$this->cleaner->handleGarbageCollection();
6656

6757
if ($availableAt = $this->hasUserHitThrottling($user)) {
6858
throw new TooManyPasswordRequestsException($availableAt);
@@ -74,7 +64,7 @@ public function generateResetToken(object $user, ?int $resetRequestLifetime = nu
7464

7565
$generatedAt = ($expiresAt->getTimestamp() - $resetRequestLifetime);
7666

77-
$tokenComponents = $this->tokenGenerator->createToken($expiresAt, $this->repository->getUserIdentifier($user));
67+
$tokenComponents = $this->generator->createToken($expiresAt, $this->repository->getUserIdentifier($user));
7868

7969
$passwordResetRequest = $this->repository->createResetPasswordRequest(
8070
$user,
@@ -99,7 +89,7 @@ public function generateResetToken(object $user, ?int $resetRequestLifetime = nu
9989
*/
10090
public function validateTokenAndFetchUser(string $fullToken): object
10191
{
102-
$this->resetPasswordCleaner->handleGarbageCollection();
92+
$this->cleaner->handleGarbageCollection();
10393

10494
if (40 !== \strlen($fullToken)) {
10595
throw new InvalidResetPasswordTokenException();
@@ -117,7 +107,7 @@ public function validateTokenAndFetchUser(string $fullToken): object
117107

118108
$user = $resetRequest->getUser();
119109

120-
$hashedVerifierToken = $this->tokenGenerator->createToken(
110+
$hashedVerifierToken = $this->generator->createToken(
121111
$resetRequest->getExpiresAt(),
122112
$this->repository->getUserIdentifier($user),
123113
substr($fullToken, self::SELECTOR_LENGTH)

src/Util/ResetPasswordCleaner.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@
2222
class ResetPasswordCleaner
2323
{
2424
/**
25-
* @var bool Enable/disable garbage collection
25+
* @param bool $enabled Enable/disable garbage collection
2626
*/
27-
private $enabled;
28-
29-
private $repository;
30-
31-
public function __construct(ResetPasswordRequestRepositoryInterface $repository, bool $enabled = true)
32-
{
33-
$this->repository = $repository;
34-
$this->enabled = $enabled;
27+
public function __construct(
28+
private ResetPasswordRequestRepositoryInterface $repository,
29+
private bool $enabled = true,
30+
) {
3531
}
3632

3733
/**

0 commit comments

Comments
 (0)