[PHP 5.5] Fix StringToClass name whilst import

This commit is contained in:
TomasVotruba 2020-05-13 09:57:21 +02:00
parent 6ae80bc07a
commit 87448dfa37
7 changed files with 98 additions and 2 deletions

View File

@ -108,6 +108,8 @@ final class NameImporter
return true;
}
// namespace <name>
// use <name>;
if ($this->isNamespaceOrUseImportName($name)) {
return true;
}

View File

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Rector\Legacy\Tests\Rector\FileSystem\FunctionToStaticMethodRector;
use Rector\Core\Testing\PHPUnit\AbstractFileSystemRectorTestCase;
use Rector\Legacy\Rector\Node\FunctionToStaticMethodRector;
use Rector\Legacy\Rector\FileSystem\FunctionToStaticMethodRector;
final class FunctionToStaticMethodRectorTest extends AbstractFileSystemRectorTestCase
{

View File

@ -14,6 +14,7 @@ use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Core\Util\StaticRectorStrings;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionClass;
/**
@ -117,7 +118,11 @@ PHP
return null;
}
return new ClassConstFetch(new FullyQualified($classLikeName), 'class');
$name = new FullyQualified($classLikeName);
/** @see \Rector\PostRector\Collector\UseNodesToAddCollector::isShortImported() */
$name->setAttribute(AttributeKey::FILE_INFO, $node->getAttribute(AttributeKey::FILE_INFO));
return new ClassConstFetch($name, 'class');
}
private function classLikeSensitiveExists(string $classLikeName): bool

View File

@ -0,0 +1,36 @@
<?php
namespace Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\FixtureImport;
class SkipImportOfJustReplacedClasses
{
public function run()
{
$class = 'Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\Source\SomeUser';
$user = new $class;
$anotherClass = 'Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\Source\SomeSecurity\SomeUser';
$user = new $anotherClass;
}
}
?>
-----
<?php
namespace Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\FixtureImport;
use Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\Source\SomeUser;
class SkipImportOfJustReplacedClasses
{
public function run()
{
$class = SomeUser::class;
$user = new $class;
$anotherClass = \Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\Source\SomeSecurity\SomeUser::class;
$user = new $anotherClass;
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector;
use Iterator;
use Rector\Core\Configuration\Option;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
final class ImportClassNameRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->setParameter(Option::AUTO_IMPORT_NAMES, true);
$this->doTestFile($file);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureImport');
}
protected function getRectorClass(): string
{
return StringClassNameToClassConstantRector::class;
}
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\Source\SomeSecurity;
final class SomeUser
{
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\Source;
final class SomeUser
{
}