Skip to content

Commit

Permalink
Revert file not changed detected to be printed by PhpParser (#3409)
Browse files Browse the repository at this point in the history
* Revert file not changed detected to be printed by PhpParser

* rollback expected output
  • Loading branch information
samsonasik committed Feb 23, 2023
1 parent 61348a8 commit 9c7da41
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
44 changes: 40 additions & 4 deletions e2e/plain-views/expected-output.diff
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
1 file with changes
===================
4 files with changes
====================

1) src/short_open_tag_with_import_name.php:0
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>
+ </div>
----------- end diff -----------

3) src/short_open_tag_with_import_name.php:0

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

[OK] 1 file would have changed (dry-run) by Rector
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
37 changes: 3 additions & 34 deletions src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Core\Application\FileProcessor;

use Nette\Utils\FileSystem;
use PHPStan\AnalysedCodeException;
use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
use Rector\Core\Application\FileDecorator\FileDiffFileDecorator;
Expand Down Expand Up @@ -59,42 +58,27 @@ public function process(File $file, Configuration $configuration): array
return $systemErrorsAndFileDiffs;
}

$hasChangedOnPostRector = false;

// 2. change nodes with Rectors
do {
$file->changeHasChanged(false);
$this->fileProcessor->refactor($file, $configuration);

$fileNewStmts = $file->getNewStmts();

// 3. apply post rectors
$filePostRectorNewStmts = $this->postFileProcessor->traverse($fileNewStmts);

$newStmts = $this->postFileProcessor->traverse($file->getNewStmts());
// this is needed for new tokens added in "afterTraverse()"
$file->changeNewStmts($filePostRectorNewStmts);

if (! $hasChangedOnPostRector && $file->getRectorWithLineChanges() === [] && $fileNewStmts !== $filePostRectorNewStmts) {
$hasChangedOnPostRector = true;
}
$file->changeNewStmts($newStmts);

// 4. print to file or string
// important to detect if file has changed
$this->printFile($file, $configuration);
} while ($file->hasChanged());

// return early on no diff
// return json here
$fileDiff = $file->getFileDiff();
if (! $fileDiff instanceof FileDiff) {
return $systemErrorsAndFileDiffs;
}

// No Line change and no PostRector change? return early
if ($file->getRectorWithLineChanges() === [] && ! $hasChangedOnPostRector) {
$this->rollbackOriginalFile($file, $configuration);
return $systemErrorsAndFileDiffs;
}

$systemErrorsAndFileDiffs[Bridge::FILE_DIFFS] = [$fileDiff];
return $systemErrorsAndFileDiffs;
}
Expand All @@ -113,21 +97,6 @@ public function getSupportedFileExtensions(): array
return ['php'];
}

private function rollbackOriginalFile(File $file, Configuration $configuration): void
{
if ($configuration->isDryRun()) {
return;
}

$filePath = $file->getFilePath();
if ($this->removedAndAddedFilesCollector->isFileRemoved($filePath)) {
// skip, because this file exists no more
return;
}

Filesystem::write($filePath, $file->getOriginalFileContent());
}

/**
* @return SystemError[]
*/
Expand Down

0 comments on commit 9c7da41

Please sign in to comment.