Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

#[\Symfony\Component\Routing\Attribute\Route("/city", name: 'some_name')]
final class KeepClassRouteAttribute extends Controller
{
#[\Symfony\Component\Routing\Attribute\Route("/street")]
public function some()
{
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

#[\Symfony\Component\Routing\Attribute\Route(name: 'some_name')]
final class KeepClassRouteAttribute extends Controller
{
#[\Symfony\Component\Routing\Attribute\Route('/city/street')]
public function some()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixR
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

#[\Symfony\Component\Routing\Attribute\Route(name: "some_org.")]
final class WithExistingName2 extends Controller
{
#[\Symfony\Component\Routing\Attribute\Route('/city/street', name: 'some_org.some')]
Expand Down
29 changes: 26 additions & 3 deletions rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getRuleDefinition(): RuleDefinition
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/api")
* @Route("/api", name="api_")
*/
class SomeController
{
Expand All @@ -77,6 +77,9 @@ public function action()
<<<'CODE_SAMPLE'
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route(name="api_")
*/
class SomeController
{
/**
Expand Down Expand Up @@ -189,7 +192,7 @@ public function refactor(Node $node): ?Class_
continue;
}

if ($methodRouteArg->name->toString() === 'name') {
if ($this->isName($methodRouteArg->name, 'name')) {
if (! $methodRouteArg->value instanceof String_) {
continue;
}
Expand Down Expand Up @@ -220,7 +223,27 @@ public function refactor(Node $node): ?Class_
} else {
foreach ($node->attrGroups as $attrGroupKey => $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
if ($attribute === $routeAttributeOrAnnotation) {
if ($attribute !== $routeAttributeOrAnnotation) {
continue;
}

// keep attribute if there are other parameters set
$attrGroup = $node->attrGroups[$attrGroupKey];
foreach ($attrGroup->attrs as $attributeKey => $attribute) {
foreach ($attribute->args as $attributeArgKey => $attributeArg) {
// silent or "path"
if ($attributeArg->name === null || $attributeArg->name->toString() === self::PATH) {
unset($attribute->args[$attributeArgKey]);
}
}

// nothing to keep, remove whole attribute
if ($attribute->args === []) {
unset($attrGroup->attrs[$attributeKey]);
}
}

if ($attrGroup->attrs === []) {
unset($node->attrGroups[$attrGroupKey]);
}
}
Expand Down