Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Strip left spaces from opening tags when comparing output for changes #5701

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Application/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
use Rector\ValueObject\Reporting\FileDiff;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use Nette\Utils\Strings;

final readonly class FileProcessor
{
/**
* @var string
* @see https://regex101.com/r/llm7XZ/1
*/
private const OPEN_TAG_SPACED_REGEX = '#^[ \t]+<\?php#m';

public function __construct(
private FormatPerservingPrinter $formatPerservingPrinter,
private RectorNodeTraverser $rectorNodeTraverser,
Expand Down Expand Up @@ -142,10 +149,16 @@ private function printFile(File $file, Configuration $configuration, string $fil
* Handle new line or space before <?php or InlineHTML node wiped on print format preserving
* On very first content level
*/
$originalFileContent = $file->getOriginalFileContent();
$ltrimOriginalFileContent = ltrim($originalFileContent);
$ltrimOriginalFileContent = ltrim($file->getOriginalFileContent());
$ltrimNewContent = ltrim($newContent);
stilliard marked this conversation as resolved.
Show resolved Hide resolved
if ($ltrimOriginalFileContent === $ltrimNewContent) {
return;
}

if ($ltrimOriginalFileContent === $newContent) {
// handle space before <?php
$ltrimNewContent = Strings::replace($ltrimNewContent, self::OPEN_TAG_SPACED_REGEX, '<?php');
$ltrimOriginalFileContent = Strings::replace($ltrimOriginalFileContent, self::OPEN_TAG_SPACED_REGEX, '<?php');
if ($ltrimOriginalFileContent === $ltrimNewContent) {
stilliard marked this conversation as resolved.
Show resolved Hide resolved
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ul>
<?php if (true) { /* comment */ ?>
<li><a href="/test">test</a></li>
<?php } ?>
</ul>