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

Annotation imports are not parsed for nested traits #268

Open
villermen opened this issue Apr 9, 2019 · 3 comments
Open

Annotation imports are not parsed for nested traits #268

villermen opened this issue Apr 9, 2019 · 3 comments
Labels

Comments

@villermen
Copy link

Bug Report

When an entity uses a trait that itself also uses a trait with a doctrine annotation the mapper failes to recognize the imported annotation. Code example below if it's not clear.

Q A
BC Break no
Version 1.6.1

How to reproduce

Create the following class and traits using doctrine/orm:

// ClassA.php

use Doctrine\Orm\Mapping\Entity;

/**
 * @Entity
 */
class ClassA
{
    use TraitA;
}
// TraitA.php

trait TraitA
{
    use TraitB;
}
// TraitB.php

use Doctrine\ORM\Mapping\Column;

trait TraitB
{
    /**
     * @Column(name="column", type="string")
     */
    protected $column;
}

Trying to create a mapping for this setup will throw an AnnotationException:

[Semantical Error] The annotation "@Column" in property Search\Model\ClassA::$column was never imported. Did you maybe forget to add a "use" statement for this annotation?

This will fail on any Doctrine annotation that is used in TraitB but not imported in TraitA or ClassA.

I expect the @Column-annotation imported in TraitB to stay also be valid for ClassA where it ends up in.

@Ocramius Ocramius added the bug label Apr 9, 2019
@javaDeveloperKid
Copy link

javaDeveloperKid commented Jul 8, 2019

Having the same issue (in my case with Gedmo). Cannot use a basic fixer PhpCsFixer\Fixer\Import\NoUnusedImportsFixer because of it. Problem does not exist when annotation is imported inside first level trait.

@maximehuran
Copy link

I put this comment in my class to be able to import Gedmo without PHPCS error :

    /**
     * @phpstan-ignore-next-line
     *
     * Use Gedmo var to be able to import it due to issue with Doctrine Annnotations and Traits
     *
     * @see https://github.com/doctrine/annotations/issues/268
     *
     * @var Gedmo
     */

So my class is like this :

<?php

declare(strict_types=1);

namespace App\Entity

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 * @ORM\Table(name="app_myentity")
 */
class MyEntity implements MyEntityInterface
{
    /**
     * @phpstan-ignore-next-line
     *
     * Use Gedmo var to be able to import it due to issue with Doctrine Annnotations and Traits
     *
     * @see https://github.com/doctrine/annotations/issues/268
     *
     * @var Gedmo
     */
    use MyEntityTrait;
}

@KovalMax
Copy link

It could be solved with FQN for annotation or php8 attributes instead of annotation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants