Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ private function importNameAndCollectNewUseStatement(
// make use of existing use import
$nameInUse = $this->resolveNameInUse($fullyQualified, $currentUses);
if ($nameInUse instanceof Name) {
// already written with this name, nothing to change
$originalName = $fullyQualified->getAttribute(AttributeKey::ORIGINAL_NAME);
if ($originalName instanceof Name && $originalName->toString() === $nameInUse->toString()) {
return null;
}

$nameInUse->setAttribute(AttributeKey::NAMESPACED_NAME, $fullyQualified->toString());
return $nameInUse;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\ApplicationFileProcessor;

use Rector\Application\ApplicationFileProcessor;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\ValueObject\Configuration;

final class ImportNamesCacheTest extends AbstractLazyTestCase
{
private ApplicationFileProcessor $applicationFileProcessor;

private ChangedFilesDetector $changedFilesDetector;

protected function setUp(): void
{
parent::setUp();

self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-import-names.php']);

$this->applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$this->changedFilesDetector = $this->make(ChangedFilesDetector::class);
$this->changedFilesDetector->clear();
}

public function testShortNameNextToSameShortNameAliasIsCachedAsUnchanged(): void
{
$filePath = __DIR__ . '/Source/ImportedNextToSameShortNameAlias.php';

$this->applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true));

$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\ApplicationFileProcessor\Source;

use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Expr\AssignOp\Plus as AssignPlus;

final class ImportedNextToSameShortNameAlias
{
public function run(Plus $plus, AssignPlus $assignPlus): void
{
}
}
10 changes: 10 additions & 0 deletions tests/Application/ApplicationFileProcessor/config-import-names.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->cacheDirectory(sys_get_temp_dir() . '/_rector_import_names_test');
$rectorConfig->importNames();
};
Loading