Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect behavior of AnnotationToAttributeRector with doctrine @Table annotation and uniqueConstraints option #7011

Closed
acrobat opened this issue Feb 20, 2022 · 4 comments · Fixed by rectorphp/rector-src#1850
Labels

Comments

@acrobat
Copy link
Contributor

acrobat commented Feb 20, 2022

Bug Report

Subject Details
Rector version 0.12.16
Installed as composer dependency

When converting a @ORM\Table annotation with uniqueConstraints option, this option is lost after the refactor. I debugged the issue to it's origin but wasn't sure where to submit fixes to as I needed a failing test in rector/rector-doctrine and the fix in rector/rector-src.

I was able to trace the issue back to this change: https://github.com/rectorphp/rector-src/pull/1766/files#diff-3f761daa31da7830aaefc8ad8ed701778a120a703ad4d205965ecb0f80c52ce8R32. But I'm not sure why is was specifically added for that option and if it can be removed again. If the array item is removed I get the expected result.

Minimal PHP Code Causing Issue

See https://getrector.org/demo/db6e1e00-8128-4647-b391-28e711f077e4

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="categories", uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})})
 */
class Category
{
    private $name;
}

Responsible rules

  • AnnotationToAttributeRector

Expected Behavior

<?php

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'categories', uniqueConstraints: [new ORM\UniqueConstraint(name: 'name_idx', columns: ['name'])])]
class Category
{
    private $name;
}
@acrobat acrobat added the bug label Feb 20, 2022
@samsonasik
Copy link
Member

I can't reproduce in unit tests in latest rector-doctrine repo, it adds the attribute:

+#[ORM\Entity]
+#[ORM\Table(name: 'categories')]
+#[ORM\UniqueConstraint(name: 'name_idx', columns: ['name'])]
+class Category

It possibly resolved in latest dev-main ? You may try:

composer config minimum-stability dev
composer config prefer-stable true
composer update rector/rector:dev-main

Demo page may show different result due to missing class. Feel free to creata failing test PR to rector-doctrine if you have different use case.

@acrobat
Copy link
Contributor Author

acrobat commented Feb 21, 2022

Hi @samsonasik, I think I have a different usecase as the uniqueConstraint is not used as a standalone attribute/annotation but as part of the table attribute/annotation.

I've added a failing test (atleast it failed locally) for it in rectorphp/rector-doctrine#82

@samsonasik
Copy link
Member

@acrobat thank you, if you feel the issue is on rector-src and can find a fix, you can create PR to rector-src as well, the Fixture should can be placed at https://github.com/rectorphp/rector-src/tree/main/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81 as well

@acrobat
Copy link
Contributor Author

acrobat commented Feb 21, 2022

@samsonasik I've tried to come up with a fix! rectorphp/rector-src#1850

acrobat added a commit to acrobat/rector-doctrine that referenced this issue Feb 23, 2022
samsonasik pushed a commit to rectorphp/rector-doctrine that referenced this issue Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants