Skip to content

Commit

Permalink
Remove SmartFileInfo and use string paths instead (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jan 3, 2023
1 parent 3aab2d0 commit fdddae9
Show file tree
Hide file tree
Showing 30 changed files with 190 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,50 @@

final class ChangedFilesDetectorTest extends AbstractKernelTestCase
{
private SplFileInfo $fileInfo;
private string $filePath;

private ChangedFilesDetector $changedFilesDetector;

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

$this->fileInfo = new SplFileInfo(__DIR__ . '/Source/OneClass.php');
$this->filePath = __DIR__ . '/Source/OneClass.php';

$this->changedFilesDetector = $this->getService(ChangedFilesDetector::class);
$this->changedFilesDetector->changeConfigurationFile(__DIR__ . '/Source/easy-coding-standard.php');
}

public function testAddFile(): void
{
$this->assertFileHasChanged($this->fileInfo);
$this->assertFileHasChanged($this->fileInfo);
$this->assertFileHasChanged($this->filePath);
$this->assertFileHasChanged($this->filePath);
}

public function testHasFileChanged(): void
{
$this->changedFilesDetector->addFileInfo($this->fileInfo);
$this->changedFilesDetector->addFilePath($this->filePath);

$this->assertFileHasNotChanged($this->fileInfo);
$this->assertFileHasNotChanged($this->filePath);
}

public function testInvalidateCacheOnConfigurationChange(): void
{
$this->changedFilesDetector->addFileInfo($this->fileInfo);
$this->assertFileHasNotChanged($this->fileInfo);
$this->changedFilesDetector->addFilePath($this->filePath);
$this->assertFileHasNotChanged($this->filePath);

$this->changedFilesDetector->changeConfigurationFile(__DIR__ . '/Source/another-configuration.php');

$this->assertFileHasChanged($this->fileInfo);
$this->assertFileHasChanged($this->filePath);
}

private function assertFileHasChanged(SplFileInfo $fileInfo): void
private function assertFileHasChanged(string $filePath): void
{
$this->assertTrue($this->changedFilesDetector->hasFileInfoChanged($fileInfo));
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}

private function assertFileHasNotChanged(SplFileInfo $fileInfo): void
private function assertFileHasNotChanged(string $filePath): void
{
$this->assertFalse($this->changedFilesDetector->hasFileInfoChanged($fileInfo));
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
}
}
3 changes: 1 addition & 2 deletions packages-tests/Skipper/Skipper/Skip/SkipSkipperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symplify\EasyCodingStandard\Tests\Skipper\Skipper\Skip\Source\NotSkippedClass;
use Symplify\EasyCodingStandard\Tests\Skipper\Skipper\Skip\Source\SomeClassToSkip;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class SkipSkipperTest extends AbstractKernelTestCase
{
Expand All @@ -31,7 +30,7 @@ protected function setUp(): void
*/
public function test(string $element, string $filePath, bool $expectedSkip): void
{
$resolvedSkip = $this->skipper->shouldSkipElementAndFileInfo($element, new SmartFileInfo($filePath));
$resolvedSkip = $this->skipper->shouldSkipElementAndFilePath($element, $filePath);
$this->assertSame($expectedSkip, $resolvedSkip);
}

Expand Down
4 changes: 1 addition & 3 deletions packages-tests/Skipper/Skipper/Skipper/SkipperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ protected function setUp(): void
*/
public function testSkipFileInfo(string $filePath, bool $expectedSkip): void
{
$smartFileInfo = new SmartFileInfo($filePath);

$resultSkip = $this->skipper->shouldSkipFileInfo($smartFileInfo);
$resultSkip = $this->skipper->shouldSkipFilePath($filePath);
$this->assertSame($expectedSkip, $resultSkip);
}

Expand Down
2 changes: 1 addition & 1 deletion packages-tests/SniffRunner/Application/FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp(): void

$fileFactory = $this->getService(FileFactory::class);

$this->file = $fileFactory->createFromFileInfo(new SmartFileInfo(__DIR__ . '/FixerSource/SomeFile.php'));
$this->file = $fileFactory->createFromFile(__DIR__ . '/FixerSource/SomeFile.php');
$this->fixer = $this->getService(Fixer::class);
}

Expand Down
4 changes: 2 additions & 2 deletions packages-tests/SniffRunner/File/FileFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ protected function setUp(): void

public function test(): void
{
$fileInfo = new SmartFileInfo(__DIR__ . '/FileFactorySource/SomeFile.php');
$file = $this->fileFactory->createFromFileInfo($fileInfo);
$file = $this->fileFactory->createFromFile(__DIR__ . '/FileFactorySource/SomeFile.php');

$this->assertInstanceOf(File::class, $file);
$this->assertInstanceOf(PhpCodeSnifferFile::class, $file);
$this->assertInstanceOf(Fixer::class, $file->fixer);
Expand Down
5 changes: 2 additions & 3 deletions packages-tests/SniffRunner/ValueObject/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public function test(): void
$this->bootKernel(EasyCodingStandardKernel::class);

$fileFactory = $this->getService(FileFactory::class);
$fileInfo = new SplFileInfo(__DIR__ . '/FileSource/SomeFile.php');

$file = $fileFactory->createFromFileInfo($fileInfo);
$file->processWithTokenListenersAndFileInfo([], $fileInfo, []);
$file = $fileFactory->createFromFile(__DIR__ . '/FileSource/SomeFile.php');
$file->processWithTokenListenersAndFilePath([], __DIR__ . '/FileSource/SomeFile.php', []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test(SmartFileInfo $fixtureFileInfo): void
$configuration = new Configuration(true);

$changedContent = $this->markdownSnippetFormatter->format(
$inputAndExpectedFileInfos->getInputFileInfo(),
$inputAndExpectedFileInfos->getInputFileInfo()->getRealPath(),
$configuration
);

Expand Down
21 changes: 10 additions & 11 deletions packages/Caching/ChangedFilesDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Symplify\EasyCodingStandard\Caching;

use SplFileInfo;
use Symplify\EasyCodingStandard\FileSystem\StaticRelativeFilePathHelper;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -37,24 +36,24 @@ public function changeConfigurationFile(string $configurationFile): void
$this->storeConfigurationDataHash($this->fileHashComputer->computeConfig($configurationFile));
}

public function addFileInfo(SplFileInfo $fileInfo): void
public function addFilePath(string $filePath): void
{
$cacheKey = $this->fileInfoToKey($fileInfo);
$currentValue = $this->fileHashComputer->compute($fileInfo->getRealPath());
$cacheKey = $this->filePathToKey($filePath);
$currentValue = $this->fileHashComputer->compute($filePath);
$this->cache->save($cacheKey, self::FILE_HASH, $currentValue);
}

public function invalidateFileInfo(SplFileInfo $fileInfo): void
public function invalidateFilePath(string $filePath): void
{
$cacheKey = $this->fileInfoToKey($fileInfo);
$cacheKey = $this->filePathToKey($filePath);
$this->cache->clean($cacheKey);
}

public function hasFileInfoChanged(SplFileInfo $fileInfo): bool
public function hasFileChanged(string $filePath): bool
{
$newFileHash = $this->fileHashComputer->compute($fileInfo->getRealPath());
$newFileHash = $this->fileHashComputer->compute($filePath);

$cacheKey = $this->fileInfoToKey($fileInfo);
$cacheKey = $this->filePathToKey($filePath);
$cachedValue = $this->cache->load($cacheKey, self::FILE_HASH);

return $newFileHash !== $cachedValue;
Expand Down Expand Up @@ -92,9 +91,9 @@ private function storeConfigurationDataHash(string $configurationHash): void
$this->cache->save(self::CONFIGURATION_HASH_KEY, self::FILE_HASH, $configurationHash);
}

private function fileInfoToKey(SplFileInfo $fileInfo): string
private function filePathToKey(string $filePath): string
{
$relativeFilePath = StaticRelativeFilePathHelper::resolveFromCwd($fileInfo->getRealPath());
$relativeFilePath = StaticRelativeFilePathHelper::resolveFromCwd($filePath);

return sha1($relativeFilePath);
}
Expand Down
47 changes: 21 additions & 26 deletions packages/FixerRunner/Application/FixerFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Symplify\EasyCodingStandard\FixerRunner\Parser\FileToTokensParser;
use Symplify\EasyCodingStandard\Parallel\ValueObject\Bridge;
use Symplify\EasyCodingStandard\Skipper\Skipper\Skipper;
use Symplify\EasyCodingStandard\SnippetFormatter\Provider\CurrentParentFileInfoProvider;
use Symplify\EasyCodingStandard\SnippetFormatter\Provider\CurrentParentFilePathProvider;
use Symplify\EasyCodingStandard\ValueObject\Configuration;
use Symplify\EasyCodingStandard\ValueObject\Error\FileDiff;
use Throwable;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function __construct(
private readonly DifferInterface $differ,
private readonly EasyCodingStandardStyle $easyCodingStandardStyle,
private readonly \Symfony\Component\Filesystem\Filesystem $filesystem,
private readonly CurrentParentFileInfoProvider $currentParentFileInfoProvider,
private readonly CurrentParentFilePathProvider $currentParentFilePathProvider,
private readonly TargetFileInfoResolver $targetFileInfoResolver,
private readonly FileDiffFactory $fileDiffFactory,
array $fixers
Expand All @@ -79,9 +79,9 @@ public function getCheckers(): array
/**
* @return array{file_diffs?: FileDiff[]}
*/
public function processFile(SplFileInfo $fileInfo, Configuration $configuration): array
public function processFile(string $filePath, Configuration $configuration): array
{
$tokens = $this->fileToTokensParser->parseFromFilePath($fileInfo->getRealPath());
$tokens = $this->fileToTokensParser->parseFromFilePath($filePath);

$appliedFixers = [];

Expand All @@ -90,7 +90,7 @@ public function processFile(SplFileInfo $fileInfo, Configuration $configuration)
continue;
}

if ($this->processTokensByFixer($fileInfo, $tokens, $fixer)) {
if ($this->processTokensByFixer($filePath, $tokens, $fixer)) {
$appliedFixers[] = $fixer::class;
}
}
Expand All @@ -99,7 +99,7 @@ public function processFile(SplFileInfo $fileInfo, Configuration $configuration)
return [];
}

$fileContents = FileSystem::read($fileInfo->getRealPath());
$fileContents = FileSystem::read($filePath);
$diff = $this->differ->diff($fileContents, $tokens->generateCode());

// some fixer with feature overlap can null each other
Expand All @@ -110,16 +110,16 @@ public function processFile(SplFileInfo $fileInfo, Configuration $configuration)
$fileDiffs = [];

// file has changed
$targetFileInfo = $this->targetFileInfoResolver->resolveTargetFileInfo($fileInfo);
$targetFilePath = $this->targetFileInfoResolver->resolveTargetFilePath($filePath);
$fileDiffs[] = $this->fileDiffFactory->createFromDiffAndAppliedCheckers(
$targetFileInfo,
$targetFilePath,
$diff,
$appliedFixers
);

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

Tokens::clearCache();
Expand All @@ -129,27 +129,22 @@ public function processFile(SplFileInfo $fileInfo, Configuration $configuration)
];
}

public function processFileToString(SplFileInfo | string $fileInfo): string
public function processFileToString(string $filePath): string
{
// compat layer
if (is_string($fileInfo)) {
$fileInfo = new SplFileInfo($fileInfo);
}

$tokens = $this->fileToTokensParser->parseFromFilePath($fileInfo->getRealPath());
$tokens = $this->fileToTokensParser->parseFromFilePath($filePath);

$appliedFixers = [];
foreach ($this->fixers as $fixer) {
if ($this->shouldSkipForMarkdownHeredocCheck($fixer)) {
continue;
}

if ($this->processTokensByFixer($fileInfo, $tokens, $fixer)) {
if ($this->processTokensByFixer($filePath, $tokens, $fixer)) {
$appliedFixers[] = $fixer::class;
}
}

$contents = FileSystem::read($fileInfo->getRealPath());
$contents = FileSystem::read($filePath);
if ($appliedFixers === []) {
return $contents;
}
Expand Down Expand Up @@ -181,9 +176,9 @@ private function sortFixers(array $fixers): array
* @param Tokens<Token> $tokens
* @return bool If fixer applied
*/
private function processTokensByFixer(SplFileInfo $fileInfo, Tokens $tokens, FixerInterface $fixer): bool
private function processTokensByFixer(string $filePath, Tokens $tokens, FixerInterface $fixer): bool
{
if ($this->shouldSkip($fileInfo, $fixer, $tokens)) {
if ($this->shouldSkip($filePath, $fixer, $tokens)) {
return false;
}

Expand All @@ -193,11 +188,11 @@ private function processTokensByFixer(SplFileInfo $fileInfo, Tokens $tokens, Fix
}

try {
$fixer->fix($fileInfo, $tokens);
$fixer->fix(new SplFileInfo($filePath), $tokens);
} catch (Throwable $throwable) {
throw new FixerFailedException(sprintf(
'Fixing of "%s" file by "%s" failed: %s in file %s on line %d',
$fileInfo->getRealPath(),
$filePath,
$fixer::class,
$throwable->getMessage(),
$throwable->getFile(),
Expand All @@ -218,13 +213,13 @@ private function processTokensByFixer(SplFileInfo $fileInfo, Tokens $tokens, Fix
/**
* @param Tokens<Token> $tokens
*/
private function shouldSkip(SplFileInfo $fileInfo, FixerInterface $fixer, Tokens $tokens): bool
private function shouldSkip(string $filePath, FixerInterface $fixer, Tokens $tokens): bool
{
if ($this->skipper->shouldSkipElementAndFileInfo($fixer, $fileInfo)) {
if ($this->skipper->shouldSkipElementAndFilePath($fixer, $filePath)) {
return true;
}

if (! $fixer->supports($fileInfo)) {
if (! $fixer->supports(new SplFileInfo($filePath))) {
return true;
}

Expand All @@ -236,7 +231,7 @@ private function shouldSkip(SplFileInfo $fileInfo, FixerInterface $fixer, Tokens
*/
private function shouldSkipForMarkdownHeredocCheck(FixerInterface $fixer): bool
{
if ($this->currentParentFileInfoProvider->provide() === null) {
if ($this->currentParentFilePathProvider->provide() === null) {
return false;
}

Expand Down
5 changes: 2 additions & 3 deletions packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura

foreach ($filePaths as $filePath) {
try {
$fileInfo = new SplFileInfo($filePath);
$currentErrorsAndFileDiffs = $this->singleFileProcessor->processFileInfo(
$fileInfo,
$currentErrorsAndFileDiffs = $this->singleFileProcessor->processFilePath(
$filePath,
$configuration
);

Expand Down
11 changes: 5 additions & 6 deletions packages/Skipper/Skipper/Skipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,22 @@ public function __construct(

public function shouldSkipElement(string | object $element): bool
{
$fileInfo = new SplFileInfo(__FILE__);
return $this->shouldSkipElementAndFileInfo($element, $fileInfo);
return $this->shouldSkipElementAndFilePath($element, __FILE__);
}

public function shouldSkipFileInfo(SplFileInfo $fileInfo): bool
public function shouldSkipFilePath(string $filePath): bool
{
return $this->shouldSkipElementAndFileInfo(self::FILE_ELEMENT, $fileInfo);
return $this->shouldSkipElementAndFilePath(self::FILE_ELEMENT, $filePath);
}

public function shouldSkipElementAndFileInfo(string | object $element, SplFileInfo $fileInfo): bool
public function shouldSkipElementAndFilePath(string | object $element, string $filePath): bool
{
foreach ($this->skipVoters as $skipVoter) {
if (! $skipVoter->match($element)) {
continue;
}

if (! $skipVoter->shouldSkip($element, $fileInfo)) {
if (! $skipVoter->shouldSkip($element, $filePath)) {
continue;
}

Expand Down
Loading

0 comments on commit fdddae9

Please sign in to comment.