Skip to content

Commit

Permalink
minor #54290 [Routing] Use constructor property promotion (PierreCapel)
Browse files Browse the repository at this point in the history
This PR was merged into the 7.1 branch.

Discussion
----------

[Routing] Use constructor property promotion

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

Commits
-------

8a9d533 refactor(routing): use constructor property promotion
  • Loading branch information
nicolas-grekas committed Mar 21, 2024
2 parents 3a5d0c6 + 8a9d533 commit 525ea4c
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 102 deletions.
29 changes: 10 additions & 19 deletions src/Symfony/Component/Routing/CompiledRoute.php
Expand Up @@ -18,15 +18,6 @@
*/
class CompiledRoute implements \Serializable
{
private array $variables;
private array $tokens;
private string $staticPrefix;
private string $regex;
private array $pathVariables;
private array $hostVariables;
private ?string $hostRegex;
private array $hostTokens;

/**
* @param string $staticPrefix The static prefix of the compiled route
* @param string $regex The regular expression to use to match this route
Expand All @@ -37,16 +28,16 @@ class CompiledRoute implements \Serializable
* @param array $hostVariables An array of host variables
* @param array $variables An array of variables (variables defined in the path and in the host patterns)
*/
public function __construct(string $staticPrefix, string $regex, array $tokens, array $pathVariables, ?string $hostRegex = null, array $hostTokens = [], array $hostVariables = [], array $variables = [])
{
$this->staticPrefix = $staticPrefix;
$this->regex = $regex;
$this->tokens = $tokens;
$this->pathVariables = $pathVariables;
$this->hostRegex = $hostRegex;
$this->hostTokens = $hostTokens;
$this->hostVariables = $hostVariables;
$this->variables = $variables;
public function __construct(
private string $staticPrefix,
private string $regex,
private array $tokens,
private array $pathVariables,
private ?string $hostRegex = null,
private array $hostTokens = [],
private array $hostVariables = [],
private array $variables = [],
) {
}

public function __serialize(): array
Expand Down
10 changes: 6 additions & 4 deletions src/Symfony/Component/Routing/Generator/CompiledUrlGenerator.php
Expand Up @@ -21,14 +21,16 @@
class CompiledUrlGenerator extends UrlGenerator
{
private array $compiledRoutes = [];
private ?string $defaultLocale;

public function __construct(array $compiledRoutes, RequestContext $context, ?LoggerInterface $logger = null, ?string $defaultLocale = null)
{
public function __construct(
array $compiledRoutes,
RequestContext $context,
?LoggerInterface $logger = null,
private ?string $defaultLocale = null,
) {
$this->compiledRoutes = $compiledRoutes;
$this->context = $context;
$this->logger = $logger;
$this->defaultLocale = $defaultLocale;
}

public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
Expand Down
Expand Up @@ -20,11 +20,9 @@
*/
abstract class GeneratorDumper implements GeneratorDumperInterface
{
private RouteCollection $routes;

public function __construct(RouteCollection $routes)
{
$this->routes = $routes;
public function __construct(
private RouteCollection $routes,
) {
}

public function getRoutes(): RouteCollection
Expand Down
17 changes: 6 additions & 11 deletions src/Symfony/Component/Routing/Generator/UrlGenerator.php
Expand Up @@ -42,12 +42,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
'%2A' => '*',
];

protected RouteCollection $routes;
protected RequestContext $context;
protected ?bool $strictRequirements = true;
protected ?LoggerInterface $logger;

private ?string $defaultLocale;

/**
* This array defines the characters (besides alphanumeric ones) that will not be percent-encoded in the path segment of the generated URL.
Expand Down Expand Up @@ -78,12 +73,12 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
'%7C' => '|',
];

public function __construct(RouteCollection $routes, RequestContext $context, ?LoggerInterface $logger = null, ?string $defaultLocale = null)
{
$this->routes = $routes;
$this->context = $context;
$this->logger = $logger;
$this->defaultLocale = $defaultLocale;
public function __construct(
protected RouteCollection $routes,
protected RequestContext $context,
protected ?LoggerInterface $logger = null,
private ?string $defaultLocale = null,
) {
}

public function setContext(RequestContext $context): void
Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Component/Routing/Loader/AttributeFileLoader.php
Expand Up @@ -25,17 +25,15 @@
*/
class AttributeFileLoader extends FileLoader
{
protected AttributeClassLoader $loader;

public function __construct(FileLocatorInterface $locator, AttributeClassLoader $loader)
{
public function __construct(
FileLocatorInterface $locator,
protected AttributeClassLoader $loader,
) {
if (!\function_exists('token_get_all')) {
throw new \LogicException('The Tokenizer extension is required for the routing attribute loader.');
}

parent::__construct($locator);

$this->loader = $loader;
}

/**
Expand Down
Expand Up @@ -16,11 +16,9 @@

class AliasConfigurator
{
private Alias $alias;

public function __construct(Alias $alias)
{
$this->alias = $alias;
public function __construct(
private Alias $alias,
) {
}

/**
Expand Down
Expand Up @@ -23,19 +23,17 @@ class CollectionConfigurator
use Traits\HostTrait;
use Traits\RouteTrait;

private RouteCollection $parent;
private ?CollectionConfigurator $parentConfigurator;
private ?array $parentPrefixes;
private string|array|null $host = null;

public function __construct(RouteCollection $parent, string $name, ?self $parentConfigurator = null, ?array $parentPrefixes = null)
{
$this->parent = $parent;
public function __construct(
private RouteCollection $parent,
string $name,
private ?self $parentConfigurator = null, // for GC control
private ?array $parentPrefixes = null,
) {
$this->name = $name;
$this->collection = new RouteCollection();
$this->route = new Route('');
$this->parentConfigurator = $parentConfigurator; // for GC control
$this->parentPrefixes = $parentPrefixes;
}

public function __sleep(): array
Expand Down
Expand Up @@ -22,11 +22,10 @@ class ImportConfigurator
use Traits\PrefixTrait;
use Traits\RouteTrait;

private RouteCollection $parent;

public function __construct(RouteCollection $parent, RouteCollection $route)
{
$this->parent = $parent;
public function __construct(
private RouteCollection $parent,
RouteCollection $route,
) {
$this->route = $route;
}

Expand Down
Expand Up @@ -22,14 +22,16 @@ class RouteConfigurator
use Traits\HostTrait;
use Traits\RouteTrait;

protected ?CollectionConfigurator $parentConfigurator;

public function __construct(RouteCollection $collection, RouteCollection $route, string $name = '', ?CollectionConfigurator $parentConfigurator = null, ?array $prefixes = null)
{
public function __construct(
RouteCollection $collection,
RouteCollection $route,
string $name = '',
protected ?CollectionConfigurator $parentConfigurator = null, // for GC control
?array $prefixes = null,
) {
$this->collection = $collection;
$this->route = $route;
$this->name = $name;
$this->parentConfigurator = $parentConfigurator; // for GC control
$this->prefixes = $prefixes;
}

Expand Down
Expand Up @@ -21,18 +21,14 @@ class RoutingConfigurator
{
use Traits\AddTrait;

private PhpFileLoader $loader;
private string $path;
private string $file;
private ?string $env;

public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file, ?string $env = null)
{
public function __construct(
RouteCollection $collection,
private PhpFileLoader $loader,
private string $path,
private string $file,
private ?string $env = null,
) {
$this->collection = $collection;
$this->loader = $loader;
$this->path = $path;
$this->file = $file;
$this->env = $env;
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Symfony/Component/Routing/Loader/ContainerLoader.php
Expand Up @@ -20,11 +20,10 @@
*/
class ContainerLoader extends ObjectLoader
{
private ContainerInterface $container;

public function __construct(ContainerInterface $container, ?string $env = null)
{
$this->container = $container;
public function __construct(
private ContainerInterface $container,
?string $env = null,
) {
parent::__construct($env);
}

Expand Down
Expand Up @@ -20,11 +20,9 @@
*/
abstract class MatcherDumper implements MatcherDumperInterface
{
private RouteCollection $routes;

public function __construct(RouteCollection $routes)
{
$this->routes = $routes;
public function __construct(
private RouteCollection $routes,
) {
}

public function getRoutes(): RouteCollection
Expand Down
Expand Up @@ -22,11 +22,9 @@
*/
class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
{
private ServiceProviderInterface $functions;

public function __construct(ServiceProviderInterface $functions)
{
$this->functions = $functions;
public function __construct(
private ServiceProviderInterface $functions,
) {
}

public function getFunctions(): array
Expand Down
12 changes: 4 additions & 8 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Expand Up @@ -32,8 +32,6 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
public const REQUIREMENT_MISMATCH = 1;
public const ROUTE_MATCH = 2;

protected RequestContext $context;

/**
* Collects HTTP methods that would be allowed for the request.
*/
Expand All @@ -45,8 +43,6 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
* @internal
*/
protected array $allowSchemes = [];

protected RouteCollection $routes;
protected ?Request $request = null;
protected ExpressionLanguage $expressionLanguage;

Expand All @@ -55,10 +51,10 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
*/
protected array $expressionLanguageProviders = [];

public function __construct(RouteCollection $routes, RequestContext $context)
{
$this->routes = $routes;
$this->context = $context;
public function __construct(
protected RouteCollection $routes,
protected RequestContext $context,
) {
}

public function setContext(RequestContext $context): void
Expand Down

0 comments on commit 525ea4c

Please sign in to comment.