Skip to content

[ANNOTATIONS_TO_ATTRIBUTES] Upgrading Assert\Range leads to error #8300

Closed

Description

@rvanlaak

Bug Report

Subject Details
Rector version v0.18.6

The Symfony / Sensio annotation to attribute upgrade works great, except for one BC break. It is up for debate whether annotations should be 100% BC with attributes.

Minimal PHP Code Causing Issue

## entity file

/*
  * @Assert\Range(
  *      min = 0,
  *      max = 100,
  *      minMessage = "Average discount must be at least {{ limit }}",
  *      maxMessage = "Average discount must be less than {{ limit }}"
  * )
  */
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\Symfony\Set\SensiolabsSetList;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/config',
        __DIR__ . '/public',
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ]);

    $rectorConfig->sets([
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
        SensiolabsSetList::ANNOTATIONS_TO_ATTRIBUTES,
    ]);
};

Actual Behaviour

The parameters are kept intact

#[Assert\Range(minMessage: 'Average discount must be at least {{ limit }}', maxMessage: 'Average discount must be less than {{ limit }}', min: 0, max: 100)]

Expected Behaviour

But should be changed to notInRangeMessage instead. Without changing it, an exception will be thrown.

Maybe we can consider

  1. always remove the values
  2. merge the two messages to one message (replace {{limit}} with min or max
  3. ask the developer on rector execution what to do
#[Assert\Range(notInRangeMessage: 'Average discount should be between {{ min }} and {{ max }}.', min: 0, max: 100)]

Activity

  1. TomasVotruba commented on Dec 3, 2023

    @TomasVotruba
    Member

    Thank you for your report!

    We'll need an isolated failing demo link from: http://getrector.com/demo,
    that way we can reproduce the bug.

    The smaller demo the better 👍

  2. mindaugasvcs commented on Dec 25, 2023

    @mindaugasvcs

    ANNOTATIONS_TO_ATTRIBUTES is working fine until Doctrine annotations gets involved, it then just hangs forever and the --debug option does not provide any message.
    I found that this line in my Entity class caused problem:
    use Doctrine\ORM\Mapping\Index as Index;
    When I removed ' as Index' it suddenly started working fine.
    Not sure if it is a bug.

  3. TomasVotruba commented on Dec 25, 2023

    @TomasVotruba
    Member

    Thank you for your report!

    We'll still need an isolated failing demo link from: http://getrector.com/demo,
    that way we can reproduce the bug. Closing for now to avoid piling up more issue without demo link.

    Feel free to create a new one with demo link, so we can see the issue ourselves and fix it 👍

  4. mindaugasvcs commented on Dec 25, 2023

    @mindaugasvcs

    Thank you for your report!

    We'll still need an isolated failing demo link from: http://getrector.com/demo, that way we can reproduce the bug. Closing for now to avoid piling up more issue without demo link.

    Feel free to create a new one with demo link, so we can see the issue ourselves and fix it 👍

    Recreating the issue is not possible on your test site as it depends on the libs it uses, still the output is not quite right, it just spits out 'use Doctrine\ORM\Mapping\Index;' on the top while leaving 'use Doctrine\ORM\Mapping\Index as Index;' as it is.

  5. TomasVotruba commented on Dec 25, 2023

    @TomasVotruba
    Member

    Could you share the demo link with minimal PHP code that is causing the troubles? Only that way we can start digging

  6. mindaugasvcs commented on Dec 25, 2023

    @mindaugasvcs

    Could you share the demo link with minimal PHP code that is causing the troubles? Only that way we can start digging

    https://getrector.com/demo/8669e371-05c9-477f-b32a-34ef1400bb99

  7. TomasVotruba commented on Dec 26, 2023

    @TomasVotruba
    Member

    Thanks 👍

    @samsonasik Could you look into this one? Seems use import is being added despite already existing one.

  8. samsonasik commented on Dec 26, 2023

    @samsonasik
    Member

    The issue seems unrelated with import.

    Alias name should be different with last name import in the namespace, it like doing useless alias:

    namespace App;
    
    use stdClass as stdClass;

    which should not happen on the first place.

    @mindaugasvcs we need reproducible repo that show the issue, with before after expected output

  9. samsonasik commented on Dec 26, 2023

    @samsonasik
    Member

    I think the solution is you can just simply remove the useless alias:

    -use Doctrine\ORM\Mapping\Index as Index;
    +use Doctrine\ORM\Mapping\Index;

    as that useless, so I guess it is not a bug.

  10. samsonasik commented on Dec 26, 2023

    @samsonasik
    Member

    I am closing it as it is not a bug, the alias name is same with last name, which should not happen on the first place.

  11. samsonasik commented on Dec 26, 2023

    @samsonasik
    Member

    I created new special rule to remove useless alias in use statement as same with last name:

  12. TomasVotruba commented on Dec 26, 2023

    @TomasVotruba
    Member

    @samsonasik Even better, thanks for the new rule 👍

  13. github-actions commented on Mar 15, 2026

    @github-actions
    Contributor

    This issue has been automatically locked because it has been closed for 150 days. Please open a new issue if you have a similar problem.

  14. locked as resolved and limited conversation to collaborators on Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions