Skip to content

Commit dfc8e0d

Browse files
authored
[code-quality] keep attribute in InlineClassRoutePrefixRector if it contains other metadata (#845)
* add test fixture for rectorphp/rector#9250 * keep attribute in InlineClassRoutePrefixRector if it contains other metadata
1 parent c612b2d commit dfc8e0d

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\Routing\Annotation\Route;
7+
8+
#[\Symfony\Component\Routing\Attribute\Route("/city", name: 'some_name')]
9+
final class KeepClassRouteAttribute extends Controller
10+
{
11+
#[\Symfony\Component\Routing\Attribute\Route("/street")]
12+
public function some()
13+
{
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;
22+
23+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
24+
use Symfony\Component\Routing\Annotation\Route;
25+
26+
#[\Symfony\Component\Routing\Attribute\Route(name: 'some_name')]
27+
final class KeepClassRouteAttribute extends Controller
28+
{
29+
#[\Symfony\Component\Routing\Attribute\Route('/city/street')]
30+
public function some()
31+
{
32+
}
33+
}
34+
35+
?>

rules-tests/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector/Fixture/with_existing_name2.php.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixR
2323
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2424
use Symfony\Component\Routing\Annotation\Route;
2525

26+
#[\Symfony\Component\Routing\Attribute\Route(name: "some_org.")]
2627
final class WithExistingName2 extends Controller
2728
{
2829
#[\Symfony\Component\Routing\Attribute\Route('/city/street', name: 'some_org.some')]

rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getRuleDefinition(): RuleDefinition
6161
use Symfony\Component\Routing\Annotation\Route;
6262
6363
/**
64-
* @Route("/api")
64+
* @Route("/api", name="api_")
6565
*/
6666
class SomeController
6767
{
@@ -77,6 +77,9 @@ public function action()
7777
<<<'CODE_SAMPLE'
7878
use Symfony\Component\Routing\Annotation\Route;
7979
80+
/**
81+
* @Route(name="api_")
82+
*/
8083
class SomeController
8184
{
8285
/**
@@ -189,7 +192,7 @@ public function refactor(Node $node): ?Class_
189192
continue;
190193
}
191194

192-
if ($methodRouteArg->name->toString() === 'name') {
195+
if ($this->isName($methodRouteArg->name, 'name')) {
193196
if (! $methodRouteArg->value instanceof String_) {
194197
continue;
195198
}
@@ -220,7 +223,27 @@ public function refactor(Node $node): ?Class_
220223
} else {
221224
foreach ($node->attrGroups as $attrGroupKey => $attrGroup) {
222225
foreach ($attrGroup->attrs as $attribute) {
223-
if ($attribute === $routeAttributeOrAnnotation) {
226+
if ($attribute !== $routeAttributeOrAnnotation) {
227+
continue;
228+
}
229+
230+
// keep attribute if there are other parameters set
231+
$attrGroup = $node->attrGroups[$attrGroupKey];
232+
foreach ($attrGroup->attrs as $attributeKey => $attribute) {
233+
foreach ($attribute->args as $attributeArgKey => $attributeArg) {
234+
// silent or "path"
235+
if ($attributeArg->name === null || $attributeArg->name->toString() === self::PATH) {
236+
unset($attribute->args[$attributeArgKey]);
237+
}
238+
}
239+
240+
// nothing to keep, remove whole attribute
241+
if ($attribute->args === []) {
242+
unset($attrGroup->attrs[$attributeKey]);
243+
}
244+
}
245+
246+
if ($attrGroup->attrs === []) {
224247
unset($node->attrGroups[$attrGroupKey]);
225248
}
226249
}

0 commit comments

Comments
 (0)