When working with Doctrine Extensions traits, annotations used in the trait must be imported in the class that use the trait.
Sample code:
namespace Acme;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
/**
* @ORM\Entity
*/
class Test
{
use TimestampableEntity;
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
}
Gedmo annotations are not used in the parent class but they are in the trait (see https://github.com/Atlantic18/DoctrineExtensions/blob/v2.3.9/lib/Gedmo/Timestampable/Traits/TimestampableEntity.php).
PHP CS Fixer remove the following line:
use Gedmo\Mapping\Annotation as Gedmo;
As a workaround, the annotation can be imported directly in the trait (it's done in the master branch for Doctrine extensions).
When you use an uptodate version of the Doctrine annotations library, you don't need to add such use statements on the class using the trait. It was a bug in the annotations library.
The right place to import the annotation namespace i indeed in the file defining the trait, as this is where you define the annotation as well.
Right. But it breaks existing applications that depend of the current stable version of Doctrine extensions or old version of Doctrine annotation. It's our case (we are upgrading to the last version but it could break other applications).
I'll close this bug if irrelevant.
Le 15 nov. 2014 à 11:36, Christophe Coevoet [email protected] a écrit :
When you use an uptodate version of the Doctrine annotations library, you don't need to add such use statements on the class using the trait. It was a bug in the annotations library.
The right place to import the annotation namespace i indeed in the file defining the trait, as this is where you define the annotation as well.—
Reply to this email directly or view it on GitHub.
but the PHP-CS-Fixer is technically right about removing them. Thus, it cannot know about the fact that one of the traits relies on it for the Doctrine annotations (it would require a very complex code specific to a Doctrine bug). The alternative is to skip the unused use fixer for your project until you can use a working version
It's what I've done for now. Closing.
Any solution to this in current 2.4 version? Thank you
Like mentioned in this discussion, the behavior of PHP CS Fixer is correct. If you're using Doctrine Annotation, please update it. If you're not using it and you faced a bug, please open a new issue.
I mean is there any fixer that removes unused use statements? Not related to Doctrine.
Yes: no_unused_imports.
Ah, thanks!
good thing there is a readme file... or website...
Good one ;)
I looked for "remove unused statements" but could not find any.
Most helpful comment
Yes:
no_unused_imports.