Skip to content

Commit

Permalink
use symfony fs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 3, 2023
1 parent c3b9f02 commit ee86121
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 43 deletions.
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use PHP_CodeSniffer\Fixer;

use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UnusedFunctionParameterSniff;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\PropertyDeclarationSniff;
Expand All @@ -13,6 +14,7 @@
use PhpCsFixer\WhitespacesFixerConfig;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\EasyCodingStandard\Application\Version\StaticVersionResolver;
use Symplify\EasyCodingStandard\Caching\Cache;
use Symplify\EasyCodingStandard\Caching\CacheFactory;
Expand Down Expand Up @@ -80,6 +82,7 @@
$services->load('Symplify\EasyCodingStandard\\', __DIR__ . '/../packages')
->exclude([__DIR__ . '/../packages/*/ValueObject/*']);

$services->set(Filesystem::class);
$services->set(Cache::class)
->factory([service(CacheFactory::class), 'create']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Symplify\EasyCodingStandard\Tests\ChangedFilesDetector\FileHashComputer;

use Symfony\Component\Filesystem\Filesystem;
use Symplify\EasyCodingStandard\Caching\FileHashComputer;
use Symplify\EasyCodingStandard\Kernel\EasyCodingStandardKernel;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileSystem;

final class FileHashComputerTest extends AbstractKernelTestCase
{
Expand All @@ -18,35 +18,35 @@ final class FileHashComputerTest extends AbstractKernelTestCase

private FileHashComputer $fileHashComputer;

private SmartFileSystem $smartFileSystem;
private Filesystem $filesystem;

protected function setUp(): void
{
$this->bootKernel(EasyCodingStandardKernel::class);

$this->fileHashComputer = $this->getService(FileHashComputer::class);
$this->smartFileSystem = $this->getService(SmartFileSystem::class);
$this->filesystem = $this->getService(Filesystem::class);
}

public function testInvalidateCacheOnConfigurationChange(): void
{
// A. create on another one with fixer
$this->smartFileSystem->copy(__DIR__ . '/Source/first_config.php', self::INCLUDED_CONFIG_FILE, true);
$this->filesystem->copy(__DIR__ . '/Source/first_config.php', self::INCLUDED_CONFIG_FILE, true);

$fileOneHash = $this->fileHashComputer->computeConfig(
__DIR__ . '/Fixture/config-including-another-one.php'
);

// B. create on another one with no fixer
$this->smartFileSystem->copy(__DIR__ . '/Source/empty_config.php', self::INCLUDED_CONFIG_FILE, true);
$this->filesystem->copy(__DIR__ . '/Source/empty_config.php', self::INCLUDED_CONFIG_FILE, true);

$fileTwoHash = $this->fileHashComputer->computeConfig(
__DIR__ . '/Fixture/config-including-another-one.php'
);

$this->assertNotSame($fileOneHash, $fileTwoHash);

$this->smartFileSystem->remove(self::INCLUDED_CONFIG_FILE);
$this->filesystem->remove(self::INCLUDED_CONFIG_FILE);
}

public function testPhpFileHash(): void
Expand Down
10 changes: 5 additions & 5 deletions packages/Caching/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace Symplify\EasyCodingStandard\Caching;

use Symfony\Component\Filesystem\Filesystem;
use Symplify\EasyCodingStandard\Caching\ValueObject\Storage\FileCacheStorage;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileSystem;

final class CacheFactory
{
public function __construct(
private readonly ParameterProvider $parameterProvider,
private readonly SmartFileSystem $smartFileSystem
private readonly Filesystem $fileSystem
) {
}

Expand All @@ -25,11 +25,11 @@ public function create(): Cache
$cacheDirectory = $this->parameterProvider->provideStringParameter(Option::CACHE_DIRECTORY);

// ensure cache directory exists
if (! $this->smartFileSystem->exists($cacheDirectory)) {
$this->smartFileSystem->mkdir($cacheDirectory);
if (! $this->fileSystem->exists($cacheDirectory)) {
$this->fileSystem->mkdir($cacheDirectory);
}

$fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->smartFileSystem);
$fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->fileSystem);

return new Cache($fileCacheStorage);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/Caching/ValueObject/Storage/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Symplify\EasyCodingStandard\Caching\ValueObject\Storage;

use Nette\Utils\Random;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\EasyCodingStandard\Caching\Exception\CachingException;
use Symplify\EasyCodingStandard\Caching\ValueObject\CacheFilePaths;
use Symplify\EasyCodingStandard\Caching\ValueObject\CacheItem;
use Symplify\SmartFileSystem\SmartFileSystem;

/**
* Inspired by
Expand All @@ -18,7 +18,7 @@ final class FileCacheStorage
{
public function __construct(
private readonly string $directory,
private readonly SmartFileSystem $smartFileSystem
private readonly Filesystem $fileSystem
) {
}

Expand All @@ -45,8 +45,8 @@ public function load(string $key, string $variableKey): ?string
public function save(string $key, string $variableKey, string $data): void
{
$cacheFilePaths = $this->getCacheFilePaths($key);
$this->smartFileSystem->mkdir($cacheFilePaths->getFirstDirectory());
$this->smartFileSystem->mkdir($cacheFilePaths->getSecondDirectory());
$this->fileSystem->mkdir($cacheFilePaths->getFirstDirectory());
$this->fileSystem->mkdir($cacheFilePaths->getSecondDirectory());

$tmpPath = sprintf('%s/%s.tmp', $this->directory, Random::generate());
$errorBefore = error_get_last();
Expand All @@ -65,17 +65,17 @@ public function save(string $key, string $variableKey, string $data): void
}

$variableFileContent = sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported);
$this->smartFileSystem->dumpFile($tmpPath, $variableFileContent);
$this->fileSystem->dumpFile($tmpPath, $variableFileContent);

$this->smartFileSystem->rename($tmpPath, $cacheFilePaths->getFilePath(), true);
$this->smartFileSystem->remove($tmpPath);
$this->fileSystem->rename($tmpPath, $cacheFilePaths->getFilePath(), true);
$this->fileSystem->remove($tmpPath);
}

public function clean(string $cacheKey): void
{
$cacheFilePaths = $this->getCacheFilePaths($cacheKey);

$this->smartFileSystem->remove([
$this->fileSystem->remove([
$cacheFilePaths->getFirstDirectory(),
$cacheFilePaths->getSecondDirectory(),
$cacheFilePaths->getFilePath(),
Expand All @@ -84,7 +84,7 @@ public function clean(string $cacheKey): void

public function clear(): void
{
$this->smartFileSystem->remove($this->directory);
$this->fileSystem->remove($this->directory);
}

private function getCacheFilePaths(string $key): CacheFilePaths
Expand Down
5 changes: 2 additions & 3 deletions packages/FixerRunner/Application/FixerFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Symplify\EasyCodingStandard\SnippetFormatter\Provider\CurrentParentFileInfoProvider;
use Symplify\EasyCodingStandard\ValueObject\Configuration;
use Symplify\EasyCodingStandard\ValueObject\Error\FileDiff;
use Symplify\SmartFileSystem\SmartFileSystem;
use Throwable;

/**
Expand Down Expand Up @@ -60,7 +59,7 @@ public function __construct(
private readonly Skipper $skipper,
private readonly DifferInterface $differ,
private readonly EasyCodingStandardStyle $easyCodingStandardStyle,
private readonly SmartFileSystem $smartFileSystem,
private readonly \Symfony\Component\Filesystem\Filesystem $filesystem,
private readonly CurrentParentFileInfoProvider $currentParentFileInfoProvider,
private readonly TargetFileInfoResolver $targetFileInfoResolver,
private readonly FileDiffFactory $fileDiffFactory,
Expand Down Expand Up @@ -120,7 +119,7 @@ public function processFile(SplFileInfo $fileInfo, Configuration $configuration)

$tokenGeneratedCode = $tokens->generateCode();
if ($configuration->isFixer()) {
$this->smartFileSystem->dumpFile($fileInfo->getRealPath(), $tokenGeneratedCode);
$this->filesystem->dumpFile($fileInfo->getRealPath(), $tokenGeneratedCode);
}

Tokens::clearCache();
Expand Down
11 changes: 3 additions & 8 deletions packages/FixerRunner/Parser/FileToTokensParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

namespace Symplify\EasyCodingStandard\FixerRunner\Parser;

use Nette\Utils\FileSystem;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use Symplify\SmartFileSystem\SmartFileSystem;

final class FileToTokensParser
{
public function __construct(
private readonly SmartFileSystem $smartFileSystem
) {
}

/**
* @return Tokens<Token>
*/
public function parseFromFilePath(string $filePath): Tokens
{
$fileContent = $this->smartFileSystem->readFile($filePath);
return Tokens::fromCode($fileContent);
$fileContents = FileSystem::read($filePath);
return Tokens::fromCode($fileContents);
}
}
5 changes: 2 additions & 3 deletions packages/SniffRunner/Application/SniffFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Symplify\EasyCodingStandard\ValueObject\Configuration;
use Symplify\EasyCodingStandard\ValueObject\Error\FileDiff;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
use Symplify\SmartFileSystem\SmartFileSystem;

/**
* @see \Symplify\EasyCodingStandard\Tests\Error\ErrorCollector\SniffFileProcessorTest
Expand All @@ -45,7 +44,7 @@ public function __construct(
private readonly FileFactory $fileFactory,
private readonly DifferInterface $differ,
private readonly SniffMetadataCollector $sniffMetadataCollector,
private readonly SmartFileSystem $smartFileSystem,
private readonly \Symfony\Component\Filesystem\Filesystem $filesystem,
private readonly FileDiffFactory $fileDiffFactory,
private readonly PrivatesAccessor $privatesAccessor,
array $sniffs
Expand Down Expand Up @@ -102,7 +101,7 @@ public function processFile(SplFileInfo $fileInfo, Configuration $configuration)
}

if ($configuration->isFixer()) {
$this->smartFileSystem->dumpFile($file->getFilename(), $this->fixer->getContents());
$this->filesystem->dumpFile($file->getFilename(), $this->fixer->getContents());
}

return $errorsAndDiffs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
use Symplify\EasyCodingStandard\ValueObject\Error\FileDiff;
use Symplify\PackageBuilder\Console\Formatter\ColorConsoleDiffFormatter;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

final class MarkdownSnippetFormatterApplication
{
public function __construct(
private readonly SnippetReporter $snippetReporter,
private readonly MarkdownSnippetFormatter $markdownSnippetFormatter,
private readonly SmartFileSystem $smartFileSystem,
private readonly \Symfony\Component\Filesystem\Filesystem $fileSystem,
private readonly SymfonyStyle $symfonyStyle,
private readonly ProcessedFileReporter $processedFileReporter,
private readonly DifferInterface $differ,
Expand Down Expand Up @@ -81,7 +80,7 @@ private function processFileInfoWithPattern(
return null;
}

$this->smartFileSystem->dumpFile($phpFileInfo->getPathname(), $fixedContent);
$this->fileSystem->dumpFile($phpFileInfo->getPathname(), $fixedContent);

$diff = $this->differ->diff($originalFileContents, $fixedContent);
$consoleFormattedDiff = $this->colorConsoleDiffFormatter->format($diff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Symplify\EasyCodingStandard\SnippetFormatter\ValueObject\SnippetPattern;
use Symplify\EasyCodingStandard\ValueObject\Configuration;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;
use Throwable;

/**
Expand Down Expand Up @@ -49,7 +48,7 @@ final class MarkdownSnippetFormatter
private const CLOSING = 'closing';

public function __construct(
private readonly SmartFileSystem $smartFileSystem,
private readonly \Symfony\Component\Filesystem\Filesystem $fileSystem,
private readonly FixerFileProcessor $fixerFileProcessor,
private readonly SniffFileProcessor $sniffFileProcessor,
private readonly CurrentParentFileInfoProvider $currentParentFileInfoProvider
Expand Down Expand Up @@ -94,7 +93,7 @@ private function fixContent(string $content, Configuration $configuration): stri

$fileContent = ltrim($content, PHP_EOL);

$this->smartFileSystem->dumpFile($temporaryFilePath, $fileContent);
$this->fileSystem->dumpFile($temporaryFilePath, $fileContent);
$temporaryFileInfo = new SmartFileInfo($temporaryFilePath);

try {
Expand All @@ -107,7 +106,7 @@ private function fixContent(string $content, Configuration $configuration): stri
$changedFileContent = $fileContent;
} finally {
// remove temporary temporaryFile
$this->smartFileSystem->remove($temporaryFilePath);
$this->fileSystem->remove($temporaryFilePath);
}

$changedFileContent = rtrim($changedFileContent, PHP_EOL) . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion src/FileSystem/PathNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function normalizePath(string $originalPath): string
$path = $originalPath;
}

$normalizedPath = str_replace('\\', '/', $path);
$normalizedPath = str_replace('\\', '/', (string) $path);
$path = Strings::replace($normalizedPath, self::TWO_AND_MORE_SLASHES_REGEX, '/');

$pathRoot = str_starts_with($path, '/') ? $directorySeparator : '';
Expand Down
6 changes: 5 additions & 1 deletion src/Testing/PHPUnit/AbstractCheckerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected function doTestFile(string $filePath): void
[$inputContents, $expectedContents] = Strings::split($fileContents, self::SPLIT_LINE_REGEX);
} else {
// no change, part before and after are the same
[$inputContents, $expectedContents] = [$fileContents, $fileContents];
$inputContents = $fileContents;
$expectedContents = $fileContents;
}

$inputFilePath = sys_get_temp_dir() . '/ecs_tests/' . md5((string) $inputContents) . '.php';
Expand Down Expand Up @@ -165,6 +166,9 @@ protected static function yieldFiles(string $directory, string $suffix = '*.php.
return $filePaths;
}

/**
* @deprecated
*/
private function doTestWrongToFixedFile(SplFileInfo $wrongFileInfo, string $fixedFile): void
{
$this->ensureSomeCheckersAreRegistered();
Expand Down

0 comments on commit ee86121

Please sign in to comment.