Skip to content

Commit

Permalink
feat: Simplify closing marker when possible in `heredoc_closing_marke…
Browse files Browse the repository at this point in the history
…r` fixer (PHP-CS-Fixer#7676)
  • Loading branch information
mvorisek authored Jan 9, 2024
1 parent d2906cd commit 41be952
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
13 changes: 12 additions & 1 deletion src/Fixer/StringNotation/HeredocClosingMarkerFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,18 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}
}

$newClosingMarker = $reservedClosingMarkersMap[mb_strtoupper($existingClosingMarker)] ?? $this->configuration['closing_marker'];
$existingClosingMarker = mb_strtoupper($existingClosingMarker);
do {
$newClosingMarker = $reservedClosingMarkersMap[$existingClosingMarker] ?? null;
if (!str_ends_with($existingClosingMarker, '_')) {
break;
}
$existingClosingMarker = substr($existingClosingMarker, 0, -1);
} while (null === $newClosingMarker);

if (null === $newClosingMarker) {
$newClosingMarker = $this->configuration['closing_marker'];
}

$content = $tokens->generatePartialCode($startIndex + 1, $index - 1);
while (Preg::match('~(^|[\r\n])\s*'.preg_quote($newClosingMarker, '~').'(?!\w)~', $content)) {
Expand Down
56 changes: 29 additions & 27 deletions tests/Fixer/StringNotation/HeredocClosingMarkerFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ public static function provideFixCases(): iterable
<?php $a = <<<EOD
xxx EOD xxx
EOD;

PHP,
<<<'PHP'
<?php $a = <<<TEST
xxx EOD xxx
TEST;

PHP,
];

Expand All @@ -60,13 +58,11 @@ public static function provideFixCases(): iterable
<?php $a = <<<'EOD'
xxx EOD xxx
EOD;

PHP,
<<<'PHP'
<?php $a = <<<'TEST'
xxx EOD xxx
TEST;

PHP,
];

Expand All @@ -75,13 +71,11 @@ public static function provideFixCases(): iterable
<?php $a = <<<EOF
xxx
EOF;

PHP,
<<<'PHP'
<?php $a = <<<TEST
xxx
TEST;

PHP,
['closing_marker' => 'EOF'],
];
Expand All @@ -97,7 +91,6 @@ public static function provideFixCases(): iterable
$b = <<<'EOD'
xxx3
EOD;

PHP,
<<<'PHP'
<?php $a = <<<TEST
Expand All @@ -109,7 +102,6 @@ public static function provideFixCases(): iterable
$b = <<<'TEST'
xxx3
TEST;

PHP,
['explicit_heredoc_style' => true],
];
Expand All @@ -119,13 +111,11 @@ public static function provideFixCases(): iterable
<?php $a = b<<<EOD
xxx EOD xxx
EOD;

PHP,
<<<'PHP'
<?php $a = b<<<TEST
xxx EOD xxx
TEST;

PHP,
];

Expand All @@ -134,13 +124,11 @@ public static function provideFixCases(): iterable
<?php $a = B<<<EOD
xxx EOD xxx
EOD;

PHP,
<<<'PHP'
<?php $a = B<<<TEST
xxx EOD xxx
TEST;

PHP,
];

Expand All @@ -149,7 +137,6 @@ public static function provideFixCases(): iterable
<?php $a = <<<PHP
xxx
PHP;

PHP_,
];

Expand All @@ -160,15 +147,13 @@ public static function provideFixCases(): iterable
PHP;
$a = <<<PHP
PHP;

PHP_,
<<<'PHP'
<?php $a = <<<php
xxx
php;
$a = <<<Php
Php;

PHP,
];

Expand All @@ -189,7 +174,6 @@ public static function provideFixCases(): iterable
$c = <<<EOD
xxx3
EOD;

PHP,
<<<'PHP_'
<?php $a = <<<Žlutý
Expand All @@ -207,23 +191,49 @@ public static function provideFixCases(): iterable
$c = <<<PHP
xxx3
PHP;

PHP_,
['reserved_closing_markers' => ['Žlutý']],
];

yield 'no longer colliding reserved marker recovery' => [
<<<'PHP'
<?php
$a = <<<CSS
CSS;
$a = <<<CSS
CSS;
$a = <<<CSS_
CSS
CSS_;
$a = <<<CSS
CSS_
CSS;
PHP,
<<<'PHP'
<?php
$a = <<<CSS_
CSS_;
$a = <<<CSS__
CSS__;
$a = <<<CSS__
CSS
CSS__;
$a = <<<CSS__
CSS_
CSS__;
PHP,
];

yield 'heredoc /w content starting with preferred closing marker' => [
<<<'PHP'
<?php $a = <<<EOD_
EOD xxx
EOD_;

PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD xxx
TEST;

PHP,
];

Expand All @@ -232,13 +242,11 @@ public static function provideFixCases(): iterable
<?php $a = <<<EOD_
EOD xxx
EOD_;

PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD xxx
TEST;

PHP,
];

Expand All @@ -247,13 +255,11 @@ public static function provideFixCases(): iterable
<?php $a = <<<EOD_
EOD'
EOD_;

PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD'
TEST;

PHP,
];

Expand All @@ -262,13 +268,11 @@ public static function provideFixCases(): iterable
<?php $a = <<<EOD_
EOD;
EOD_;

PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD;
TEST;

PHP,
];

Expand All @@ -277,13 +281,11 @@ public static function provideFixCases(): iterable
<?php $a = <<<EOD
xxx EOD
EOD;

PHP,
<<<'PHP'
<?php $a = <<<TEST
xxx EOD
TEST;

PHP,
];
}
Expand Down

0 comments on commit 41be952

Please sign in to comment.