From 9c5b5221d0b44c05ae482106ea2871733bad8278 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 03:05:45 +0700 Subject: [PATCH 01/10] [Windows] Utilize Nette\Utils\FileSystem instead of Symfony\Component\Filesystem\Filesystem on write() file --- bin/add-phpstan-self-replace.php | 2 +- src/Caching/CacheFactory.php | 12 ++------ .../ValueObject/Storage/FileCacheStorage.php | 30 ++++++++++++------- src/Configuration/ConfigInitializer.php | 2 +- src/Console/Command/CustomRuleCommand.php | 2 +- src/Console/Command/SetupCICommand.php | 4 +-- src/FileSystem/JsonFileSystem.php | 2 +- .../Printer/FormatPerservingPrinter.php | 5 ++-- src/Testing/Fixture/FixtureFileUpdater.php | 2 +- src/Testing/Fixture/FixtureTempFileDumper.php | 2 +- .../PHPUnit/AbstractRectorTestCase.php | 2 +- .../FileHashComputer/FileHashComputerTest.php | 4 +-- .../FileHashComputer/Fixture/rector_temp.php | 10 +++++++ .../Fixture/rector_temp_equal.php | 10 +++++++ .../ValueObject/Storage/Source/0tcp3rwb4v.tmp | 6 ++++ .../ValueObject/Storage/Source/20jb2z91wm.tmp | 6 ++++ .../ValueObject/Storage/Source/cpiaeketfa.tmp | 6 ++++ .../ValueObject/Storage/Source/lmf598f314.tmp | 6 ++++ .../ValueObject/Storage/Source/qs3uzpe727.tmp | 6 ++++ .../ValueObject/Storage/Source/ta28gtu9if.tmp | 6 ++++ tests/Util/FileHasherTest.php | 9 ++---- 21 files changed, 94 insertions(+), 40 deletions(-) create mode 100644 tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php create mode 100644 tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php create mode 100644 tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp create mode 100644 tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp create mode 100644 tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp create mode 100644 tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp create mode 100644 tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp create mode 100644 tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp diff --git a/bin/add-phpstan-self-replace.php b/bin/add-phpstan-self-replace.php index e65922b0427..6fffe031f0e 100644 --- a/bin/add-phpstan-self-replace.php +++ b/bin/add-phpstan-self-replace.php @@ -15,6 +15,6 @@ $composerJson['replace']['phpstan/phpstan'] = $composerJson['require']['phpstan/phpstan']; $modifiedComposerJsonFileContents = Json::encode($composerJson, Json::PRETTY); -FileSystem::write(__DIR__ . '/../composer.json', $modifiedComposerJsonFileContents); +FileSystem::write(__DIR__ . '/../composer.json', $modifiedComposerJsonFileContents, null); echo 'Done!' . PHP_EOL; diff --git a/src/Caching/CacheFactory.php b/src/Caching/CacheFactory.php index 5277e982734..d780e5554af 100644 --- a/src/Caching/CacheFactory.php +++ b/src/Caching/CacheFactory.php @@ -8,15 +8,9 @@ use Rector\Caching\ValueObject\Storage\MemoryCacheStorage; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; -use Symfony\Component\Filesystem\Filesystem; final readonly class CacheFactory { - public function __construct( - private Filesystem $fileSystem - ) { - } - /** * @api config factory */ @@ -32,11 +26,11 @@ public function create(): Cache if ($cacheClass === FileCacheStorage::class) { // ensure cache directory exists - if (! $this->fileSystem->exists($cacheDirectory)) { - $this->fileSystem->mkdir($cacheDirectory); + if (! is_dir($cacheDirectory)) { + mkdir($cacheDirectory); } - $fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->fileSystem); + $fileCacheStorage = new FileCacheStorage($cacheDirectory); return new Cache($fileCacheStorage); } diff --git a/src/Caching/ValueObject/Storage/FileCacheStorage.php b/src/Caching/ValueObject/Storage/FileCacheStorage.php index ae0ee7d019c..f750c4e2bd8 100644 --- a/src/Caching/ValueObject/Storage/FileCacheStorage.php +++ b/src/Caching/ValueObject/Storage/FileCacheStorage.php @@ -19,8 +19,7 @@ final readonly class FileCacheStorage implements CacheStorageInterface { public function __construct( - private string $directory, - private \Symfony\Component\Filesystem\Filesystem $filesystem + private string $directory ) { } @@ -50,8 +49,17 @@ public function load(string $key, string $variableKey): mixed public function save(string $key, string $variableKey, mixed $data): void { $cacheFilePaths = $this->getCacheFilePaths($key); - $this->filesystem-> mkdir($cacheFilePaths->getFirstDirectory()); - $this->filesystem->mkdir($cacheFilePaths->getSecondDirectory()); + + $firstDirectory = $cacheFilePaths->getFirstDirectory(); + $secondDirectory = $cacheFilePaths->getSecondDirectory(); + + if (! is_dir($firstDirectory)) { + mkdir($firstDirectory); + } + + if (! is_dir($secondDirectory)) { + mkdir($secondDirectory); + } $filePath = $cacheFilePaths->getFilePath(); @@ -69,8 +77,8 @@ public function save(string $key, string $variableKey, mixed $data): void } // for performance reasons we don't use SmartFileSystem - FileSystem::write($tmpPath, \sprintf("filesystem->remove($this->directory); + FileSystem::delete($this->directory); } private function processRemoveCacheFilePath(CacheFilePaths $cacheFilePaths): void { $filePath = $cacheFilePaths->getFilePath(); - if (! $this->filesystem->exists($filePath)) { + if (! file_exists($filePath)) { return; } - $this->filesystem->remove($filePath); + FileSystem::delete($filePath); } private function processRemoveEmptyDirectory(string $directory): void { - if (! $this->filesystem->exists($directory)) { + if (! is_dir($directory)) { return; } @@ -114,7 +122,7 @@ private function processRemoveEmptyDirectory(string $directory): void return; } - $this->filesystem->remove($directory); + FileSystem::delete($directory); } private function isNotEmptyDirectory(string $directory): bool diff --git a/src/Configuration/ConfigInitializer.php b/src/Configuration/ConfigInitializer.php index 608895b1636..0822f0fb502 100644 --- a/src/Configuration/ConfigInitializer.php +++ b/src/Configuration/ConfigInitializer.php @@ -46,7 +46,7 @@ public function createConfig(string $projectDirectory): void $configContents = $this->replacePathsContents($configContents, $projectDirectory); - FileSystem::write($commonRectorConfigPath, $configContents); + FileSystem::write($commonRectorConfigPath, $configContents, null); $this->symfonyStyle->success('The config is added now. Re-run command to make Rector do the work!'); } diff --git a/src/Console/Command/CustomRuleCommand.php b/src/Console/Command/CustomRuleCommand.php index d6fd9f83c25..d72e753e02c 100644 --- a/src/Console/Command/CustomRuleCommand.php +++ b/src/Console/Command/CustomRuleCommand.php @@ -95,7 +95,7 @@ static function (string $answer): string { $newContent = $this->replaceNameVariable($rectorName, $fileInfo->getContents()); $newFilePath = $this->replaceNameVariable($rectorName, $fileInfo->getRelativePathname()); - FileSystem::write(getcwd() . '/' . $newFilePath, $newContent); + FileSystem::write(getcwd() . '/' . $newFilePath, $newContent, null); $generatedFilePaths[] = $newFilePath; } diff --git a/src/Console/Command/SetupCICommand.php b/src/Console/Command/SetupCICommand.php index 29ed815d541..a79d205723c 100644 --- a/src/Console/Command/SetupCICommand.php +++ b/src/Console/Command/SetupCICommand.php @@ -73,7 +73,7 @@ private function addGithubActionsWorkflow(string $currentRepository, string $tar '__CURRENT_REPOSITORY__' => $currentRepository, ]); - FileSystem::write($targetWorkflowFilePath, $workflowContents); + FileSystem::write($targetWorkflowFilePath, $workflowContents, null); $this->symfonyStyle->newLine(); $this->symfonyStyle->success('The ".github/workflows/rector.yaml" file was added'); @@ -96,7 +96,7 @@ private function addGithubActionsWorkflow(string $currentRepository, string $tar private function addGitlabFile(string $targetGitlabFilePath): void { $gitlabTemplate = FileSystem::read(__DIR__ . '/../../../templates/rector-gitlab-check.yaml'); - FileSystem::write($targetGitlabFilePath, $gitlabTemplate); + FileSystem::write($targetGitlabFilePath, $gitlabTemplate, null); $this->symfonyStyle->newLine(); $this->symfonyStyle->success('The "gitlab/rector.yaml" file was added'); diff --git a/src/FileSystem/JsonFileSystem.php b/src/FileSystem/JsonFileSystem.php index 19cb986da4a..13394743761 100644 --- a/src/FileSystem/JsonFileSystem.php +++ b/src/FileSystem/JsonFileSystem.php @@ -25,6 +25,6 @@ public static function readFilePath(string $filePath): array public static function writeFile(string $filePath, array $data): void { $json = Json::encode($data, Json::PRETTY); - FileSystem::write($filePath, $json); + FileSystem::write($filePath, $json, null); } } diff --git a/src/PhpParser/Printer/FormatPerservingPrinter.php b/src/PhpParser/Printer/FormatPerservingPrinter.php index 4166c777bd8..7053a879544 100644 --- a/src/PhpParser/Printer/FormatPerservingPrinter.php +++ b/src/PhpParser/Printer/FormatPerservingPrinter.php @@ -4,9 +4,9 @@ namespace Rector\PhpParser\Printer; +use Nette\Utils\FileSystem; use PhpParser\Node; use Rector\ValueObject\Application\File; -use Symfony\Component\Filesystem\Filesystem; /** * @see \Rector\Tests\PhpParser\Printer\FormatPerservingPrinterTest @@ -15,7 +15,6 @@ { public function __construct( private BetterStandardPrinter $betterStandardPrinter, - private Filesystem $filesystem ) { } @@ -46,6 +45,6 @@ public function printParsedStmstAndTokensToString(File $file): string public function dumpFile(string $filePath, string $newContent): void { - $this->filesystem->dumpFile($filePath, $newContent); + FileSystem::write($filePath, $newContent, null); } } diff --git a/src/Testing/Fixture/FixtureFileUpdater.php b/src/Testing/Fixture/FixtureFileUpdater.php index b846e7d54f5..b14c4d2f09a 100644 --- a/src/Testing/Fixture/FixtureFileUpdater.php +++ b/src/Testing/Fixture/FixtureFileUpdater.php @@ -22,7 +22,7 @@ public static function updateFixtureContent( $newOriginalContent = self::resolveNewFixtureContent($originalContent, $changedContent); - FileSystem::write($fixtureFilePath, $newOriginalContent); + FileSystem::write($fixtureFilePath, $newOriginalContent, null); } private static function resolveNewFixtureContent(string $originalContent, string $changedContent): string diff --git a/src/Testing/Fixture/FixtureTempFileDumper.php b/src/Testing/Fixture/FixtureTempFileDumper.php index 89d13379a90..f1de9f7eec0 100644 --- a/src/Testing/Fixture/FixtureTempFileDumper.php +++ b/src/Testing/Fixture/FixtureTempFileDumper.php @@ -22,7 +22,7 @@ public static function dump(string $fileContents, string $suffix = 'php'): strin $temporaryFileName = sys_get_temp_dir() . self::TEMP_FIXTURE_DIRECTORY . '/' . md5( $fileContents ) . '.' . $suffix; - FileSystem::write($temporaryFileName, $fileContents); + FileSystem::write($temporaryFileName, $fileContents, null); return $temporaryFileName; } diff --git a/src/Testing/PHPUnit/AbstractRectorTestCase.php b/src/Testing/PHPUnit/AbstractRectorTestCase.php index 714747c40e7..9a7310ebc9c 100644 --- a/src/Testing/PHPUnit/AbstractRectorTestCase.php +++ b/src/Testing/PHPUnit/AbstractRectorTestCase.php @@ -159,7 +159,7 @@ protected function doTestFile(string $fixtureFilePath): void } // write temp file - FileSystem::write($inputFilePath, $inputFileContents); + FileSystem::write($inputFilePath, $inputFileContents, null); $this->doTestFileMatchesExpectedContent( $inputFilePath, diff --git a/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php b/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php index c0e13a36f77..3dcaa4b73c8 100644 --- a/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php +++ b/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php @@ -34,7 +34,7 @@ public function testRectorPhpChanged(): void $this->bootFromConfigFiles([__DIR__ . '/Fixture/rector.php']); $newHashedFile = $this->fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - rename(__DIR__ . '/Fixture/rector_temp.php', __DIR__ . '/Fixture/rector.php'); + copy(__DIR__ . '/Fixture/rector_temp.php', __DIR__ . '/Fixture/rector.php'); $this->assertNotSame($newHashedFile, $hashedFile); } @@ -55,7 +55,7 @@ public function testRectorPhpNotChanged(): void $this->bootFromConfigFiles([__DIR__ . '/Fixture/rector.php']); $newHashedFile = $this->fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - rename(__DIR__ . '/Fixture/rector_temp_equal.php', __DIR__ . '/Fixture/rector.php'); + copy(__DIR__ . '/Fixture/rector_temp_equal.php', __DIR__ . '/Fixture/rector.php'); $this->assertSame($newHashedFile, $hashedFile); } diff --git a/tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php b/tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php new file mode 100644 index 00000000000..76975c2f816 --- /dev/null +++ b/tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php @@ -0,0 +1,10 @@ +rules([DeclareStrictTypesRector::class]); +}; diff --git a/tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php b/tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php new file mode 100644 index 00000000000..76975c2f816 --- /dev/null +++ b/tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php @@ -0,0 +1,10 @@ +rules([DeclareStrictTypesRector::class]); +}; diff --git a/tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp b/tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp new file mode 100644 index 00000000000..766f56708b2 --- /dev/null +++ b/tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp @@ -0,0 +1,6 @@ + 'TEST', + 'data' => 'file cached', +)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp b/tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp new file mode 100644 index 00000000000..766f56708b2 --- /dev/null +++ b/tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp @@ -0,0 +1,6 @@ + 'TEST', + 'data' => 'file cached', +)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp b/tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp new file mode 100644 index 00000000000..4ab89ff5d6d --- /dev/null +++ b/tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp @@ -0,0 +1,6 @@ + 'TEST', + 'data' => 'file cached with the same two first caracters', +)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp b/tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp new file mode 100644 index 00000000000..4ab89ff5d6d --- /dev/null +++ b/tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp @@ -0,0 +1,6 @@ + 'TEST', + 'data' => 'file cached with the same two first caracters', +)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp b/tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp new file mode 100644 index 00000000000..766f56708b2 --- /dev/null +++ b/tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp @@ -0,0 +1,6 @@ + 'TEST', + 'data' => 'file cached', +)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp b/tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp new file mode 100644 index 00000000000..4ab89ff5d6d --- /dev/null +++ b/tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp @@ -0,0 +1,6 @@ + 'TEST', + 'data' => 'file cached with the same two first caracters', +)); \ No newline at end of file diff --git a/tests/Util/FileHasherTest.php b/tests/Util/FileHasherTest.php index c5e73a47a30..14586b48038 100644 --- a/tests/Util/FileHasherTest.php +++ b/tests/Util/FileHasherTest.php @@ -4,20 +4,17 @@ namespace Rector\Tests\Util; +use Nette\Utils\FileSystem; use Rector\Testing\PHPUnit\AbstractLazyTestCase; use Rector\Util\FileHasher; -use Symfony\Component\Filesystem\Filesystem; final class FileHasherTest extends AbstractLazyTestCase { private FileHasher $fileHasher; - private Filesystem $filesystem; - protected function setUp(): void { $this->fileHasher = $this->make(FileHasher::class); - $this->filesystem = $this->make(Filesystem::class); } public function testHash(): void @@ -32,12 +29,12 @@ public function testHashFiles(): void $file = $dir . '/FileHasherTest-fixture.txt'; try { - $this->filesystem->dumpFile($file, 'some string'); + FileSystem::write($file, 'some string', null); $hash = $this->fileHasher->hashFiles([$file]); $this->assertSame('8df638f91bacc826bf50c04efd7df1b1', $hash); } finally { - $this->filesystem->remove($file); + FileSystem::delete($file); } } From 8edbf5fd9455f656f2537ce99f6c67acdad16d12 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 03:09:15 +0700 Subject: [PATCH 02/10] fix --- .../ValueObject/Storage/FileCacheStorage.php | 2 +- .../FileHashComputer/FileHashComputerTest.php | 12 ++++++------ .../Fixture/rector_rule_equals.php | 15 --------------- .../FileHashComputer/Fixture/rector_temp.php | 10 ---------- .../Fixture/rector_temp_equal.php | 10 ---------- .../Fixture/updated_rector_rule.php | 14 -------------- .../ValueObject/Storage/Source/0tcp3rwb4v.tmp | 6 ------ .../ValueObject/Storage/Source/20jb2z91wm.tmp | 6 ------ .../ValueObject/Storage/Source/cpiaeketfa.tmp | 6 ------ .../ValueObject/Storage/Source/lmf598f314.tmp | 6 ------ .../ValueObject/Storage/Source/qs3uzpe727.tmp | 6 ------ .../ValueObject/Storage/Source/ta28gtu9if.tmp | 6 ------ 12 files changed, 7 insertions(+), 92 deletions(-) delete mode 100644 tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php delete mode 100644 tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php delete mode 100644 tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php delete mode 100644 tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php delete mode 100644 tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp delete mode 100644 tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp delete mode 100644 tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp delete mode 100644 tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp delete mode 100644 tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp delete mode 100644 tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp diff --git a/src/Caching/ValueObject/Storage/FileCacheStorage.php b/src/Caching/ValueObject/Storage/FileCacheStorage.php index f750c4e2bd8..3407aa52b05 100644 --- a/src/Caching/ValueObject/Storage/FileCacheStorage.php +++ b/src/Caching/ValueObject/Storage/FileCacheStorage.php @@ -78,7 +78,7 @@ public function save(string $key, string $variableKey, mixed $data): void // for performance reasons we don't use SmartFileSystem FileSystem::write($tmpPath, \sprintf("fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - copy(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp.php'); - copy(__DIR__ . '/Fixture/updated_rector_rule.php', __DIR__ . '/Fixture/rector.php'); + rename(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp.php'); + rename(__DIR__ . '/Fixture/updated_rector_rule.php', __DIR__ . '/Fixture/rector.php'); SimpleParameterProvider::setParameter(Option::REGISTERED_RECTOR_RULES, null); $this->bootFromConfigFiles([__DIR__ . '/Fixture/rector.php']); $newHashedFile = $this->fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - copy(__DIR__ . '/Fixture/rector_temp.php', __DIR__ . '/Fixture/rector.php'); + rename(__DIR__ . '/Fixture/rector_temp.php', __DIR__ . '/Fixture/rector.php'); $this->assertNotSame($newHashedFile, $hashedFile); } @@ -47,15 +47,15 @@ public function testRectorPhpNotChanged(): void $hashedFile = $this->fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - copy(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp_equal.php'); - copy(__DIR__ . '/Fixture/rector_rule_equals.php', __DIR__ . '/Fixture/rector.php'); + rename(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp_equal.php'); + rename(__DIR__ . '/Fixture/rector_rule_equals.php', __DIR__ . '/Fixture/rector.php'); SimpleParameterProvider::setParameter(Option::REGISTERED_RECTOR_RULES, null); $this->bootFromConfigFiles([__DIR__ . '/Fixture/rector.php']); $newHashedFile = $this->fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - copy(__DIR__ . '/Fixture/rector_temp_equal.php', __DIR__ . '/Fixture/rector.php'); + rename(__DIR__ . '/Fixture/rector_temp_equal.php', __DIR__ . '/Fixture/rector.php'); $this->assertSame($newHashedFile, $hashedFile); } diff --git a/tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php b/tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php deleted file mode 100644 index 545b045cddc..00000000000 --- a/tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php +++ /dev/null @@ -1,15 +0,0 @@ -rules([ - - // only spaced/comment added, no need to clear cache - DeclareStrictTypesRector::class - - ]); -}; diff --git a/tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php b/tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php deleted file mode 100644 index 76975c2f816..00000000000 --- a/tests/Caching/Config/FileHashComputer/Fixture/rector_temp.php +++ /dev/null @@ -1,10 +0,0 @@ -rules([DeclareStrictTypesRector::class]); -}; diff --git a/tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php b/tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php deleted file mode 100644 index 76975c2f816..00000000000 --- a/tests/Caching/Config/FileHashComputer/Fixture/rector_temp_equal.php +++ /dev/null @@ -1,10 +0,0 @@ -rules([DeclareStrictTypesRector::class]); -}; diff --git a/tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php b/tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php deleted file mode 100644 index 06f08d7ead7..00000000000 --- a/tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php +++ /dev/null @@ -1,14 +0,0 @@ -rules([ - DeclareStrictTypesRector::class, - RemoveDeadStmtRector::class, - ]); -}; diff --git a/tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp b/tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp deleted file mode 100644 index 766f56708b2..00000000000 --- a/tests/Caching/ValueObject/Storage/Source/0tcp3rwb4v.tmp +++ /dev/null @@ -1,6 +0,0 @@ - 'TEST', - 'data' => 'file cached', -)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp b/tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp deleted file mode 100644 index 766f56708b2..00000000000 --- a/tests/Caching/ValueObject/Storage/Source/20jb2z91wm.tmp +++ /dev/null @@ -1,6 +0,0 @@ - 'TEST', - 'data' => 'file cached', -)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp b/tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp deleted file mode 100644 index 4ab89ff5d6d..00000000000 --- a/tests/Caching/ValueObject/Storage/Source/cpiaeketfa.tmp +++ /dev/null @@ -1,6 +0,0 @@ - 'TEST', - 'data' => 'file cached with the same two first caracters', -)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp b/tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp deleted file mode 100644 index 4ab89ff5d6d..00000000000 --- a/tests/Caching/ValueObject/Storage/Source/lmf598f314.tmp +++ /dev/null @@ -1,6 +0,0 @@ - 'TEST', - 'data' => 'file cached with the same two first caracters', -)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp b/tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp deleted file mode 100644 index 766f56708b2..00000000000 --- a/tests/Caching/ValueObject/Storage/Source/qs3uzpe727.tmp +++ /dev/null @@ -1,6 +0,0 @@ - 'TEST', - 'data' => 'file cached', -)); \ No newline at end of file diff --git a/tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp b/tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp deleted file mode 100644 index 4ab89ff5d6d..00000000000 --- a/tests/Caching/ValueObject/Storage/Source/ta28gtu9if.tmp +++ /dev/null @@ -1,6 +0,0 @@ - 'TEST', - 'data' => 'file cached with the same two first caracters', -)); \ No newline at end of file From f565705f83645548af268e98eb407405ece7856b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 03:15:01 +0700 Subject: [PATCH 03/10] roll --- .../Config/FileHashComputer/FileHashComputerTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php b/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php index 20aa99fdf7e..c0e13a36f77 100644 --- a/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php +++ b/tests/Caching/Config/FileHashComputer/FileHashComputerTest.php @@ -26,8 +26,8 @@ public function testRectorPhpChanged(): void $hashedFile = $this->fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - rename(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp.php'); - rename(__DIR__ . '/Fixture/updated_rector_rule.php', __DIR__ . '/Fixture/rector.php'); + copy(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp.php'); + copy(__DIR__ . '/Fixture/updated_rector_rule.php', __DIR__ . '/Fixture/rector.php'); SimpleParameterProvider::setParameter(Option::REGISTERED_RECTOR_RULES, null); @@ -47,8 +47,8 @@ public function testRectorPhpNotChanged(): void $hashedFile = $this->fileHashComputer->compute(__DIR__ . '/Fixture/rector.php'); - rename(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp_equal.php'); - rename(__DIR__ . '/Fixture/rector_rule_equals.php', __DIR__ . '/Fixture/rector.php'); + copy(__DIR__ . '/Fixture/rector.php', __DIR__ . '/Fixture/rector_temp_equal.php'); + copy(__DIR__ . '/Fixture/rector_rule_equals.php', __DIR__ . '/Fixture/rector.php'); SimpleParameterProvider::setParameter(Option::REGISTERED_RECTOR_RULES, null); From db5907993b5d72f031c99168a4d6c72d9ce616a7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 03:15:08 +0700 Subject: [PATCH 04/10] fix --- .../Fixture/rector_rule_equals.php | 15 +++++++++++++++ .../Fixture/updated_rector_rule.php | 14 ++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php create mode 100644 tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php diff --git a/tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php b/tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php new file mode 100644 index 00000000000..545b045cddc --- /dev/null +++ b/tests/Caching/Config/FileHashComputer/Fixture/rector_rule_equals.php @@ -0,0 +1,15 @@ +rules([ + + // only spaced/comment added, no need to clear cache + DeclareStrictTypesRector::class + + ]); +}; diff --git a/tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php b/tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php new file mode 100644 index 00000000000..06f08d7ead7 --- /dev/null +++ b/tests/Caching/Config/FileHashComputer/Fixture/updated_rector_rule.php @@ -0,0 +1,14 @@ +rules([ + DeclareStrictTypesRector::class, + RemoveDeadStmtRector::class, + ]); +}; From 7c62f3d6ef9addeb81ab072952b7fc9d2014d294 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 03:20:12 +0700 Subject: [PATCH 05/10] recursive mkdir --- src/Caching/ValueObject/Storage/FileCacheStorage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Caching/ValueObject/Storage/FileCacheStorage.php b/src/Caching/ValueObject/Storage/FileCacheStorage.php index 3407aa52b05..bc3fab8ba1f 100644 --- a/src/Caching/ValueObject/Storage/FileCacheStorage.php +++ b/src/Caching/ValueObject/Storage/FileCacheStorage.php @@ -54,11 +54,11 @@ public function save(string $key, string $variableKey, mixed $data): void $secondDirectory = $cacheFilePaths->getSecondDirectory(); if (! is_dir($firstDirectory)) { - mkdir($firstDirectory); + mkdir($firstDirectory, 0777, true); } if (! is_dir($secondDirectory)) { - mkdir($secondDirectory); + mkdir($secondDirectory, 0777, true); } $filePath = $cacheFilePaths->getFilePath(); From 197f2ba1520505cd2e1a240f4ffc11a7a1bd07d3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 03:22:27 +0700 Subject: [PATCH 06/10] fix phpstan --- tests/Caching/ValueObject/Storage/FileCacheStorageTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php b/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php index 54558b57418..be1c6770769 100644 --- a/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php +++ b/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; use Rector\Caching\ValueObject\Storage\FileCacheStorage; use Rector\Testing\PHPUnit\AbstractLazyTestCase; -use Symfony\Component\Filesystem\Filesystem; final class FileCacheStorageTest extends AbstractLazyTestCase { @@ -17,7 +16,7 @@ protected function setUp(): void { parent::setUp(); - $this->fileCacheStorage = new FileCacheStorage(__DIR__ . '/Source', new Filesystem()); + $this->fileCacheStorage = new FileCacheStorage(__DIR__ . '/Source'); } #[DoesNotPerformAssertions] From 383b220ae120928bd0ac5820da92dfab2b4c490d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 03:38:53 +0700 Subject: [PATCH 07/10] final touch: use copy instead of rename on FileCacheStorage --- src/Caching/ValueObject/Storage/FileCacheStorage.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Caching/ValueObject/Storage/FileCacheStorage.php b/src/Caching/ValueObject/Storage/FileCacheStorage.php index bc3fab8ba1f..9a199b7e4a2 100644 --- a/src/Caching/ValueObject/Storage/FileCacheStorage.php +++ b/src/Caching/ValueObject/Storage/FileCacheStorage.php @@ -78,12 +78,13 @@ public function save(string $key, string $variableKey, mixed $data): void // for performance reasons we don't use SmartFileSystem FileSystem::write($tmpPath, \sprintf(" Date: Mon, 29 Jan 2024 03:43:52 +0700 Subject: [PATCH 08/10] Final touch: fix phpstan --- phpstan.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index be449492ccd..6c1ef8459ab 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -146,7 +146,7 @@ parameters: paths: - src/Caching/ValueObject/Storage/FileCacheStorage.php - - message: '#"@\\rename(.*?)" is forbidden to use#' + message: '#"@\\copy(.*?)" is forbidden to use#' paths: - src/Caching/ValueObject/Storage/FileCacheStorage.php - From 40733f2c0e6767fdc1d90474e5ff3379c378ffa5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 05:28:44 +0700 Subject: [PATCH 09/10] final touch: only change related to write/rename --- src/Caching/CacheFactory.php | 12 ++++++--- .../ValueObject/Storage/FileCacheStorage.php | 26 +++++++------------ .../Storage/FileCacheStorageTest.php | 3 ++- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Caching/CacheFactory.php b/src/Caching/CacheFactory.php index d780e5554af..5277e982734 100644 --- a/src/Caching/CacheFactory.php +++ b/src/Caching/CacheFactory.php @@ -8,9 +8,15 @@ use Rector\Caching\ValueObject\Storage\MemoryCacheStorage; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; +use Symfony\Component\Filesystem\Filesystem; final readonly class CacheFactory { + public function __construct( + private Filesystem $fileSystem + ) { + } + /** * @api config factory */ @@ -26,11 +32,11 @@ public function create(): Cache if ($cacheClass === FileCacheStorage::class) { // ensure cache directory exists - if (! is_dir($cacheDirectory)) { - mkdir($cacheDirectory); + if (! $this->fileSystem->exists($cacheDirectory)) { + $this->fileSystem->mkdir($cacheDirectory); } - $fileCacheStorage = new FileCacheStorage($cacheDirectory); + $fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->fileSystem); return new Cache($fileCacheStorage); } diff --git a/src/Caching/ValueObject/Storage/FileCacheStorage.php b/src/Caching/ValueObject/Storage/FileCacheStorage.php index 9a199b7e4a2..027ee43dcda 100644 --- a/src/Caching/ValueObject/Storage/FileCacheStorage.php +++ b/src/Caching/ValueObject/Storage/FileCacheStorage.php @@ -19,7 +19,8 @@ final readonly class FileCacheStorage implements CacheStorageInterface { public function __construct( - private string $directory + private string $directory, + private \Symfony\Component\Filesystem\Filesystem $filesystem ) { } @@ -49,17 +50,8 @@ public function load(string $key, string $variableKey): mixed public function save(string $key, string $variableKey, mixed $data): void { $cacheFilePaths = $this->getCacheFilePaths($key); - - $firstDirectory = $cacheFilePaths->getFirstDirectory(); - $secondDirectory = $cacheFilePaths->getSecondDirectory(); - - if (! is_dir($firstDirectory)) { - mkdir($firstDirectory, 0777, true); - } - - if (! is_dir($secondDirectory)) { - mkdir($secondDirectory, 0777, true); - } + $this->filesystem-> mkdir($cacheFilePaths->getFirstDirectory()); + $this->filesystem->mkdir($cacheFilePaths->getSecondDirectory()); $filePath = $cacheFilePaths->getFilePath(); @@ -100,22 +92,22 @@ public function clean(string $key): void public function clear(): void { - FileSystem::delete($this->directory); + $this->filesystem->remove($this->directory); } private function processRemoveCacheFilePath(CacheFilePaths $cacheFilePaths): void { $filePath = $cacheFilePaths->getFilePath(); - if (! file_exists($filePath)) { + if (! $this->filesystem->exists($filePath)) { return; } - FileSystem::delete($filePath); + $this->filesystem->remove($filePath); } private function processRemoveEmptyDirectory(string $directory): void { - if (! is_dir($directory)) { + if (! $this->filesystem->exists($directory)) { return; } @@ -123,7 +115,7 @@ private function processRemoveEmptyDirectory(string $directory): void return; } - FileSystem::delete($directory); + $this->filesystem->remove($directory); } private function isNotEmptyDirectory(string $directory): bool diff --git a/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php b/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php index be1c6770769..54558b57418 100644 --- a/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php +++ b/tests/Caching/ValueObject/Storage/FileCacheStorageTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; use Rector\Caching\ValueObject\Storage\FileCacheStorage; use Rector\Testing\PHPUnit\AbstractLazyTestCase; +use Symfony\Component\Filesystem\Filesystem; final class FileCacheStorageTest extends AbstractLazyTestCase { @@ -16,7 +17,7 @@ protected function setUp(): void { parent::setUp(); - $this->fileCacheStorage = new FileCacheStorage(__DIR__ . '/Source'); + $this->fileCacheStorage = new FileCacheStorage(__DIR__ . '/Source', new Filesystem()); } #[DoesNotPerformAssertions] From 93a667cd9e42bc7c74bca76910daed020de2f74a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 Jan 2024 05:34:16 +0700 Subject: [PATCH 10/10] final touch: using Filsystem::delete() as wel as symfony->remove() is using rename() internally --- src/Caching/ValueObject/Storage/FileCacheStorage.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Caching/ValueObject/Storage/FileCacheStorage.php b/src/Caching/ValueObject/Storage/FileCacheStorage.php index 027ee43dcda..94a4172cf8f 100644 --- a/src/Caching/ValueObject/Storage/FileCacheStorage.php +++ b/src/Caching/ValueObject/Storage/FileCacheStorage.php @@ -92,7 +92,7 @@ public function clean(string $key): void public function clear(): void { - $this->filesystem->remove($this->directory); + FileSystem::delete($this->directory); } private function processRemoveCacheFilePath(CacheFilePaths $cacheFilePaths): void @@ -102,7 +102,7 @@ private function processRemoveCacheFilePath(CacheFilePaths $cacheFilePaths): voi return; } - $this->filesystem->remove($filePath); + FileSystem::delete($filePath); } private function processRemoveEmptyDirectory(string $directory): void @@ -115,7 +115,7 @@ private function processRemoveEmptyDirectory(string $directory): void return; } - $this->filesystem->remove($directory); + FileSystem::delete($directory); } private function isNotEmptyDirectory(string $directory): bool