Skip to content

Commit 11ae2fa

Browse files
committed
use constructor property promotion
1 parent 3c16176 commit 11ae2fa

File tree

7 files changed

+28
-49
lines changed

7 files changed

+28
-49
lines changed

Command/UserPasswordHashCommand.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,10 @@
3838
#[AsCommand(name: 'security:hash-password', description: 'Hash a user password')]
3939
class UserPasswordHashCommand extends Command
4040
{
41-
private PasswordHasherFactoryInterface $hasherFactory;
42-
private array $userClasses;
43-
44-
public function __construct(PasswordHasherFactoryInterface $hasherFactory, array $userClasses = [])
45-
{
46-
$this->hasherFactory = $hasherFactory;
47-
$this->userClasses = $userClasses;
48-
41+
public function __construct(
42+
private PasswordHasherFactoryInterface $hasherFactory,
43+
private array $userClasses = [],
44+
) {
4945
parent::__construct();
5046
}
5147

Hasher/MessageDigestPasswordHasher.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,23 @@ class MessageDigestPasswordHasher implements LegacyPasswordHasherInterface
2424
{
2525
use CheckPasswordLengthTrait;
2626

27-
private string $algorithm;
28-
private bool $encodeHashAsBase64;
29-
private int $iterations = 1;
3027
private int $hashLength = -1;
3128

3229
/**
3330
* @param string $algorithm The digest algorithm to use
3431
* @param bool $encodeHashAsBase64 Whether to base64 encode the password hash
3532
* @param int $iterations The number of iterations to use to stretch the password hash
3633
*/
37-
public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase64 = true, int $iterations = 5000)
38-
{
39-
$this->algorithm = $algorithm;
40-
$this->encodeHashAsBase64 = $encodeHashAsBase64;
41-
34+
public function __construct(
35+
private string $algorithm = 'sha512',
36+
private bool $encodeHashAsBase64 = true,
37+
private int $iterations = 5000,
38+
) {
4239
try {
4340
$this->hashLength = \strlen($this->hash('', 'salt'));
4441
} catch (\LogicException) {
4542
// ignore algorithm not supported
4643
}
47-
48-
$this->iterations = $iterations;
4944
}
5045

5146
public function hash(#[\SensitiveParameter] string $plainPassword, ?string $salt = null): string

Hasher/MigratingPasswordHasher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
*/
2525
final class MigratingPasswordHasher implements PasswordHasherInterface
2626
{
27-
private PasswordHasherInterface $bestHasher;
2827
private array $extraHashers;
2928

30-
public function __construct(PasswordHasherInterface $bestHasher, PasswordHasherInterface ...$extraHashers)
31-
{
32-
$this->bestHasher = $bestHasher;
29+
public function __construct(
30+
private PasswordHasherInterface $bestHasher,
31+
PasswordHasherInterface ...$extraHashers,
32+
) {
3333
$this->extraHashers = $extraHashers;
3434
}
3535

Hasher/PasswordHasherFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
*/
2424
class PasswordHasherFactory implements PasswordHasherFactoryInterface
2525
{
26-
private array $passwordHashers;
27-
2826
/**
2927
* @param array<string, PasswordHasherInterface|array> $passwordHashers
3028
*/
31-
public function __construct(array $passwordHashers)
32-
{
33-
$this->passwordHashers = $passwordHashers;
29+
public function __construct(
30+
private array $passwordHashers,
31+
) {
3432
}
3533

3634
public function getPasswordHasher(string|PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user): PasswordHasherInterface

Hasher/Pbkdf2PasswordHasher.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ final class Pbkdf2PasswordHasher implements LegacyPasswordHasherInterface
3232
{
3333
use CheckPasswordLengthTrait;
3434

35-
private string $algorithm;
36-
private bool $encodeHashAsBase64;
37-
private int $iterations = 1;
38-
private int $length;
3935
private int $encodedLength = -1;
4036

4137
/**
@@ -44,19 +40,17 @@ final class Pbkdf2PasswordHasher implements LegacyPasswordHasherInterface
4440
* @param int $iterations The number of iterations to use to stretch the password hash
4541
* @param int $length Length of derived key to create
4642
*/
47-
public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase64 = true, int $iterations = 1000, int $length = 40)
48-
{
49-
$this->algorithm = $algorithm;
50-
$this->encodeHashAsBase64 = $encodeHashAsBase64;
51-
$this->length = $length;
52-
43+
public function __construct(
44+
private string $algorithm = 'sha512',
45+
private bool $encodeHashAsBase64 = true,
46+
private int $iterations = 1000,
47+
private int $length = 40,
48+
) {
5349
try {
5450
$this->encodedLength = \strlen($this->hash('', 'salt'));
5551
} catch (\LogicException) {
5652
// ignore unsupported algorithm
5753
}
58-
59-
$this->iterations = $iterations;
6054
}
6155

6256
public function hash(#[\SensitiveParameter] string $plainPassword, ?string $salt = null): string

Hasher/PlaintextPasswordHasher.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ class PlaintextPasswordHasher implements LegacyPasswordHasherInterface
2525
{
2626
use CheckPasswordLengthTrait;
2727

28-
private bool $ignorePasswordCase;
29-
3028
/**
3129
* @param bool $ignorePasswordCase Compare password case-insensitive
3230
*/
33-
public function __construct(bool $ignorePasswordCase = false)
34-
{
35-
$this->ignorePasswordCase = $ignorePasswordCase;
31+
public function __construct(
32+
private bool $ignorePasswordCase = false,
33+
) {
3634
}
3735

3836
public function hash(#[\SensitiveParameter] string $plainPassword, ?string $salt = null): string

Hasher/UserPasswordHasher.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
*/
2424
class UserPasswordHasher implements UserPasswordHasherInterface
2525
{
26-
private PasswordHasherFactoryInterface $hasherFactory;
27-
28-
public function __construct(PasswordHasherFactoryInterface $hasherFactory)
29-
{
30-
$this->hasherFactory = $hasherFactory;
26+
public function __construct(
27+
private PasswordHasherFactoryInterface $hasherFactory,
28+
) {
3129
}
3230

3331
public function hashPassword(PasswordAuthenticatedUserInterface $user, #[\SensitiveParameter] string $plainPassword): string

0 commit comments

Comments
 (0)