Skip to content

[Symfony 6.1] Add RouteRequirementStringToConstantRector - #1037

Merged
TomasVotruba merged 2 commits into
mainfrom
route-requirement-constants
Aug 12, 2026
Merged

[Symfony 6.1] Add RouteRequirementStringToConstantRector#1037
TomasVotruba merged 2 commits into
mainfrom
route-requirement-constants

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Custom rule alternative to #1036 — scoped to #[Route] requirements instead of a global StringToClassConstantRector configuration.

 use Symfony\Component\Routing\Attribute\Route;
+use Symfony\Component\Routing\Requirement\Requirement;

 final class SomeController
 {
     #[Route('/detail/{id}', requirements: [
-        'id' => '[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}',
+        'id' => Requirement::UUID_V4,
     ])]
     public function detail()
     {
     }
 }

Why a custom rule:

  • StringToClassConstantRector replaces the regex anywhere in the project - in a preg_match() call, a validator constraint, a test fixture. Here the replacement only happens inside the requirements: argument of #[Route].
  • The constant map is read from the installed Requirement class via reflection, so each project gets exactly the constants its symfony/routing version has - no per-version config blocks (6.1 / 6.2 / 7.3) to keep in sync, and MONGODB_ID starts applying automatically once a project is on 7.3.
  • Version bonded via ComposerPackageConstraintInterface to symfony/routing >=6.1, registered once in config/sets/symfony/composer-based.php.

TomasVotruba and others added 2 commits August 12, 2026 14:39
Replace regex strings in #[Route] requirements with Symfony\Component\Routing\Requirement\Requirement constants.
@TomasVotruba
TomasVotruba merged commit 77d3bb9 into main Aug 12, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the route-requirement-constants branch August 12, 2026 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants