Skip to content

Commit

Permalink
minor #53966 [OptionsResolver][PasswordHasher][PropertyAccess][Semaph…
Browse files Browse the repository at this point in the history
…ore][Stopwatch][Yaml] use constructor property promotion (xabbuh)

This PR was merged into the 7.1 branch.

Discussion
----------

[OptionsResolver][PasswordHasher][PropertyAccess][Semaphore][Stopwatch][Yaml] use constructor property promotion

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

Commits
-------

c4e4843 use constructor property promotion
  • Loading branch information
fabpot committed Feb 21, 2024
2 parents 86b07f0 + c4e4843 commit 8d352bc
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 119 deletions.
11 changes: 4 additions & 7 deletions src/Symfony/Component/OptionsResolver/OptionConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@

final class OptionConfigurator
{
private string $name;
private OptionsResolver $resolver;

public function __construct(string $name, OptionsResolver $resolver)
{
$this->name = $name;
$this->resolver = $resolver;
public function __construct(
private string $name,
private OptionsResolver $resolver,
) {
$this->resolver->setDefined($name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@
#[AsCommand(name: 'security:hash-password', description: 'Hash a user password')]
class UserPasswordHashCommand extends Command
{
private PasswordHasherFactoryInterface $hasherFactory;
private array $userClasses;

public function __construct(PasswordHasherFactoryInterface $hasherFactory, array $userClasses = [])
{
$this->hasherFactory = $hasherFactory;
$this->userClasses = $userClasses;

public function __construct(
private PasswordHasherFactoryInterface $hasherFactory,
private array $userClasses = [],
) {
parent::__construct();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,23 @@ class MessageDigestPasswordHasher implements LegacyPasswordHasherInterface
{
use CheckPasswordLengthTrait;

private string $algorithm;
private bool $encodeHashAsBase64;
private int $iterations = 1;
private int $hashLength = -1;

/**
* @param string $algorithm The digest algorithm to use
* @param bool $encodeHashAsBase64 Whether to base64 encode the password hash
* @param int $iterations The number of iterations to use to stretch the password hash
*/
public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase64 = true, int $iterations = 5000)
{
$this->algorithm = $algorithm;
$this->encodeHashAsBase64 = $encodeHashAsBase64;

public function __construct(
private string $algorithm = 'sha512',
private bool $encodeHashAsBase64 = true,
private int $iterations = 5000,
) {
try {
$this->hashLength = \strlen($this->hash('', 'salt'));
} catch (\LogicException) {
// ignore algorithm not supported
}

$this->iterations = $iterations;
}

public function hash(#[\SensitiveParameter] string $plainPassword, ?string $salt = null): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
*/
final class MigratingPasswordHasher implements PasswordHasherInterface
{
private PasswordHasherInterface $bestHasher;
private array $extraHashers;

public function __construct(PasswordHasherInterface $bestHasher, PasswordHasherInterface ...$extraHashers)
{
$this->bestHasher = $bestHasher;
public function __construct(
private PasswordHasherInterface $bestHasher,
PasswordHasherInterface ...$extraHashers,
) {
$this->extraHashers = $extraHashers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
*/
class PasswordHasherFactory implements PasswordHasherFactoryInterface
{
private array $passwordHashers;

/**
* @param array<string, PasswordHasherInterface|array> $passwordHashers
*/
public function __construct(array $passwordHashers)
{
$this->passwordHashers = $passwordHashers;
public function __construct(
private array $passwordHashers,
) {
}

public function getPasswordHasher(string|PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user): PasswordHasherInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ final class Pbkdf2PasswordHasher implements LegacyPasswordHasherInterface
{
use CheckPasswordLengthTrait;

private string $algorithm;
private bool $encodeHashAsBase64;
private int $iterations = 1;
private int $length;
private int $encodedLength = -1;

/**
Expand All @@ -44,19 +40,17 @@ final class Pbkdf2PasswordHasher implements LegacyPasswordHasherInterface
* @param int $iterations The number of iterations to use to stretch the password hash
* @param int $length Length of derived key to create
*/
public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase64 = true, int $iterations = 1000, int $length = 40)
{
$this->algorithm = $algorithm;
$this->encodeHashAsBase64 = $encodeHashAsBase64;
$this->length = $length;

public function __construct(
private string $algorithm = 'sha512',
private bool $encodeHashAsBase64 = true,
private int $iterations = 1000,
private int $length = 40,
) {
try {
$this->encodedLength = \strlen($this->hash('', 'salt'));
} catch (\LogicException) {
// ignore unsupported algorithm
}

$this->iterations = $iterations;
}

public function hash(#[\SensitiveParameter] string $plainPassword, ?string $salt = null): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ class PlaintextPasswordHasher implements LegacyPasswordHasherInterface
{
use CheckPasswordLengthTrait;

private bool $ignorePasswordCase;

/**
* @param bool $ignorePasswordCase Compare password case-insensitive
*/
public function __construct(bool $ignorePasswordCase = false)
{
$this->ignorePasswordCase = $ignorePasswordCase;
public function __construct(
private bool $ignorePasswordCase = false,
) {
}

public function hash(#[\SensitiveParameter] string $plainPassword, ?string $salt = null): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
*/
class UserPasswordHasher implements UserPasswordHasherInterface
{
private PasswordHasherFactoryInterface $hasherFactory;

public function __construct(PasswordHasherFactoryInterface $hasherFactory)
{
$this->hasherFactory = $hasherFactory;
public function __construct(
private PasswordHasherFactoryInterface $hasherFactory,
) {
}

public function hashPassword(PasswordAuthenticatedUserInterface $user, #[\SensitiveParameter] string $plainPassword): string
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class PropertyAccessor implements PropertyAccessorInterface
private const CACHE_PREFIX_PROPERTY_PATH = 'p';
private const RESULT_PROTO = [self::VALUE => null];

private int $magicMethodsFlags;
private bool $ignoreInvalidIndices;
private bool $ignoreInvalidProperty;
private ?CacheItemPoolInterface $cacheItemPool;
Expand All @@ -79,9 +78,13 @@ class PropertyAccessor implements PropertyAccessorInterface
* @param int $throw A bitwise combination of the THROW_* constants
* to specify when exceptions should be thrown
*/
public function __construct(int $magicMethods = self::MAGIC_GET | self::MAGIC_SET, int $throw = self::THROW_ON_INVALID_PROPERTY_PATH, ?CacheItemPoolInterface $cacheItemPool = null, ?PropertyReadInfoExtractorInterface $readInfoExtractor = null, ?PropertyWriteInfoExtractorInterface $writeInfoExtractor = null)
{
$this->magicMethodsFlags = $magicMethods;
public function __construct(
private int $magicMethodsFlags = self::MAGIC_GET | self::MAGIC_SET,
int $throw = self::THROW_ON_INVALID_PROPERTY_PATH,
?CacheItemPoolInterface $cacheItemPool = null,
?PropertyReadInfoExtractorInterface $readInfoExtractor = null,
?PropertyWriteInfoExtractorInterface $writeInfoExtractor = null,
) {
$this->ignoreInvalidIndices = 0 === ($throw & self::THROW_ON_INVALID_INDEX);
$this->cacheItemPool = $cacheItemPool instanceof NullAdapter ? null : $cacheItemPool; // Replace the NullAdapter by the null value
$this->ignoreInvalidProperty = 0 === ($throw & self::THROW_ON_INVALID_PROPERTY_PATH);
Expand Down
9 changes: 3 additions & 6 deletions src/Symfony/Component/PropertyAccess/PropertyPathIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
*/
class PropertyPathIterator extends \ArrayIterator implements PropertyPathIteratorInterface
{
protected PropertyPathInterface $path;

public function __construct(PropertyPathInterface $path)
{
public function __construct(
protected PropertyPathInterface $path,
) {
parent::__construct($path->getElements());

$this->path = $path;
}

public function isIndex(): bool
Expand Down
13 changes: 5 additions & 8 deletions src/Symfony/Component/Semaphore/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
*/
final class Key
{
private string $resource;
private int $limit;
private int $weight;
private ?float $expiringTime = null;
private array $state = [];

public function __construct(string $resource, int $limit, int $weight = 1)
{
public function __construct(
private string $resource,
private int $limit,
private int $weight = 1,
) {
if (1 > $limit) {
throw new InvalidArgumentException("The limit ($limit) should be greater than 0.");
}
Expand All @@ -38,9 +38,6 @@ public function __construct(string $resource, int $limit, int $weight = 1)
if ($weight > $limit) {
throw new InvalidArgumentException("The weight ($weight) should be lower or equals to the limit ($limit).");
}
$this->resource = $resource;
$this->limit = $limit;
$this->weight = $weight;
}

public function __toString(): string
Expand Down
16 changes: 6 additions & 10 deletions src/Symfony/Component/Semaphore/Semaphore.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ final class Semaphore implements SemaphoreInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

private Key $key;
private PersistingStoreInterface $store;
private float $ttlInSecond;
private bool $autoRelease;
private bool $dirty = false;

public function __construct(Key $key, PersistingStoreInterface $store, float $ttlInSecond = 300.0, bool $autoRelease = true)
{
$this->store = $store;
$this->key = $key;
$this->ttlInSecond = $ttlInSecond;
$this->autoRelease = $autoRelease;
public function __construct(
private Key $key,
private PersistingStoreInterface $store,
private float $ttlInSecond = 300.0,
private bool $autoRelease = true,
) {
}

public function __sleep(): array
Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Component/Stopwatch/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class Section
*/
private array $events = [];

private ?float $origin;
private bool $morePrecision;
private ?string $id = null;

/**
Expand All @@ -36,10 +34,10 @@ class Section
* @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time
* @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
*/
public function __construct(?float $origin = null, bool $morePrecision = false)
{
$this->origin = $origin;
$this->morePrecision = $morePrecision;
public function __construct(
private ?float $origin = null,
private bool $morePrecision = false,
) {
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/Stopwatch/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class_exists(Section::class);
*/
class Stopwatch implements ResetInterface
{
private bool $morePrecision;

/**
* @var Section[]
*/
Expand All @@ -38,9 +36,9 @@ class Stopwatch implements ResetInterface
/**
* @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
*/
public function __construct(bool $morePrecision = false)
{
$this->morePrecision = $morePrecision;
public function __construct(
private bool $morePrecision = false,
) {
$this->reset();
}

Expand Down
10 changes: 6 additions & 4 deletions src/Symfony/Component/Stopwatch/StopwatchEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class StopwatchEvent

private float $origin;
private string $category;
private bool $morePrecision;

/**
* @var float[]
Expand All @@ -42,11 +41,14 @@ class StopwatchEvent
*
* @throws \InvalidArgumentException When the raw time is not valid
*/
public function __construct(float $origin, ?string $category = null, bool $morePrecision = false, ?string $name = null)
{
public function __construct(
float $origin,
?string $category = null,
private bool $morePrecision = false,
?string $name = null,
) {
$this->origin = $this->formatTime($origin);
$this->category = \is_string($category) ? $category : 'default';
$this->morePrecision = $morePrecision;
$this->name = $name ?? 'default';
}

Expand Down
21 changes: 8 additions & 13 deletions src/Symfony/Component/Yaml/Exception/ParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,19 @@
*/
class ParseException extends RuntimeException
{
private ?string $parsedFile;
private int $parsedLine;
private ?string $snippet;
private string $rawMessage;

/**
* @param string $message The error message
* @param string $rawMessage The error message
* @param int $parsedLine The line where the error occurred
* @param string|null $snippet The snippet of code near the problem
* @param string|null $parsedFile The file name where the error occurred
*/
public function __construct(string $message, int $parsedLine = -1, ?string $snippet = null, ?string $parsedFile = null, ?\Throwable $previous = null)
{
$this->parsedFile = $parsedFile;
$this->parsedLine = $parsedLine;
$this->snippet = $snippet;
$this->rawMessage = $message;

public function __construct(
private string $rawMessage,
private int $parsedLine = -1,
private ?string $snippet = null,
private ?string $parsedFile = null,
?\Throwable $previous = null,
) {
$this->updateRepr();

parent::__construct($this->message, 0, $previous);
Expand Down
11 changes: 4 additions & 7 deletions src/Symfony/Component/Yaml/Tag/TaggedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
*/
final class TaggedValue
{
private string $tag;
private mixed $value;

public function __construct(string $tag, mixed $value)
{
$this->tag = $tag;
$this->value = $value;
public function __construct(
private string $tag,
private mixed $value,
) {
}

public function getTag(): string
Expand Down

0 comments on commit 8d352bc

Please sign in to comment.