Skip to content

Commit

Permalink
[Windows] Utilize Nette\Utils\FileSystem instead of Symfony\Component…
Browse files Browse the repository at this point in the history
…\Filesystem\Filesystem to write file (#5514)

* [Windows] Utilize Nette\Utils\FileSystem instead of Symfony\Component\Filesystem\Filesystem on write() file

* fix

* roll

* fix

* recursive mkdir

* fix phpstan

* final touch: use copy instead of rename on FileCacheStorage

* Final touch: fix phpstan

* final touch: only change related to write/rename

* final touch: using Filsystem::delete() as wel as symfony->remove() is using rename() internally
  • Loading branch information
samsonasik authored Jan 28, 2024
1 parent e17f878 commit 1608ae8
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bin/add-phpstan-self-replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down
15 changes: 8 additions & 7 deletions src/Caching/ValueObject/Storage/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ public function save(string $key, string $variableKey, mixed $data): void
}

// for performance reasons we don't use SmartFileSystem
FileSystem::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported));
$renameSuccess = @\rename($tmpPath, $filePath);
if ($renameSuccess) {
FileSystem::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported), null);
$copySuccess = @\copy($tmpPath, $filePath);
@\unlink($tmpPath);

if ($copySuccess) {
return;
}

@\unlink($tmpPath);
if (\DIRECTORY_SEPARATOR === '/' || ! \file_exists($filePath)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}
Expand All @@ -91,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
Expand All @@ -101,7 +102,7 @@ private function processRemoveCacheFilePath(CacheFilePaths $cacheFilePaths): voi
return;
}

$this->filesystem->remove($filePath);
FileSystem::delete($filePath);
}

private function processRemoveEmptyDirectory(string $directory): void
Expand All @@ -114,7 +115,7 @@ private function processRemoveEmptyDirectory(string $directory): void
return;
}

$this->filesystem->remove($directory);
FileSystem::delete($directory);
}

private function isNotEmptyDirectory(string $directory): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/ConfigInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/CustomRuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Command/SetupCICommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/FileSystem/JsonFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 2 additions & 3 deletions src/PhpParser/Printer/FormatPerservingPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +15,6 @@
{
public function __construct(
private BetterStandardPrinter $betterStandardPrinter,
private Filesystem $filesystem
) {
}

Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Testing/Fixture/FixtureFileUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/Fixture/FixtureTempFileDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions tests/Util/FileHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 1608ae8

Please sign in to comment.