Skip to content

Commit dfd9693

Browse files
minor #54290 [Routing] Use constructor property promotion (PierreCapel)
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 ------- 8a9d5336ba refactor(routing): use constructor property promotion
2 parents 14ef058 + f37877e commit dfd9693

14 files changed

+70
-102
lines changed

CompiledRoute.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
*/
1919
class CompiledRoute implements \Serializable
2020
{
21-
private array $variables;
22-
private array $tokens;
23-
private string $staticPrefix;
24-
private string $regex;
25-
private array $pathVariables;
26-
private array $hostVariables;
27-
private ?string $hostRegex;
28-
private array $hostTokens;
29-
3021
/**
3122
* @param string $staticPrefix The static prefix of the compiled route
3223
* @param string $regex The regular expression to use to match this route
@@ -37,16 +28,16 @@ class CompiledRoute implements \Serializable
3728
* @param array $hostVariables An array of host variables
3829
* @param array $variables An array of variables (variables defined in the path and in the host patterns)
3930
*/
40-
public function __construct(string $staticPrefix, string $regex, array $tokens, array $pathVariables, ?string $hostRegex = null, array $hostTokens = [], array $hostVariables = [], array $variables = [])
41-
{
42-
$this->staticPrefix = $staticPrefix;
43-
$this->regex = $regex;
44-
$this->tokens = $tokens;
45-
$this->pathVariables = $pathVariables;
46-
$this->hostRegex = $hostRegex;
47-
$this->hostTokens = $hostTokens;
48-
$this->hostVariables = $hostVariables;
49-
$this->variables = $variables;
31+
public function __construct(
32+
private string $staticPrefix,
33+
private string $regex,
34+
private array $tokens,
35+
private array $pathVariables,
36+
private ?string $hostRegex = null,
37+
private array $hostTokens = [],
38+
private array $hostVariables = [],
39+
private array $variables = [],
40+
) {
5041
}
5142

5243
public function __serialize(): array

Generator/CompiledUrlGenerator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
class CompiledUrlGenerator extends UrlGenerator
2222
{
2323
private array $compiledRoutes = [];
24-
private ?string $defaultLocale;
2524

26-
public function __construct(array $compiledRoutes, RequestContext $context, ?LoggerInterface $logger = null, ?string $defaultLocale = null)
27-
{
25+
public function __construct(
26+
array $compiledRoutes,
27+
RequestContext $context,
28+
?LoggerInterface $logger = null,
29+
private ?string $defaultLocale = null,
30+
) {
2831
$this->compiledRoutes = $compiledRoutes;
2932
$this->context = $context;
3033
$this->logger = $logger;
31-
$this->defaultLocale = $defaultLocale;
3234
}
3335

3436
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string

Generator/Dumper/GeneratorDumper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
*/
2121
abstract class GeneratorDumper implements GeneratorDumperInterface
2222
{
23-
private RouteCollection $routes;
24-
25-
public function __construct(RouteCollection $routes)
26-
{
27-
$this->routes = $routes;
23+
public function __construct(
24+
private RouteCollection $routes,
25+
) {
2826
}
2927

3028
public function getRoutes(): RouteCollection

Generator/UrlGenerator.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
4242
'%2A' => '*',
4343
];
4444

45-
protected RouteCollection $routes;
46-
protected RequestContext $context;
4745
protected ?bool $strictRequirements = true;
48-
protected ?LoggerInterface $logger;
49-
50-
private ?string $defaultLocale;
5146

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

81-
public function __construct(RouteCollection $routes, RequestContext $context, ?LoggerInterface $logger = null, ?string $defaultLocale = null)
82-
{
83-
$this->routes = $routes;
84-
$this->context = $context;
85-
$this->logger = $logger;
86-
$this->defaultLocale = $defaultLocale;
76+
public function __construct(
77+
protected RouteCollection $routes,
78+
protected RequestContext $context,
79+
protected ?LoggerInterface $logger = null,
80+
private ?string $defaultLocale = null,
81+
) {
8782
}
8883

8984
public function setContext(RequestContext $context): void

Loader/AttributeFileLoader.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@
2525
*/
2626
class AttributeFileLoader extends FileLoader
2727
{
28-
protected AttributeClassLoader $loader;
29-
30-
public function __construct(FileLocatorInterface $locator, AttributeClassLoader $loader)
31-
{
28+
public function __construct(
29+
FileLocatorInterface $locator,
30+
protected AttributeClassLoader $loader,
31+
) {
3232
if (!\function_exists('token_get_all')) {
3333
throw new \LogicException('The Tokenizer extension is required for the routing attribute loader.');
3434
}
3535

3636
parent::__construct($locator);
37-
38-
$this->loader = $loader;
3937
}
4038

4139
/**

Loader/Configurator/AliasConfigurator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
class AliasConfigurator
1818
{
19-
private Alias $alias;
20-
21-
public function __construct(Alias $alias)
22-
{
23-
$this->alias = $alias;
19+
public function __construct(
20+
private Alias $alias,
21+
) {
2422
}
2523

2624
/**

Loader/Configurator/CollectionConfigurator.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@ class CollectionConfigurator
2323
use Traits\HostTrait;
2424
use Traits\RouteTrait;
2525

26-
private RouteCollection $parent;
27-
private ?CollectionConfigurator $parentConfigurator;
28-
private ?array $parentPrefixes;
2926
private string|array|null $host = null;
3027

31-
public function __construct(RouteCollection $parent, string $name, ?self $parentConfigurator = null, ?array $parentPrefixes = null)
32-
{
33-
$this->parent = $parent;
28+
public function __construct(
29+
private RouteCollection $parent,
30+
string $name,
31+
private ?self $parentConfigurator = null, // for GC control
32+
private ?array $parentPrefixes = null,
33+
) {
3434
$this->name = $name;
3535
$this->collection = new RouteCollection();
3636
$this->route = new Route('');
37-
$this->parentConfigurator = $parentConfigurator; // for GC control
38-
$this->parentPrefixes = $parentPrefixes;
3937
}
4038

4139
public function __sleep(): array

Loader/Configurator/ImportConfigurator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ class ImportConfigurator
2222
use Traits\PrefixTrait;
2323
use Traits\RouteTrait;
2424

25-
private RouteCollection $parent;
26-
27-
public function __construct(RouteCollection $parent, RouteCollection $route)
28-
{
29-
$this->parent = $parent;
25+
public function __construct(
26+
private RouteCollection $parent,
27+
RouteCollection $route,
28+
) {
3029
$this->route = $route;
3130
}
3231

Loader/Configurator/RouteConfigurator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ class RouteConfigurator
2222
use Traits\HostTrait;
2323
use Traits\RouteTrait;
2424

25-
protected ?CollectionConfigurator $parentConfigurator;
26-
27-
public function __construct(RouteCollection $collection, RouteCollection $route, string $name = '', ?CollectionConfigurator $parentConfigurator = null, ?array $prefixes = null)
28-
{
25+
public function __construct(
26+
RouteCollection $collection,
27+
RouteCollection $route,
28+
string $name = '',
29+
protected ?CollectionConfigurator $parentConfigurator = null, // for GC control
30+
?array $prefixes = null,
31+
) {
2932
$this->collection = $collection;
3033
$this->route = $route;
3134
$this->name = $name;
32-
$this->parentConfigurator = $parentConfigurator; // for GC control
3335
$this->prefixes = $prefixes;
3436
}
3537

Loader/Configurator/RoutingConfigurator.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@ class RoutingConfigurator
2121
{
2222
use Traits\AddTrait;
2323

24-
private PhpFileLoader $loader;
25-
private string $path;
26-
private string $file;
27-
private ?string $env;
28-
29-
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file, ?string $env = null)
30-
{
24+
public function __construct(
25+
RouteCollection $collection,
26+
private PhpFileLoader $loader,
27+
private string $path,
28+
private string $file,
29+
private ?string $env = null,
30+
) {
3131
$this->collection = $collection;
32-
$this->loader = $loader;
33-
$this->path = $path;
34-
$this->file = $file;
35-
$this->env = $env;
3632
}
3733

3834
/**

0 commit comments

Comments
 (0)