Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add applied rules to file #56

Merged
merged 1 commit into from
May 3, 2024
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
6 changes: 6 additions & 0 deletions fractor-yaml/src/YamlFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace a9f\FractorYaml;

use a9f\Fractor\Application\Contract\FileProcessor;
use a9f\Fractor\Application\ValueObject\AppliedRule;
use a9f\Fractor\Application\ValueObject\File;
use a9f\Fractor\ValueObject\Indent;
use a9f\FractorYaml\Contract\YamlDumper;
Expand Down Expand Up @@ -35,7 +36,12 @@ public function handle(File $file): void
$newYaml = $yaml;

foreach ($this->rules as $rule) {
$oldYaml = $newYaml;
$newYaml = $rule->refactor($newYaml);

if ($oldYaml !== $newYaml) {
$file->addAppliedRule(AppliedRule::fromRule($rule));
}
}

// Nothing has changed.
Expand Down
2 changes: 1 addition & 1 deletion fractor-yaml/tests/Fixtures/DummyYamlFractorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function buildNewTranslations(array $oldTranslations): array
{
return array_filter(
$oldTranslations,
static fn ($oldTranslationFile): bool => !\str_starts_with((string) $oldTranslationFile, 'EXT:form')
static fn ($oldTranslationFile): bool => !\str_starts_with($oldTranslationFile, 'EXT:form')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace a9f\FractorYaml\Tests\YamlFileProcessor;

use a9f\Fractor\Application\ValueObject\AppliedRule;
use a9f\Fractor\Application\ValueObject\File;
use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
use a9f\FractorYaml\Tests\Fixtures\DummyYamlFractorRule;
use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;

Expand All @@ -14,6 +17,10 @@ final class YamlFileProcessorTest extends AbstractFractorTestCase
public function test(string $filePath): void
{
$this->doTestFile($filePath);

$file = $this->fileCollector->getFileByPath($filePath);
self::assertInstanceOf(File::class, $file);
self::assertEquals([AppliedRule::fromClassString(DummyYamlFractorRule::class)], $file->getAppliedRules());
}

public static function provideData(): Iterator
Expand Down
7 changes: 5 additions & 2 deletions fractor/src/Application/FractorRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ public function run(Output $output, Configuration $configuration): void

foreach ($filePaths as $filePath) {
$file = new File($filePath, FileSystem::read($filePath));
$this->fileCollector->addFile($file);

foreach ($this->processors as $processor) {
if (!$processor->canHandle($file)) {
$output->progressAdvance();
continue;
}

$this->fileCollector->addFile($file);

$processor->handle($file);
$output->progressAdvance();
}

if (!$file->hasChanged()) {
continue;
}

$file->setFileDiff($this->fileDiffFactory->createFileDiff($file));
}

Expand Down
39 changes: 39 additions & 0 deletions fractor/src/Application/ValueObject/AppliedRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace a9f\Fractor\Application\ValueObject;

use a9f\Fractor\Application\Contract\FractorRule;

final readonly class AppliedRule
{
/**
* @param class-string<FractorRule> $fractorRule
*/
private function __construct(
private string $fractorRule,
) {
}

public static function fromRule(FractorRule $fractorRule): self
{
return new self($fractorRule::class);
}

/**
* @param class-string<FractorRule> $fractorRule
*/
public static function fromClassString(string $fractorRule): self
{
return new self($fractorRule);
}

/**
* @return class-string<FractorRule>
*/
public function getFractorRule(): string
{
return $this->fractorRule;
}
}
18 changes: 18 additions & 0 deletions fractor/src/Application/ValueObject/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class File
private readonly string $fileExtension;
private ?FileDiff $fileDiff = null;

/**
* @var AppliedRule[]
*/
private array $appliedRules = [];

public function __construct(private readonly string $filePath, private string $content)
{
$this->originalContent = $this->content;
Expand Down Expand Up @@ -83,9 +88,22 @@ public function setFileDiff(FileDiff $fileDiff): void
{
$this->fileDiff = $fileDiff;
}

public function addAppliedRule(AppliedRule $appliedRule): void
{
$this->appliedRules[] = $appliedRule;
}

public function getFileDiff(): ?FileDiff
{
return $this->fileDiff;
}

/**
* @return AppliedRule[]
*/
public function getAppliedRules(): array
{
return $this->appliedRules;
}
}