Skip to content

Commit

Permalink
use constructor property promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jun 4, 2024
1 parent b3508ee commit b1e4439
Show file tree
Hide file tree
Showing 95 changed files with 468 additions and 707 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@
*/
class CacheTokenVerifier implements TokenVerifierInterface
{
private CacheItemPoolInterface $cache;
private int $outdatedTokenTtl;
private string $cacheKeyPrefix;

/**
* @param int $outdatedTokenTtl How long the outdated token should still be considered valid. Defaults
* to 60, which matches how often the PersistentRememberMeHandler will at
* most refresh tokens. Increasing to more than that is not recommended,
* but you may use a lower value.
*/
public function __construct(CacheItemPoolInterface $cache, int $outdatedTokenTtl = 60, string $cacheKeyPrefix = 'rememberme-stale-')
{
$this->cache = $cache;
$this->outdatedTokenTtl = $outdatedTokenTtl;
$this->cacheKeyPrefix = $cacheKeyPrefix;
public function __construct(
private CacheItemPoolInterface $cache,
private int $outdatedTokenTtl = 60,
private string $cacheKeyPrefix = 'rememberme-stale-',
) {
}

public function verifyToken(PersistentTokenInterface $token, #[\SensitiveParameter] string $tokenValue): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
*/
final class PersistentToken implements PersistentTokenInterface
{
private string $class;
private string $userIdentifier;
private string $series;
private string $tokenValue;
private \DateTimeImmutable $lastUsed;

public function __construct(string $class, string $userIdentifier, string $series, #[\SensitiveParameter] string $tokenValue, \DateTimeInterface $lastUsed)
{
public function __construct(
private string $class,
private string $userIdentifier,
private string $series,
#[\SensitiveParameter] private string $tokenValue,
\DateTimeInterface $lastUsed,
) {
if (!$class) {
throw new \InvalidArgumentException('$class must not be empty.');
}
Expand All @@ -39,10 +40,6 @@ public function __construct(string $class, string $userIdentifier, string $serie
throw new \InvalidArgumentException('$tokenValue must not be empty.');
}

$this->class = $class;
$this->userIdentifier = $userIdentifier;
$this->series = $series;
$this->tokenValue = $tokenValue;
$this->lastUsed = \DateTimeImmutable::createFromInterface($lastUsed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
*/
class PreAuthenticatedToken extends AbstractToken
{
private string $firewallName;

/**
* @param string[] $roles
*/
public function __construct(UserInterface $user, string $firewallName, array $roles = [])
{
public function __construct(
UserInterface $user,
private string $firewallName,
array $roles = [],
) {
parent::__construct($roles);

if ('' === $firewallName) {
throw new \InvalidArgumentException('$firewallName must not be empty.');
}

$this->setUser($user);
$this->firewallName = $firewallName;
}

public function getFirewallName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
class RememberMeToken extends AbstractToken
{
private ?string $secret = null;
private string $firewallName;

/**
* @throws \InvalidArgumentException
*/
public function __construct(UserInterface $user, string $firewallName)
{
public function __construct(
UserInterface $user,
private string $firewallName,
) {
parent::__construct($user->getRoles());

if (\func_num_args() > 2) {
Expand All @@ -40,8 +41,6 @@ public function __construct(UserInterface $user, string $firewallName)
throw new InvalidArgumentException('$firewallName must not be empty.');
}

$this->firewallName = $firewallName;

$this->setUser($user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
*/
final class UsageTrackingTokenStorage implements TokenStorageInterface, ServiceSubscriberInterface
{
private TokenStorageInterface $storage;
private ContainerInterface $container;
private bool $enableUsageTracking = false;

public function __construct(TokenStorageInterface $storage, ContainerInterface $container)
{
$this->storage = $storage;
$this->container = $container;
public function __construct(
private TokenStorageInterface $storage,
private ContainerInterface $container,
) {
}

public function getToken(): ?TokenInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class SwitchUserToken extends UsernamePasswordToken
{
private TokenInterface $originalToken;
private ?string $originatedFromUri = null;

/**
Expand All @@ -29,11 +28,15 @@ class SwitchUserToken extends UsernamePasswordToken
*
* @throws \InvalidArgumentException
*/
public function __construct(UserInterface $user, string $firewallName, array $roles, TokenInterface $originalToken, ?string $originatedFromUri = null)
{
public function __construct(
UserInterface $user,
string $firewallName,
array $roles,
private TokenInterface $originalToken,
?string $originatedFromUri = null,
) {
parent::__construct($user, $firewallName, $roles);

$this->originalToken = $originalToken;
$this->originatedFromUri = $originatedFromUri;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
*/
class UsernamePasswordToken extends AbstractToken
{
private string $firewallName;

public function __construct(UserInterface $user, string $firewallName, array $roles = [])
{
public function __construct(
UserInterface $user,
private string $firewallName,
array $roles = [],
) {
parent::__construct($roles);

if ('' === $firewallName) {
throw new \InvalidArgumentException('$firewallName must not be empty.');
}

$this->setUser($user);
$this->firewallName = $firewallName;
}

public function getFirewallName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ final class AccessDecisionManager implements AccessDecisionManagerInterface
VoterInterface::ACCESS_ABSTAIN => true,
];

private iterable $voters;
private array $votersCacheAttributes = [];
private array $votersCacheObject = [];
private AccessDecisionStrategyInterface $strategy;

/**
* @param iterable<mixed, VoterInterface> $voters An array or an iterator of VoterInterface instances
*/
public function __construct(iterable $voters = [], ?AccessDecisionStrategyInterface $strategy = null)
{
$this->voters = $voters;
public function __construct(
private iterable $voters = [],
?AccessDecisionStrategyInterface $strategy = null,
) {
$this->strategy = $strategy ?? new AffirmativeStrategy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
*/
final class AffirmativeStrategy implements AccessDecisionStrategyInterface, \Stringable
{
private bool $allowIfAllAbstainDecisions;

public function __construct(bool $allowIfAllAbstainDecisions = false)
{
$this->allowIfAllAbstainDecisions = $allowIfAllAbstainDecisions;
public function __construct(
private bool $allowIfAllAbstainDecisions = false,
) {
}

public function decide(\Traversable $results): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@
*/
final class ConsensusStrategy implements AccessDecisionStrategyInterface, \Stringable
{
private bool $allowIfAllAbstainDecisions;
private bool $allowIfEqualGrantedDeniedDecisions;

public function __construct(bool $allowIfAllAbstainDecisions = false, bool $allowIfEqualGrantedDeniedDecisions = true)
{
$this->allowIfAllAbstainDecisions = $allowIfAllAbstainDecisions;
$this->allowIfEqualGrantedDeniedDecisions = $allowIfEqualGrantedDeniedDecisions;
public function __construct(
private bool $allowIfAllAbstainDecisions = false,
private bool $allowIfEqualGrantedDeniedDecisions = true,
) {
}

public function decide(\Traversable $results): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
*/
final class PriorityStrategy implements AccessDecisionStrategyInterface, \Stringable
{
private bool $allowIfAllAbstainDecisions;

public function __construct(bool $allowIfAllAbstainDecisions = false)
{
$this->allowIfAllAbstainDecisions = $allowIfAllAbstainDecisions;
public function __construct(
private bool $allowIfAllAbstainDecisions = false,
) {
}

public function decide(\Traversable $results): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
*/
final class UnanimousStrategy implements AccessDecisionStrategyInterface, \Stringable
{
private bool $allowIfAllAbstainDecisions;

public function __construct(bool $allowIfAllAbstainDecisions = false)
{
$this->allowIfAllAbstainDecisions = $allowIfAllAbstainDecisions;
public function __construct(
private bool $allowIfAllAbstainDecisions = false,
) {
}

public function decide(\Traversable $results): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@
*/
class TraceableAccessDecisionManager implements AccessDecisionManagerInterface
{
private AccessDecisionManagerInterface $manager;
private ?AccessDecisionStrategyInterface $strategy = null;
/** @var iterable<mixed, VoterInterface> */
private iterable $voters = [];
private array $decisionLog = []; // All decision logs
private array $currentLog = []; // Logs being filled in

public function __construct(AccessDecisionManagerInterface $manager)
{
$this->manager = $manager;

public function __construct(
private AccessDecisionManagerInterface $manager,
) {
// The strategy and voters are stored in a private properties of the decorated service
if (property_exists($manager, 'strategy')) {
$reflection = new \ReflectionProperty($manager::class, 'strategy');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ class AuthenticatedVoter implements CacheableVoterInterface
public const IS_REMEMBERED = 'IS_REMEMBERED';
public const PUBLIC_ACCESS = 'PUBLIC_ACCESS';

private AuthenticationTrustResolverInterface $authenticationTrustResolver;

public function __construct(AuthenticationTrustResolverInterface $authenticationTrustResolver)
{
$this->authenticationTrustResolver = $authenticationTrustResolver;
public function __construct(
private AuthenticationTrustResolverInterface $authenticationTrustResolver,
) {
}

public function vote(TokenInterface $token, mixed $subject, array $attributes): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@
*/
class ExpressionVoter implements CacheableVoterInterface
{
private ExpressionLanguage $expressionLanguage;
private AuthenticationTrustResolverInterface $trustResolver;
private AuthorizationCheckerInterface $authChecker;
private ?RoleHierarchyInterface $roleHierarchy;

public function __construct(ExpressionLanguage $expressionLanguage, AuthenticationTrustResolverInterface $trustResolver, AuthorizationCheckerInterface $authChecker, ?RoleHierarchyInterface $roleHierarchy = null)
{
$this->expressionLanguage = $expressionLanguage;
$this->trustResolver = $trustResolver;
$this->authChecker = $authChecker;
$this->roleHierarchy = $roleHierarchy;
public function __construct(
private ExpressionLanguage $expressionLanguage,
private AuthenticationTrustResolverInterface $trustResolver,
private AuthorizationCheckerInterface $authChecker,
private ?RoleHierarchyInterface $roleHierarchy = null,
) {
}

public function supportsAttribute(string $attribute): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
*/
class RoleHierarchyVoter extends RoleVoter
{
private RoleHierarchyInterface $roleHierarchy;

public function __construct(RoleHierarchyInterface $roleHierarchy, string $prefix = 'ROLE_')
{
$this->roleHierarchy = $roleHierarchy;

public function __construct(
private RoleHierarchyInterface $roleHierarchy,
string $prefix = 'ROLE_',
) {
parent::__construct($prefix);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
*/
class RoleVoter implements CacheableVoterInterface
{
private string $prefix;

public function __construct(string $prefix = 'ROLE_')
{
$this->prefix = $prefix;
public function __construct(
private string $prefix = 'ROLE_',
) {
}

public function vote(TokenInterface $token, mixed $subject, array $attributes): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
*/
class TraceableVoter implements CacheableVoterInterface
{
private VoterInterface $voter;
private EventDispatcherInterface $eventDispatcher;

public function __construct(VoterInterface $voter, EventDispatcherInterface $eventDispatcher)
{
$this->voter = $voter;
$this->eventDispatcher = $eventDispatcher;
public function __construct(
private VoterInterface $voter,
private EventDispatcherInterface $eventDispatcher,
) {
}

public function vote(TokenInterface $token, mixed $subject, array $attributes): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
*/
class AuthenticationEvent extends Event
{
private TokenInterface $authenticationToken;

public function __construct(TokenInterface $token)
{
$this->authenticationToken = $token;
public function __construct(
private TokenInterface $token,
) {
}

public function getAuthenticationToken(): TokenInterface
{
return $this->authenticationToken;
return $this->token;
}
}
Loading

0 comments on commit b1e4439

Please sign in to comment.