[Naming] Fix imported trait names (#500)

This commit is contained in:
Tomas Votruba 2021-07-23 16:32:29 +02:00 committed by GitHub
parent e3a1f9399b
commit 9d51f3c47f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 152 additions and 0 deletions

View File

@ -18,6 +18,7 @@ use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeNameResolver\Error\InvalidNameNodeReporter;
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class NodeNameResolver
{
@ -74,6 +75,12 @@ final class NodeNameResolver
return $node;
}
// useful for looped imported names
$namespacedName = $node->getAttribute(AttributeKey::NAMESPACED_NAME);
if (is_string($namespacedName)) {
return $namespacedName;
}
if ($node instanceof MethodCall || $node instanceof StaticCall) {
if ($this->isCallOrIdentifier($node->name)) {
return null;

View File

@ -36,7 +36,11 @@ final class FullyQualifiedObjectType extends ObjectType
public function getShortNameNode(): Name
{
$name = new Name($this->getShortName());
// to avoid processing short name twice
$name->setAttribute(AttributeKey::VIRTUAL_NODE, true);
// keep original to avoid loss on while importing
$name->setAttribute(AttributeKey::NAMESPACED_NAME, $this->getClassName());
return $name;
}

View File

@ -564,3 +564,6 @@ parameters:
# phsptan waits for php-parser 4.12 release
- '#Call to an undefined method PhpParser\\Node\:\:isFinal\(\)#'
# complex
- '#Cognitive complexity for "Rector\\NodeNameResolver\\NodeNameResolver\:\:getName\(\)" is 10, keep it under 9#'

View File

@ -78,6 +78,7 @@ final class PhpFileProcessor implements FileProcessorInterface
continue;
}
// important to detect if file has changed
$this->tryCatchWrapper($file, function (File $file) use ($configuration): void {
$this->printFile($file, $configuration);
}, ApplicationPhase::PRINT());

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Prophecy\PhpUnit;
if (trait_exists('Prophecy\PhpUnit\ProphecyTrait')) {
return;
}
trait ProphecyTrait
{
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\DoubleImportedTraitUse;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DoubleImportedTraitUseTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/double_imported_trait_use.php';
}
}

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\DoubleImportedTraitUse\Fixture;
use PHPStan\Testing\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use stdClass;
final class AddArrayTrait extends TestCase
{
use ProphecyTrait;
private ObjectProphecy $client;
protected function setUp(): void
{
$this->client = $this->prophesize(stdClass::class);
}
/**
* @dataProvider invalidValues
*/
public function testInvalidValues(int | array $value): void
{
}
public function invalidValues()
{
yield 'Class without __toString not possible' => [1000];
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\DoubleImportedTraitUse\Fixture;
use PHPStan\Testing\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use stdClass;
final class AddArrayTrait extends TestCase
{
use ProphecyTrait;
private ObjectProphecy $client;
protected function setUp(): void
{
$this->client = $this->prophesize(stdClass::class);
}
/**
* @dataProvider invalidValues
* @param int $value
*/
public function testInvalidValues(int | array $value): void
{
}
public function invalidValues()
{
yield 'Class without __toString not possible' => [1000];
}
}
?>

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\PHPUnit\Rector\Class_\AddProphecyTraitRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$services = $containerConfigurator->services();
$services->set(AddProphecyTraitRector::class);
$services->set(AddArrayParamDocTypeRector::class);
};