Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ class SomeClass extends SomeParentClass

### RequireRouteNameToGenerateControllerRouteRule

To pass a controller class to generate() method, the controller must have "#[Route(name: self::class)]" above the __invoke() method
To pass a controller class to generate() method, the controller must have "#[Route]" above the __invoke() method

```yaml
rules:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

/**
* To pass a controller class in $this->router->generate(SomeController::class),
* the controller must be present #[Route(name:: self::class)
* the __invoke() must have a #[Route]. Since Symfony 6.4 the FQCN route alias is
* auto-created for any route name.
*
* @see https://symfony.com/blog/new-in-symfony-6-4-fqcn-based-routes
*
Expand All @@ -36,7 +37,7 @@
/**
* @api
*/
public const string ERROR_MESSAGE = 'To pass a controller class to generate() method, the controller must have "#[Route(name: self::class)]" above the __invoke() method';
public const string ERROR_MESSAGE = 'To pass a controller class to generate() method, the controller must have "#[Route]" above the __invoke() method';

public function __construct(
private ReflectionProvider $reflectionProvider,
Expand Down Expand Up @@ -77,7 +78,9 @@ public function processNode(Node $node, Scope $scope): array
}

$routeAttributes = $this->findRouteAttributes($invokeClassMethodReflection);
if ($this->hasAtLeastOneRouteWithSelfClassName($routeAttributes, $controllerClassReflection)) {

// any #[Route] is enough, Symfony 6.4+ auto-creates the FQCN alias
if ($routeAttributes !== []) {
return [];
}

Expand Down Expand Up @@ -134,21 +137,4 @@ private function findRouteAttributes(ReflectionMethod $reflectionMethod): array
$reflectionMethod->getAttributes(SymfonyClass::ROUTE_ANNOTATION)
);
}

/**
* @param ReflectionAttribute[] $routeAttributes
*/
private function hasAtLeastOneRouteWithSelfClassName(array $routeAttributes, ClassReflection $classReflection): bool
{
foreach ($routeAttributes as $routeAttribute) {
$routeName = $routeAttribute->getArguments()['name'] ?? null;

// name must be same as current controller class
if ($routeName === $classReflection->getName()) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Routing\RouterInterface;
use Symplify\PHPStanRules\Tests\Rules\Symfony\RequireRouteNameToGenerateControllerRouteRule\Source\SomeControllerWithStringNameClass;

final class CallingControllerWithWrongString
final class CallingControllerWithStringName
{
public function run(RouterInterface $router)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ public static function provideData(): Iterator
yield [__DIR__ . '/Fixture/CallingCorrectController.php', []];
yield [__DIR__ . '/Fixture/TwoRoutes.php', []];

// any #[Route] name is valid since Symfony 6.4 auto-creates the FQCN alias
yield [__DIR__ . '/Fixture/CallingControllerWithStringName.php', []];

yield [__DIR__ . '/Fixture/CallingWrongController.php', [
[RequireRouteNameToGenerateControllerRouteRule::ERROR_MESSAGE, 14],
]];

yield [__DIR__ . '/Fixture/CallingControllerWithoutInvoke.php', [
[RequireRouteNameToGenerateControllerRouteRule::ERROR_MESSAGE, 14],
]];

yield [__DIR__ . '/Fixture/CallingControllerWithWrongString.php', [
[RequireRouteNameToGenerateControllerRouteRule::ERROR_MESSAGE, 14],
]];
}

protected function getRule(): Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace Symplify\PHPStanRules\Tests\Rules\Symfony\RequireRouteNameToGenerateControllerRouteRule\Source;

use Symfony\Component\Routing\Attribute\Route;

class SomeControllerWIthoutRouteClass
{
#[Route()]
public function __invoke()
{
}
Expand Down
Loading