Skip to content

Commit 85b9714

Browse files
committed
[TASK] Attribute CodeFormatRule to YAML files a rule rewrites
- On a file a rule changes, use the re-dumped original as the diff baseline so the reported diff shows only the rule's change, and attribute the extra reformatting (indentation, comments the YAML dumper cannot keep) to the virtual CodeFormatRule - Leave files no rule touches byte-exact: never re-dump for formatting alone, so comment-only files are not rewritten project-wide - Cover the behaviour with a YamlProcessResult test (rule only, rule plus reformat, and a commented file left untouched)
1 parent 86071bd commit 85b9714

7 files changed

Lines changed: 100 additions & 7 deletions

File tree

e2e/typo3-yaml/expected-output.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
-
1010
options:
1111
recipients:
12-
- bar@domain.com: 'Bar'
1312
+ foo@domain.com: Bar
14-
+ bar@domain.com: Bar
13+
bar@domain.com: Bar
1514
subject: 'Kontaktanfrage von Website'
1615
- recipientAddress: foo@domain.com
1716
- recipientName: Bar
@@ -32,9 +31,8 @@
3231
-
3332
options:
3433
recipients:
35-
- bar@domain.com: 'Bar'
3634
+ foo@domain.com: Bar
37-
+ bar@domain.com: Bar
35+
bar@domain.com: Bar
3836
subject: 'Kontaktanfrage von Website'
3937
- recipientAddress: foo@domain.com
4038
- recipientName: Bar
@@ -70,8 +68,9 @@
7068
----------- end diff -----------
7169

7270
Applied rules:
71+
* CodeFormatRule
7372
* EmailFinisherYamlFractor
7473

7574

76-
[OK] 1 file has been changed by Fractor
75+
[OK] 1 file has been changed by Fractor
7776

packages/fractor-yaml/src/YamlFileProcessor.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ public function handle(File $file, iterable $appliedRules): void
5252
}
5353
}
5454

55-
// Nothing has changed.
55+
// Never re-dump for formatting alone: the dumper drops comments, so
56+
// untouched files stay byte-exact.
5657
if ($newYaml === $yaml) {
5758
$this->changedFilesDetector->addCachableFile($file->getFilePath());
5859
return;
5960
}
6061

61-
$file->changeFileContent($this->yamlDumper->dump($newYaml, $indent));
62+
// Re-dumped original as baseline: the diff shows only the rule's change,
63+
// any extra reformatting is attributed to CodeFormatRule by the runner.
64+
$file->changeOriginalContent(rtrim($this->yamlDumper->dump($yaml, $indent)) . "\n");
65+
$file->changeFileContent(rtrim($this->yamlDumper->dump($newYaml, $indent)) . "\n");
6266
}
6367

6468
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# this comment is lost when the file is re-dumped
2+
Form:
3+
renderingOptions:
4+
translation:
5+
translationFile:
6+
10: 'EXT:form/Resources/Private/Language/locallang.xlf'
7+
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
8+
-----
9+
Form:
10+
renderingOptions:
11+
translation:
12+
translationFiles:
13+
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Form:
2+
renderingOptions:
3+
translation:
4+
translationFile:
5+
10: 'EXT:form/Resources/Private/Language/locallang.xlf'
6+
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
7+
-----
8+
Form:
9+
renderingOptions:
10+
translation:
11+
translationFiles:
12+
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# this comment survives because no rule rewrites the file
2+
Form:
3+
renderingOptions:
4+
skinning: true
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorYaml\Tests\YamlProcessResult;
6+
7+
use a9f\Fractor\Application\ValueObject\AppliedRule;
8+
use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
9+
use a9f\FractorYaml\Tests\Fixtures\DummyYamlFractorRule;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
12+
/**
13+
* The YAML dumper cannot preserve comments, so the processor only re-dumps files
14+
* a rule already rewrites — never for formatting alone. On such a file the rule
15+
* stays attributed to itself, and any reformatting that rides along (indentation,
16+
* lost comments) is additionally attributed to the virtual CodeFormatRule. A file
17+
* no rule touches is left byte-exact, even when it carries comments.
18+
*/
19+
final class YamlProcessResultTest extends AbstractFractorTestCase
20+
{
21+
/**
22+
* @param list<string> $expectedAppliedRules
23+
*/
24+
#[DataProvider('provideData')]
25+
public function test(string $fixture, array $expectedAppliedRules): void
26+
{
27+
$fractorTestResult = $this->doTestFile(__DIR__ . '/Fixtures/' . $fixture);
28+
29+
self::assertSame($expectedAppliedRules, $fractorTestResult->getAppliedFractorRules());
30+
}
31+
32+
/**
33+
* @return \Iterator<string, array{string, list<string>}>
34+
*/
35+
public static function provideData(): \Iterator
36+
{
37+
yield 'rule change on canonical input is attributed to the real rule' => [
38+
'rule-only.yaml.fixture',
39+
[DummyYamlFractorRule::class],
40+
];
41+
yield 'reformatting that rides along a rule is also attributed to the code-format rule' => [
42+
'rule-and-reformat.yaml.fixture',
43+
[AppliedRule::CODE_FORMAT_RULE, DummyYamlFractorRule::class],
44+
];
45+
yield 'a file no rule touches is left byte-exact' => ['untouched-with-comment.yaml.fixture', []];
46+
}
47+
48+
public function provideConfigFilePath(): string
49+
{
50+
return __DIR__ . '/config/fractor.php';
51+
}
52+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use a9f\Fractor\Configuration\FractorConfiguration;
6+
use a9f\FractorYaml\Tests\Fixtures\DummyYamlFractorRule;
7+
8+
return FractorConfiguration::configure()
9+
->withRules([DummyYamlFractorRule::class]);

0 commit comments

Comments
 (0)