mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-26 20:53:31 +00:00
fix importing of soon-to-be-existing classes
This commit is contained in:
parent
75b8d37e4b
commit
e5e9abbcac
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -18,3 +18,6 @@ create-rector.yaml
|
|||
/compiler/composer.lock
|
||||
/compiler/vendor
|
||||
/tmp
|
||||
|
||||
# testing
|
||||
abz
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
final class EntityFactory
|
||||
{
|
||||
private SomeInterface $property;
|
||||
|
||||
public function create(SomeInterface $param): SomeInterface
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
interface SomeInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -110,6 +110,7 @@ final class UseAddingPostRector extends AbstractPostRector
|
|||
|
||||
public function getPriority(): int
|
||||
{
|
||||
// must be after name importing
|
||||
return 500;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user