Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ private function processFile(File $file, Configuration $configuration): FileProc

if ($fileProcessResult->getSystemErrors() !== []) {
$this->changedFilesDetector->invalidateFile($file->getFilePath());
} elseif (! $configuration->isDryRun() || ! $fileProcessResult->getFileDiff() instanceof FileDiff) {
} elseif (! $configuration->isDryRun() || ! $fileProcessResult->hasChanged()) {
// gate on the actual content change, not on FileDiff: a FileDiff also carries reported line changes
// that print identically, and such files would otherwise be re-processed on every dry run
// selective runs are safe to cache now — the key is scoped to the rule selection
$this->changedFilesDetector->cacheFile($file->getFilePath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ public function testCleanFileIsCachedAsUnchanged(): void
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
}

public function testDryRunCachesFileWhoseReportedChangesLeaveContentUnchanged(): void
{
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-import-names.php']);
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$changedFilesDetector = $this->make(ChangedFilesDetector::class);

$filePath = __DIR__ . '/Source/WithAliasedImportOfSameShortName.php';

// name importing reports line changes here, but the printed file is identical, so the diff is empty
$processResult = $applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true));

$fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false);
$this->assertCount(1, $fileDiffs);
$this->assertSame('', $fileDiffs[0]->getDiff());
$this->assertFalse($changedFilesDetector->hasFileChanged($filePath));

$changedFilesDetector->clear();
}

public function testDryRunDoesNotCacheFileWithPendingChanges(): void
{
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-max-changes.php']);
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$changedFilesDetector = $this->make(ChangedFilesDetector::class);

$filePath = __DIR__ . '/Source/WithTwoClosuresFirst.php';

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

$this->assertTrue($changedFilesDetector->hasFileChanged($filePath));

$changedFilesDetector->clear();
}

public function testOnlyRuleRunCachesUnderOwnScopeWithoutPoisoningFullRun(): void
{
$filePath = __DIR__ . '/Source/CleanFile.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\ApplicationFileProcessor\Source\Rules;

final class Money
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\ApplicationFileProcessor\Source\ValueObjects;

final class Money
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\ApplicationFileProcessor\Source;

use Rector\Tests\Application\ApplicationFileProcessor\Source\Rules\Money;
use Rector\Tests\Application\ApplicationFileProcessor\Source\ValueObjects\Money as MoneyValue;

final class WithAliasedImportOfSameShortName
{
public function rule(): Money
{
return new Money();
}

public function value(): MoneyValue
{
return new MoneyValue();
}
}
12 changes: 12 additions & 0 deletions tests/Application/ApplicationFileProcessor/config-import-names.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\StringableForToStringRector;

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