Skip to content

Commit 95f513a

Browse files
committed
[FEATURE] Add applied rules to file
1 parent d3d5755 commit 95f513a

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

fractor-yaml/src/YamlFileProcessor.php

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace a9f\FractorYaml;
66

77
use a9f\Fractor\Application\Contract\FileProcessor;
8+
use a9f\Fractor\Application\ValueObject\AppliedRule;
89
use a9f\Fractor\Application\ValueObject\File;
910
use a9f\Fractor\ValueObject\Indent;
1011
use a9f\FractorYaml\Contract\YamlDumper;
@@ -35,7 +36,12 @@ public function handle(File $file): void
3536
$newYaml = $yaml;
3637

3738
foreach ($this->rules as $rule) {
39+
$oldYaml = $newYaml;
3840
$newYaml = $rule->refactor($newYaml);
41+
42+
if ($oldYaml !== $newYaml) {
43+
$file->addAppliedRule(AppliedRule::fromRule($rule));
44+
}
3945
}
4046

4147
// Nothing has changed.

fractor-yaml/tests/Fixtures/DummyYamlFractorRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private function buildNewTranslations(array $oldTranslations): array
6161
{
6262
return array_filter(
6363
$oldTranslations,
64-
static fn ($oldTranslationFile): bool => !\str_starts_with((string) $oldTranslationFile, 'EXT:form')
64+
static fn ($oldTranslationFile): bool => !\str_starts_with($oldTranslationFile, 'EXT:form')
6565
);
6666
}
6767
}

fractor-yaml/tests/YamlFileProcessor/YamlFileProcessorTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace a9f\FractorYaml\Tests\YamlFileProcessor;
66

7+
use a9f\Fractor\Application\ValueObject\AppliedRule;
8+
use a9f\Fractor\Application\ValueObject\File;
79
use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
10+
use a9f\FractorYaml\Tests\Fixtures\DummyYamlFractorRule;
811
use Iterator;
912
use PHPUnit\Framework\Attributes\DataProvider;
1013

@@ -14,6 +17,10 @@ final class YamlFileProcessorTest extends AbstractFractorTestCase
1417
public function test(string $filePath): void
1518
{
1619
$this->doTestFile($filePath);
20+
21+
$file = $this->fileCollector->getFileByPath($filePath);
22+
self::assertInstanceOf(File::class, $file);
23+
self::assertEquals([AppliedRule::fromClassString(DummyYamlFractorRule::class)], $file->getAppliedRules());
1724
}
1825

1926
public static function provideData(): Iterator

fractor/src/Application/FractorRunner.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ public function run(Output $output, Configuration $configuration): void
3434

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

3839
foreach ($this->processors as $processor) {
3940
if (!$processor->canHandle($file)) {
4041
$output->progressAdvance();
4142
continue;
4243
}
4344

44-
$this->fileCollector->addFile($file);
45-
4645
$processor->handle($file);
4746
$output->progressAdvance();
4847
}
4948

49+
if (!$file->hasChanged()) {
50+
continue;
51+
}
52+
5053
$file->setFileDiff($this->fileDiffFactory->createFileDiff($file));
5154
}
5255

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\Application\ValueObject;
6+
7+
use a9f\Fractor\Application\Contract\FractorRule;
8+
9+
final readonly class AppliedRule
10+
{
11+
/**
12+
* @param class-string<FractorRule> $fractorRule
13+
*/
14+
private function __construct(
15+
private string $fractorRule,
16+
) {
17+
}
18+
19+
public static function fromRule(FractorRule $fractorRule): self
20+
{
21+
return new self($fractorRule::class);
22+
}
23+
24+
/**
25+
* @param class-string<FractorRule> $fractorRule
26+
*/
27+
public static function fromClassString(string $fractorRule): self
28+
{
29+
return new self($fractorRule);
30+
}
31+
32+
/**
33+
* @return class-string<FractorRule>
34+
*/
35+
public function getFractorRule(): string
36+
{
37+
return $this->fractorRule;
38+
}
39+
}

fractor/src/Application/ValueObject/File.php

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ final class File
1616
private readonly string $fileExtension;
1717
private ?FileDiff $fileDiff = null;
1818

19+
/**
20+
* @var AppliedRule[]
21+
*/
22+
private array $appliedRules = [];
23+
1924
public function __construct(private readonly string $filePath, private string $content)
2025
{
2126
$this->originalContent = $this->content;
@@ -83,9 +88,22 @@ public function setFileDiff(FileDiff $fileDiff): void
8388
{
8489
$this->fileDiff = $fileDiff;
8590
}
91+
92+
public function addAppliedRule(AppliedRule $appliedRule): void
93+
{
94+
$this->appliedRules[] = $appliedRule;
95+
}
8696

8797
public function getFileDiff(): ?FileDiff
8898
{
8999
return $this->fileDiff;
90100
}
101+
102+
/**
103+
* @return AppliedRule[]
104+
*/
105+
public function getAppliedRules(): array
106+
{
107+
return $this->appliedRules;
108+
}
91109
}

0 commit comments

Comments
 (0)