Skip to content
Open
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
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ insert_final_newline = true
[tabs.txt]
indent_style = tab

# Text fixtures deliberately carry trailing whitespace to exercise reformatting
[*.txt.fixture]
trim_trailing_whitespace = false

# YAML-Files
[{*.yml,*.yaml,*.yml.fixture,*.yaml.fixture}]
indent_size = 2
Expand Down
9 changes: 4 additions & 5 deletions e2e/typo3-yaml/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
-
options:
recipients:
- bar@domain.com: 'Bar'
+ foo@domain.com: Bar
+ bar@domain.com: Bar
bar@domain.com: Bar

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is there a diff?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The YAML processor now diffs against a normalized baseline (the re-dumped original) instead of the raw source. Previously the diff was raw: 'Bar' against 'Bar'. After our change we diff against Bar without quotes. That's why the expected-output.txt had to change.

subject: 'Kontaktanfrage von Website'
- recipientAddress: foo@domain.com
- recipientName: Bar
Expand All @@ -32,9 +31,8 @@
-
options:
recipients:
- bar@domain.com: 'Bar'
+ foo@domain.com: Bar
+ bar@domain.com: Bar
bar@domain.com: Bar
subject: 'Kontaktanfrage von Website'
- recipientAddress: foo@domain.com
- recipientName: Bar
Expand Down Expand Up @@ -70,8 +68,9 @@
----------- end diff -----------

Applied rules:
* CodeFormatRule
* EmailFinisherYamlFractor


[OK] 1 file has been changed by Fractor
[OK] 1 file has been changed by Fractor

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering about the extra whitespace at the end here where it comes from and why it is not trimmed as before

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are coming from the symfony output and are technically always present in the console. I can only assume that the whitespaces had been trimmed by ones IDE in the past and therefore were formerly missing. They don't really matter and are only of cosmetic nature. We can savely remove them if you like to.


17 changes: 11 additions & 6 deletions packages/fractor-composer-json/src/ComposerJsonFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ public function canHandle(File $file): bool

public function handle(File $file, iterable $appliedRules): void
{
$fileHasChanged = \false;
$rawContent = $file->getContent();
$composerJson = $this->composerJsonFactory->createFromFile($file);
$oldComposerJson = $this->composerJsonFactory->createFromFile($file);

foreach ($appliedRules as $rule) {
$beforeArray = $composerJson->getJsonArray();
$rule->refactor($composerJson);

if ($oldComposerJson->getJsonArray() !== $composerJson->getJsonArray()) {
$file->changeFileContent($this->composerJsonPrinter->printToString($this->indent, $composerJson));
if ($beforeArray !== $composerJson->getJsonArray()) {
$file->addAppliedRule(AppliedRule::fromRule($rule));
$fileHasChanged = \true;
}
}

if (! $fileHasChanged) {
// Always re-print: a formatting-only change (indentation) is then
// attributed to CodeFormatRule by the runner rather than applied silently.
$newContent = rtrim($this->composerJsonPrinter->printToString($this->indent, $composerJson)) . "\n";

if ($newContent === $rawContent) {
$this->changedFilesDetector->addCachableFile($file->getFilePath());
return;
}

$file->changeFileContent($newContent);
}

public function allowedFileExtensions(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace a9f\FractorComposerJson\Tests\ComposerJsonProcessResult;

use a9f\Fractor\Application\ValueObject\AppliedRule;
use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
use a9f\FractorComposerJson\RemovePackageComposerJsonFractor;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* The composer.json processor re-prints files it touches, so a formatting-only
* change (indentation) must be attributed to the virtual CodeFormatRule — so a
* re-indentation is reported rather than applied silently — while a real
* transformation stays attributed to its own rule.
*/
final class ComposerJsonProcessResultTest extends AbstractFractorTestCase
{
/**
* @param list<string> $expectedAppliedRules
*/
#[DataProvider('provideData')]
public function test(string $fixture, array $expectedAppliedRules): void
{
$fractorTestResult = $this->doTestFile(__DIR__ . '/Fixtures/' . $fixture);

self::assertSame($expectedAppliedRules, $fractorTestResult->getAppliedFractorRules());
}

/**
* @return \Iterator<string, array{string, list<string>}>
*/
public static function provideData(): \Iterator
{
yield 'rule change is attributed to the real rule' => [
'rule-change/composer.json.fixture',
[RemovePackageComposerJsonFractor::class],
];
yield 'pure reformat is attributed to the virtual code-format rule' => [
'reformatting/composer.json.fixture',
[AppliedRule::CODE_FORMAT_RULE],
];
yield 'already formatted file reports no change' => ['already-formatted/composer.json.fixture', []];
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/fractor.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "acme/demo",
"require": {
"vendor1/keep": "^2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "acme/demo",
"require": {
"vendor1/keep": "^2.0"
}
}
-----
{
"name": "acme/demo",
"require": {
"vendor1/keep": "^2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"require": {
"vendor1/legacy": "^1.0",
"vendor1/keep": "^2.0"
}
}
-----
{
"require": {
"vendor1/keep": "^2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\FractorComposerJson\RemovePackageComposerJsonFractor;

return FractorConfiguration::configure()
->import(__DIR__ . '/../../../config/application.php')
->withConfiguredRule(RemovePackageComposerJsonFractor::class, ['vendor1/legacy']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
page = PAGE
page.10 = TEXT
page.10.value = Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
page = PAGE
page.10 = TEXT
page.10.value = Hello
-----
page = PAGE
page.10 = TEXT
page.10.value = Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
page = PAGE
config.spamProtectEmailAddresses = ascii
-----
page = PAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace a9f\FractorTypoScript\Tests\TypoScriptProcessResult;

use a9f\Fractor\Application\ValueObject\AppliedRule;
use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
use a9f\FractorTypoScript\Tests\Fixtures\DummyTypoScriptFractorRule;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* The TypoScript pretty-printer re-formats files it touches, so a formatting-only
* reformat must be attributed to the virtual CodeFormatRule (and count toward the
* exit code), while a real transformation stays attributed to its own rule.
*/
final class TypoScriptProcessResultTest extends AbstractFractorTestCase
{
/**
* @param list<string> $expectedAppliedRules
*/
#[DataProvider('provideData')]
public function test(string $fixture, array $expectedAppliedRules): void
{
$fractorTestResult = $this->doTestFile(__DIR__ . '/Fixtures/' . $fixture);

self::assertSame($expectedAppliedRules, $fractorTestResult->getAppliedFractorRules());
}

/**
* @return \Iterator<string, array{string, list<string>}>
*/
public static function provideData(): \Iterator
{
yield 'rule change is attributed to the real rule' => [
'rule-change.typoscript.fixture',
[DummyTypoScriptFractorRule::class],
];
yield 'pure reformat is attributed to the virtual code-format rule' => [
'reformatting.typoscript.fixture',
[AppliedRule::CODE_FORMAT_RULE],
];
yield 'already formatted file reports no change' => ['already-formatted.typoscript.fixture', []];
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/fractor.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
use a9f\FractorTypoScript\Tests\Fixtures\DummyTypoScriptFractorRule;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConditionTermination;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;

return FractorConfiguration::configure()
->withOptions([
TypoScriptProcessorOption::INDENT_SIZE => 4,
TypoScriptProcessorOption::INDENT_CHARACTER => PrettyPrinterConfiguration::INDENTATION_STYLE_SPACES,
TypoScriptProcessorOption::ADD_CLOSING_GLOBAL => false,
TypoScriptProcessorOption::INCLUDE_EMPTY_LINE_BREAKS => true,
TypoScriptProcessorOption::INDENT_CONDITIONS => true,
TypoScriptProcessorOption::CONDITION_TERMINATION => PrettyPrinterConditionTermination::Keep,
])
->withRules([DummyTypoScriptFractorRule::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff
xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="messages">
<body>
<trans-unit id="key1" xml:space="preserve">
<source>Goodbye</source>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="messages">
<body>
<trans-unit id="key1" xml:space="preserve">
<source>Goodbye</source>
</trans-unit>
</body>
</file>
</xliff>
-----
<?xml version="1.0" encoding="UTF-8"?>
<xliff
xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="messages">
<body>
<trans-unit id="key1" xml:space="preserve">
<source>Goodbye</source>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="messages">
<body>
<trans-unit id="key1" xml:space="preserve">
<source>Hello</source>
</trans-unit>
</body>
</file>
</xliff>
-----
<?xml version="1.0" encoding="UTF-8"?>
<xliff
xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="messages">
<body>
<trans-unit id="key1" xml:space="preserve">
<source>Hello World</source>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff
xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="messages">
<body>
<trans-unit id="key1" xml:space="preserve">
<source>Hello</source>
</trans-unit>
</body>
</file>
</xliff>
-----
<?xml version="1.0" encoding="UTF-8"?>
<xliff
xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="messages">
<body>
<trans-unit id="key1" xml:space="preserve">
<source>Hello World</source>
</trans-unit>
</body>
</file>
</xliff>
Loading