Import Names

Rector works with all class names as fully qualified by default. That way it knows the exact types even on just changed nodes. In the most projects, that's not a desired behavior, because short version with use statement is easier to read:

+use App\Some\Namespace\SomeClass;

-$object = new \App\Some\Namespace\SomeClass();
+$object = new SomeClass();


To import FQN like these, configure rector.php with:

$rectorConfig->importNames();


Single short classes are imported too:

+use DateTime;
-$someClass = \DateTime();
+$someClass = DateTime();


To keep those:

$rectorConfig->importShortClasses(false);

How to Remove Unused Imports?

To remove imports, use ECS with NoUnusedImportsFixerrule:

// ecs.php
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
    $ecsConfig->rule(NoUnusedImportsFixer::class);
};


Run it to remove unused imports:

vendor/bin/ecs check src --fix


That's it. Happy coding!