Php-cs-fixer: [Bug] Unused use and doctrine annotation

Created on 23 Jul 2018  路  4Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

When having a class A using a trait B, in this trait, it's using another trait C relying on @Gedmo annotation.

The use is detected as "not used" and thus, removed causing the application to crash.

PS: doctrine/annotations is v1.6.0 and tried 1.6.x-dev because read that old version of doctrine/annotations can cause this (https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/729)

After:

(1/1) AnnotationException
[Semantical Error] The annotation "@Gedmo\Timestampable" in property Yprox\Model\Site::$createdAt was never imported. Did you maybe forget to add a "use" statement for this annotation?

The PHP version you are using ($ php -v):

7.2.7

PHP CS Fixer version you are using ($ php-cs-fixer -V):

v2.12.2

The command you use to run PHP CS Fixer:

php-cs-fixer fix

The configuration file you are using, if any:

<?php

$finder = PhpCsFixer\Finder::create()
    ->in([__DIR__.'/src'])
    ->exclude(['Tests'])
;

return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@Symfony' => true,
        'no_unused_imports' => true,
        'array_syntax' => [
            'syntax' => 'short',
        ],
        'binary_operator_spaces' => [
            'align_double_arrow' => true,
            'align_equals' => true
        ],
        'no_unreachable_default_argument_value' => false,
        'braces' => [
            'allow_single_line_closure' => true,
        ],
        'heredoc_to_nowdoc' => false,
        'phpdoc_summary' => false,
        'yoda_style' => false,
        'native_constant_invocation' => true,
        'native_function_invocation' => [
            'include' => ['@compiler_optimized'],
            'scope'   => 'namespaced',
        ],
        'no_superfluous_phpdoc_tags' => true,
    ])
    ->setFinder($finder)
;

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
namespace Yprox\Model;

use Gedmo\Mapping\Annotation as Gedmo;
...

/**
 * @ORM\Entity(repositoryClass=SiteRepository::class)
 * @ORM\Table(name="site", indexes={@ORM\Index(name="host_idx", columns={"host"})})
 * @UniqueEntity("host")
 * @UniqueEntity("zohoPlanCode")
 */
class Site implements ThemeOwnerInterface, LockableInterface
{
    use Translatable;
    use InheritableEntity;
} 
namespace Yprox\Model\Site\Ancestor;

use Doctrine\ORM\Mapping as ORM;
use Yprox\Traits\TimestampableEntity;

trait InheritableEntity
{
    use TimestampableEntity;
} 
namespace Yprox\Traits;

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

trait TimestampableEntity
{
    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime", name="created_at")
     */
    protected $createdAt;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime", name="updated_at")
     */
    protected $updatedAt;
  • with unexpected changes applied when running PHP CS Fixer:
namespace Yprox\Model;

/**
 * @ORM\Entity(repositoryClass=SiteRepository::class)
 * @ORM\Table(name="site", indexes={@ORM\Index(name="host_idx", columns={"host"})})
 * @UniqueEntity("host")
 * @UniqueEntity("zohoPlanCode")
 */
class Site implements ThemeOwnerInterface, LockableInterface
{
    use Translatable;
    use InheritableEntity;
}
  • with the changes you expected instead:
no changes
topiannotation

Most helpful comment

For future reference there is an opened issue in doctrine for this: https://github.com/doctrine/annotations/issues/81

All 4 comments

Hi and thanks for reporting @tristanbes ,

I'm not 100% sure we can fix this in a easy way, but I like to hear @julienfalque on this topic :)
(maybe importing the annotation in the traits and not in the entity class would work?)

Nope, importing the annotation in the traits and not in the entity class is not working. I tried that :D

IMO PHP CS Fixer is right about removing the use statement. In #729 the discussion mentions that not handling the one in the trait itself was a bug in doctrine/annotation that should have been fixed. If not, I'd suggest opening a new issue on the related repository.

For future reference there is an opened issue in doctrine for this: https://github.com/doctrine/annotations/issues/81

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grachevko picture grachevko  路  3Comments

EvgenyOrekhov picture EvgenyOrekhov  路  3Comments

OskarStark picture OskarStark  路  3Comments

aidantwoods picture aidantwoods  路  3Comments

EvgenyOrekhov picture EvgenyOrekhov  路  3Comments