mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 11:50:51 +00:00
[Naming] Fix imported trait names (#500)
This commit is contained in:
parent
e3a1f9399b
commit
9d51f3c47f
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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#'
|
||||
|
|
|
@ -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());
|
||||
|
|
13
stubs/Prophecy/PhpUnit/ProphecyTrait.php
Normal file
13
stubs/Prophecy/PhpUnit/ProphecyTrait.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Prophecy\PhpUnit;
|
||||
|
||||
if (trait_exists('Prophecy\PhpUnit\ProphecyTrait')) {
|
||||
return;
|
||||
}
|
||||
|
||||
trait ProphecyTrait
|
||||
{
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
};
|
Loading…
Reference in New Issue
Block a user