php-cs-fixer

php-cs-fixer including vendor namespaces but not the bundle in src/


Starting with a simple entity class in a symfony 3.4 bundle AppBundle, php-cs-fixer seems to strip imports used by docstrings but ONLY when referring to AppBundle/ – namespaces in vendor/ seem to be safe

Starting with the following, simple entity class

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\EntityManager;
use AppBundle\Entity\AclObjectIdentities; // will be removed
use AppBundle\Entity\AclSecurityIdentities; // will be removed

class AclEntries
{
    /** @var AclObjectIdentities Object identity */
    private $objectIdentity;

    /** @var EntityManager Doctrine entity manager */
    private $em;
}

I run the php-cs-fixer command with some reasonable defaults

php-cs-fixer fix src/AppBundle/Entity/AclEntriesDEBUG.php --rules=@PSR2,@Symfony

The file gets cleaned up nicely, however all phpdoc imports to AppBundle are removed as unused

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\EntityManager;

class AclEntries
{
    /** @var AclObjectIdentities Object identity */
    private $objectIdentity;

    /** @var EntityManager Doctrine entity manager */
    private $em;
}

The expected behavior to me should be that "all imports are equal" and the "use AppBundle..." doesn't get stripped.


Solution

  • Import from same namespace you are currently in has no effect, as PHP autoimport it anyway. That's why PHP CS Fixer removed them. If you want to keep them even if they are not needed, remove no_unused_imports rule from your config, eg: php-cs-fixer fix src/AppBundle/Entity/AclEntriesDEBUG.php --rules=@PSR2,@Symfony,-no_unused_imports