Skip to content

Commit

Permalink
[Core] Handle space removed on no rules applied on PhpFileProcessor (#…
Browse files Browse the repository at this point in the history
…3412)

* [Core] Handle space removed on no rules applied on PhpFileProcessor

* handle without --dry-run

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* handle any space but new line

* Future comment

* Future comment

* Future comment

* Fix

* Really Final touch: Grammar fix

* Really Final touch: Grammar fix

* Handle open tag inside string

* Handle open tag inside string

* Final touch: improve performance: avoid double print

* Final touch: phpstan

* Final touch: ensure test in multiple extensions: php, phtml

* Final touch: ensure test in multiple extensions: php, phtml

* deprecate FormatPerservingPrinter::printParsedStmstAndTokens()

* remove printParsedStmstAndTokens

* Remove commented code

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 25, 2023
1 parent 7a6531d commit f60aaa5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 53 deletions.
43 changes: 4 additions & 39 deletions e2e/plain-views/expected-output.diff
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
4 files with changes
====================
1 file with changes
===================

1) src/with_whitespace_previous.php:0

---------- begin diff ----------
@@ @@
-
-
-
<?php

echo 'test';
----------- end diff -----------

2) src/view.php:0

---------- begin diff ----------
@@ @@
-
- <div>
+<div>
<?php echo 1 ?>
</div>
----------- end diff -----------

3) src/short_open_tag_with_import_name.php:0
1) src/short_open_tag_with_import_name.php:0

---------- begin diff ----------
@@ @@
Expand All @@ -45,16 +22,4 @@
+}
----------- end diff -----------

4) src/short_open_tag.php:4

---------- begin diff ----------
@@ @@
<div class="wrap">
<img src="<?= escape('hi there'); ?>">
</div>
- <?php
+<?php
}
----------- end diff -----------

[OK] 4 files would have changed (dry-run) by Rector
[OK] 1 file would have changed (dry-run) by Rector
1 change: 1 addition & 0 deletions e2e/plain-views/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

$rectorConfig->sets([\Rector\Set\ValueObject\SetList::CODE_QUALITY]);
$rectorConfig->importNames();
$rectorConfig->fileExtensions(['php', 'phtml']);
};
10 changes: 10 additions & 0 deletions e2e/plain-views/src/open_tag_inside_string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
function test_callback2() {
// var_dump($output);
?>
<div class="wrap">
<img src="<?= escape('hi there'); ?>">
</div>
<?php
echo " <?php";
}
9 changes: 9 additions & 0 deletions e2e/plain-views/src/short_open_tag.php.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
function test_callback() {
// var_dump($output);
?>
<div class="wrap">
<img src="<?= escape('hi there'); ?>">
</div>
<?php
}
41 changes: 38 additions & 3 deletions src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Core\Application\FileProcessor;

use Nette\Utils\Strings;
use PHPStan\AnalysedCodeException;
use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
use Rector\Core\Application\FileDecorator\FileDiffFileDecorator;
Expand All @@ -26,6 +27,12 @@

final class PhpFileProcessor implements FileProcessorInterface
{
/**
* @var string
* @see https://regex101.com/r/xP2MGa/1
*/
private const OPEN_TAG_SPACED_REGEX = '#^(?<open_tag_spaced>[^\S\r\n]+\<\?php)#m';

public function __construct(
private readonly FormatPerservingPrinter $formatPerservingPrinter,
private readonly FileProcessor $fileProcessor,
Expand Down Expand Up @@ -142,9 +149,37 @@ private function printFile(File $file, Configuration $configuration): void
return;
}

$newContent = $configuration->isDryRun()
? $this->formatPerservingPrinter->printParsedStmstAndTokensToString($file)
: $this->formatPerservingPrinter->printParsedStmstAndTokens($file);
// only save to string first, no need to print to file when not needed
$newContent = $this->formatPerservingPrinter->printParsedStmstAndTokensToString($file);

/**
* When no Rules applied, the PostRector may still change the content, that's why printing still needed
* On printing, the space may be wiped, these below check compare with original file content used to verify
* that no diff actually needed
*/
if ($file->getRectorWithLineChanges() === []) {
/**
* Handle new line or space before <?php or InlineHTML node wiped on print format preserving
* On very first content level
*/
$originalFileContent = $file->getOriginalFileContent();
if (ltrim($originalFileContent) === $newContent) {
return;
}

/**
* Handle space before <?php wiped on print format preserving
* On inside content level
*/
$cleanedOriginalFileContent = Strings::replace($originalFileContent, self::OPEN_TAG_SPACED_REGEX, '<?php');
if ($cleanedOriginalFileContent === $newContent) {
return;
}
}

if (! $configuration->isDryRun()) {
$this->formatPerservingPrinter->dumpFile($file->getFilePath(), $newContent);
}

$file->changeFileContent($newContent);
$this->fileDiffFileDecorator->decorate([$file]);
Expand Down
14 changes: 3 additions & 11 deletions src/PhpParser/Printer/FormatPerservingPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public function printToFile(string $filePath, array $newStmts, array $oldStmts,
{
$newContent = $this->betterStandardPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens);

$this->filesystem->dumpFile($filePath, $newContent);

// @todo how to keep origianl access rights without the SplFileInfo
// $this->filesystem->chmod($filePath, $fileInfo->getPerms());
$this->dumpFile($filePath, $newContent);

return $newContent;
}
Expand All @@ -47,13 +44,8 @@ public function printParsedStmstAndTokensToString(File $file): string
);
}

public function printParsedStmstAndTokens(File $file): string
public function dumpFile(string $filePath, string $newContent): void
{
return $this->printToFile(
$file->getFilePath(),
$file->getNewStmts(),
$file->getOldStmts(),
$file->getOldTokens()
);
$this->filesystem->dumpFile($filePath, $newContent);
}
}

0 comments on commit f60aaa5

Please sign in to comment.