fix importing of soon-to-be-existing classes

This commit is contained in:
TomasVotruba 2020-06-19 12:06:12 +02:00
parent 75b8d37e4b
commit e5e9abbcac
7 changed files with 44 additions and 27 deletions

3
.gitignore vendored
View File

@ -18,3 +18,6 @@ create-rector.yaml
/compiler/composer.lock
/compiler/vendor
/tmp
# testing
abz

View File

@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Entity;
final class EntityFactory
{
private SomeInterface $property;
public function create(SomeInterface $param): SomeInterface
{
}
}

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Entity;
interface SomeInterface
{
}

View File

@ -110,6 +110,7 @@ final class UseAddingPostRector extends AbstractPostRector
public function getPriority(): int
{
// must be after name importing
return 500;
}

View File

@ -29,10 +29,23 @@ final class FullyQualifiedNodeMapper implements PhpParserNodeMapperInterface
$fullyQualifiedName = $node->toString();
// is aliased?
if ($originalName !== $fullyQualifiedName && ! Strings::endsWith($fullyQualifiedName, '\\' . $originalName)) {
if ($this->isAliasedName($originalName, $fullyQualifiedName)) {
return new AliasedObjectType($originalName, $fullyQualifiedName);
}
return new FullyQualifiedObjectType($node->toString());
}
private function isAliasedName(string $originalName, string $fullyQualifiedName): bool
{
if ($originalName === '') {
return false;
}
if ($originalName === $fullyQualifiedName) {
return false;
}
return ! Strings::endsWith($fullyQualifiedName, '\\' . $originalName);
}
}

View File

@ -10,10 +10,21 @@ use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PSR4\Collector\RenamedClassesCollector;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
final class NameNodeMapper implements PhpParserNodeMapperInterface
{
/**
* @var RenamedClassesCollector
*/
private $renamedClassesCollector;
public function __construct(RenamedClassesCollector $renamedClassesCollector)
{
$this->renamedClassesCollector = $renamedClassesCollector;
}
public function getNodeType(): string
{
return Name::class;
@ -26,10 +37,22 @@ final class NameNodeMapper implements PhpParserNodeMapperInterface
{
$name = $node->toString();
if (ClassExistenceStaticHelper::doesClassLikeExist($name)) {
return new FullyQualifiedObjectType($node->toString());
if ($this->isExistingClass($name)) {
return new FullyQualifiedObjectType($name);
}
return new MixedType();
}
private function isExistingClass(string $name): bool
{
if (ClassExistenceStaticHelper::doesClassLikeExist($name)) {
return true;
}
// to be existing class names
$newClasses = $this->renamedClassesCollector->getOldToNewClasses();
return in_array($name, $newClasses, true);
}
}

View File

@ -161,6 +161,7 @@ final class FileProcessor
[$newStmts, $oldStmts, $oldTokens] = $this->tokensByFilePathStorage->getForFileInfo($smartFileInfo);
$this->currentFileInfoProvider->setCurrentStmt($newStmts);
$this->currentFileInfoProvider->setCurrentFileInfo($smartFileInfo);
$newStmts = $this->postFileProcessor->traverse($newStmts);