10000 CS, licensing and check alias array is array of string · symfony/symfony@107a0c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 107a0c7

Browse files
committed
CS, licensing and check alias array is array of string
1 parent 749c442 commit 107a0c7

File tree

8 files changed

+62
-16
lines changed

8 files changed

+62
-16
lines changed

src/Symfony/Component/Routing/Attribute/Route.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Route
2222
private array $localizedPaths = [];
2323
private array $methods;
2424
private array $schemes;
25+
private array $aliases;
2526

2627
/**
2728
* @param string|array<string,string>|null $path The route path (i.e. "/user/login")
@@ -39,7 +40,7 @@ class Route
3940
* @param bool|null $utf8 Whether the route accepts UTF-8 in its parameters
4041
* @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes
4142
* @param string|null $env The env in which the route is defined (i.e. "dev", "test", "prod")
42-
* @param string[] $aliases The list of aliases for this route
43+
* @param string|string[] $alias The list of aliases for this route
4344
*/
4445
public function __construct(
4546
string|array|null $path = null,
@@ -57,7 +58,7 @@ public function __construct(
5758
?bool $utf8 = null,
5859
?bool $stateless = null,
5960
private ?string $env = null,
60-
private array $aliases = [],
61+
array|string $alias = [],
6162
) {
6263
if (\is_array($path)) {
6364
$this->localizedPaths = $path;
@@ -66,7 +67,15 @@ public function __construct(
6667
}
6768
$this->setMethods($methods);
6869
$this->setSchemes($schemes);
69-
$this->setAliases($aliases);
70+
71+
if (\is_array($alias)) {
72+
foreach ($alias as $a) {
73+
if (!\is_string($a)) {
74+
throw new \TypeError(\sprintf('The "alias" argument of the Route attribute must be a string or an array of strings, but got "%s".', \get_debug_type($a)));
75+
}
76+
}
77+
}
78+
$this->setAliases($alias);
7079

7180
if (null !== $locale) {
7281
$this->defaults['_locale'] = $locale;
@@ -210,9 +219,9 @@ public function getAliases(): array
210219
return $this->aliases;
211220
}
212221

213-
public function setAliases(array $aliases): void
222+
public function setAliases(array|string $aliases): void
214223
{
215-
$this->aliases = $aliases;
224+
$this->aliases = (array) $aliases;
216225
}
217226
}
218227

src/Symfony/Component/Routing/Loader/AttributeClassLoader.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public function load(mixed $class, ?string $type = null): RouteCollection
110110

111111
if (!$class->hasMethod('__invoke')) {
112112
foreach ($this->getAttributes($class) as $attr) {
113-
if (0 !== \count($attr->getAliases())) {
114-
throw new \InvalidArgumentException(\sprintf('Class "%s" cannot have aliases as it does not have an "__invoke" method.', $class->name));
113+
if ($attr->getAliases()) {
114+
throw new \InvalidArgumentException(\sprintf('Route aliases cannot be used on non-invokable class "%s".', $class->name));
115115
}
116116
}
117117
}
@@ -239,10 +239,8 @@ protected function addRoute(RouteCollection $collection, object $attr, array $gl
239239
} else {
240240
$collection->add($name, $route, $priority);
241241
}
242-
if (0 !== \count($attr->getAliases())) {
243-
foreach ($attr->getAliases() as $alias) {
244-
$collection->addAlias($alias, $name);
245-
}
242+
foreach ($attr->getAliases() as $alias) {
243+
$collection->addAlias($alias, $name);
246244
}
247245
}
248246
}

src/Symfony/Component/Routing/Tests/Attribute/RouteTest.php

+9
32
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public function testLoadFromAttribute(string $methodName, string $getter, mixed
2727
$this->assertEquals($route->$getter(), $expectedReturn);
2828
}
2929

30+
public function testAliasIsAnArrayOfString()
31+
{
+
$this->expectException(\TypeError::class);
33+
$this->expectExceptionMessage('The "alias" argument of the Route attribute must be a string or an array of strings, but got stdClass.');
34+
35+
new Route('/hello', alias: ['alias', new \stdClass()]);
36+
}
37+
3038
public static function getValidParameters(): iterable
3139
{
3240
return [
@@ -40,6 +48,7 @@ public static function getValidParameters(): iterable
4048
['methods', 'getMethods', ['GET', 'POST']],
4149
['host', 'getHost', '{locale}.example.com'],
4250
['condition', 'getCondition', 'context.getMethod() == \'GET\''],
51+
['alias', 'getAliases', ['alias', 'completely_different_name']],
4352
];
4453
}
4554
}

src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasClassController.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
413

514
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -8,7 +17,6 @@
8< 10000 code>17
#[Route('/hello', alias: ['alias', 'completely_different_name'])]
918
class AliasClassController
1019
{
11-
1220
#[Route('/world')]
1321
public function actionWorld()
1422
{

src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasInvokableController.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
413

514
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -8,7 +17,6 @@
817
#[Route('/path', name:'invokable_path', alias: ['alias', 'completely_different_name'])]
918
class AliasInvokableController
1019
{
11-
1220
public function __invoke()
1321
{
1422
}

src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasRouteController.php

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
413

514
use Symfony\Component\Routing\Attribute\Route;

src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/FooController.php

+5
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ public function host()
5555
public function condition()
5656
{
5757
}
58+
59+
#[Route(alias: ['alias', 'completely_different_name'])]
60+
public function alias()
61+
{
62+
}
5863
}

src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,15 @@ public function testAliasesOnMethod()
373373
$routes = $this->loader->load(AliasRouteController::class);
374374
$route = $routes->get('action_with_a 2851 lias');
375375
$this->assertCount(1, $routes);
376-
$this->assertEquals('/path', $route->getPath());
376+
$this->assertSame('/path', $route->getPath());
377377
$this->assertEquals(new Alias('action_with_alias'), $routes->getAlias('alias'));
378378
$this->assertEquals(new Alias('action_with_alias'), $routes->getAlias('completely_different_name'));
379379
}
380380

381381
public function testThrowsWithAliasesOnClass()
382382
{
383383
$this->expectException(\InvalidArgumentException::class);
384-
$this->expectExceptionMessage('Class "Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasClassController" cannot have aliases as it does not have an "__invoke" method.');
384+
$this->expectExceptionMessage('Route aliases cannot be used on non-invokable class "Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasClassController".');
385385

386386
$this->loader->load(AliasClassController::class);
387387
}
@@ -391,7 +391,7 @@ public function testAliasesOnInvokableClass()
391391
$routes = $this->loader->load(AliasInvokableController::class);
392392
$route = $routes->get('invokable_path');
393393
$this->assertCount(1, $routes);
394-
$this->assertEquals('/path', $route->getPath());
394+
$this->assertSame('/path', $route->getPath());
395395
$this->assertEquals(new Alias('invokable_path'), $routes->getAlias('alias'));
396396
$this->assertEquals(new Alias('invokable_path'), $routes->getAlias('completely_different_name'));
397397
}

0 commit comments

Comments
 (0)
0