rector-symfony icon indicating copy to clipboard operation
rector-symfony copied to clipboard

AddRouteAnnotationRector does not add multiple @Route

Open javaDeveloperKid opened this issue 2 years ago • 6 comments

Consider this example:

route_name:
  path: /some-path
  defaults: { _controller: 'MyBundle:Customer:edit' }

route_name_2:
  path: /some-path # same path
  defaults: { _controller: 'MyBundle:Customer:edit' } # same defaults

AddRouteAnnotationRector results in

    /**
     * @Route(path="some-path", name="route_name")
     */
    public function editAction()
    {
    }

AddRouteAnnotationRector should add two @Route annotations.

javaDeveloperKid avatar Sep 14 '22 07:09 javaDeveloperKid

pinging @malteschlueter as an author

javaDeveloperKid avatar Sep 14 '22 07:09 javaDeveloperKid

@javaDeveloperKid I was already afraid that the question would come up at some point^^.

This behavior is I think quite difficult to handle, because we currently can't (maybe never) distinguish if the route Rector currently handles is from a YAML configuration or an existing route annotation (which must not be added again).

Because of that I skipped the route if a route annoation already exists.

I'm sorry that I can't help you :/

But give it a try maybe you can solve this issue :)

malteschlueter avatar Sep 14 '22 08:09 malteschlueter

Hmm, that gives me 2 thoughts:

  1. should Rector have a feature to mark rectors as risky? Such information would be then documented and would show up in the process command output.
  2. which must not be added again - will this break an application? Or the second one will just override the first one? If breaks then in my opinion this is still better to duplicate because we will see this in the git diff. Right know this second route just vanishes.

javaDeveloperKid avatar Sep 14 '22 08:09 javaDeveloperKid

1. should Rector have a feature to mark rectors as risky? Such information would be then documented and would show up in the `process` command output.

I have no opinion to that.

2. `which must not be added again` - will this break an application? Or the second one will just override the first one? If breaks then in my opinion this is still better to duplicate because we will see this in the git diff. Right know this second route just vanishes.

You may add an option to configure which allows Rector to add an annotation even one exists?

malteschlueter avatar Sep 14 '22 14:09 malteschlueter

You may add an option to configure which allows Rector to add an annotation even one exists?

I'll see.

javaDeveloperKid avatar Sep 14 '22 21:09 javaDeveloperKid

Because of that I skipped the route if a route annoation already exists.

I have second thoughts on this. The first route annotation is different because the name is different. So AddRouteAnnotationRector may compare names and I think names are event more important because they are unique, i.e. you can have 2 routes with the same path but you can't have 2 routes with the same name.

javaDeveloperKid avatar Sep 14 '22 21:09 javaDeveloperKid

@javaDeveloperKid I've added a fixture mimicking your example, there were no changes needed to the rule.

Can you confirm this issue is resolved?

stefantalen avatar Feb 14 '23 20:02 stefantalen

Thanks @stefantalen :clap:

Handled in https://github.com/rectorphp/rector-symfony/pull/360/files

TomasVotruba avatar Jun 24 '23 08:06 TomasVotruba