rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.7k stars 684 forks source link

Refactor Symfony route names and paths to snake_case with Rector #8868

Open ahmed-bhs opened 1 hour ago

ahmed-bhs commented 1 hour ago

Hi Rector team,

Following the recent acceptance of the proposal to use snake_case for routes in Symfony, as detailed in this Symfony documentation pull request, I’d like to suggest adding a Rector rule that automates the refactoring of route names and paths to snake_case.

Why is this useful?

  1. Consistency: Symfony is moving towards snake_case for route naming and paths, as it’s more readable and aligns with common practices in other frameworks and languages like Python and Ruby.
  2. Industry standards: Major companies like Google and Facebook use snake_case in their APIs. Aligning Symfony routes with this standard will help developers transitioning from other ecosystems.
  3. Automation: Having a Rector rule will make it easier to apply these changes across projects, ensuring uniformity without manual intervention.

Next Steps:

Looking forward to your feedback and guidance!

Best regards,

TomasVotruba commented 1 hour ago

Hi, thanks for proposal.

Sounds good to me. Feel free to send the rule :+1:

The best way to start is with the docs for custom rules: https://getrector.com/documentation/custom-rule

ahmed-bhs commented 1 hour ago

Hi @TomasVotruba, Thank you for considering the idea. I appreciate your attention to this matter.

Here’s the plan:

  1. Objective: Create a custom Rector class that renames all route names and paths to snake_case if they are currently in camelCase, kebab-case, or any other format.

  2. Implementation Steps:

    • Utilize the Reflection API to access the route attribute definitions.
    • Retrieve all route attributes defined in PHP classes.
    • Use the Symfony String component to convert names and paths to snake_case.
    • Implement logic to also search for route definitions in Twig, YAML, and automatically rename them.
    • Ensure the new naming conventions are applied consistently across all identified routes.
  3. Class Structure:

    • Create a class class SnakeCaseRouteRector that extends AbstractRector.
    • Implement the refactor method to iterate over the routes.
    • Define a private method to rename each route and convert it to snake_case.
    • Implement logic for handling Twig, YAML files for renaming routes.
  4. Testing:

    • Thoroughly test the Rector class to ensure it correctly renames routes without breaking functionality.
  5. Documentation:

    • Document the new features and usage of the custom Rector class.

If you have any comments or concerns about anything, just let me know. I’m eager to hear your thoughts on this plan!