Skip to content

Commit 66f6e56

Browse files
committed
[FEATURE] Add applied rules to file
1 parent d3d5755 commit 66f6e56

File tree

5 files changed

+71
-2
lines changed

5 files changed

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

fractor/src/Application/ValueObject/File.php

+15
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,19 @@ 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+
public function getAppliedRules(): array
103+
{
104+
return $this->appliedRules;
105+
}
91106
}

0 commit comments

Comments
 (0)