diff --git a/e2e/print-post-rectors/.gitignore b/e2e/print-post-rectors/.gitignore new file mode 100644 index 00000000000..61ead86667c --- /dev/null +++ b/e2e/print-post-rectors/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/e2e/print-post-rectors/composer.json b/e2e/print-post-rectors/composer.json new file mode 100644 index 00000000000..8b5ca727be7 --- /dev/null +++ b/e2e/print-post-rectors/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "php": "^8.1" + } +} diff --git a/e2e/print-post-rectors/expected-output.diff b/e2e/print-post-rectors/expected-output.diff new file mode 100644 index 00000000000..08415708451 --- /dev/null +++ b/e2e/print-post-rectors/expected-output.diff @@ -0,0 +1,63 @@ +1 file with changes +=================== + +1) src/TestClass.php:4 + + ---------- begin diff ---------- +@@ @@ + + namespace E2e\Parallel\Print\Post\Rectors; + ++use DateTime; + use PhpParser\Node\Name\FullyQualified; +-use PhpParser\Node\Expr\New_; + + class TestClass + { + /** +- * @var \PhpParser\Node\Name\FullyQualified|null ++ * @var FullyQualified|null + */ + private $fullyQualified = null; + +@@ @@ + private $cond1; + + /** +- * @var \DateTime|null ++ * @var DateTime|null + */ + private $dateTime = null; + +@@ @@ + { + if ($this->cond1) { + $this->doSomething(); +- } else { +- if ($this->dateTime !== null) { +- $this->doSomething(); +- } ++ } elseif ($this->dateTime !== null) { ++ $this->doSomething(); + } + } + +@@ @@ + { + } + +- public function getFullyQualified(\PhpParser\Node\Name\FullyQualified $fullyQualified): FullyQualified|null ++ public function getFullyQualified(FullyQualified $fullyQualified): FullyQualified|null + { + if ($this->fullyQualified === null) { + return $this->fullyQualified; + ----------- end diff ----------- + +Applied rules: + * ShortenElseIfRector + * DocblockNameImportingPostRector + * NameImportingPostRector + * UnusedImportRemovingPostRector + + + [OK] 1 file would have been changed (dry-run) by Rector \ No newline at end of file diff --git a/e2e/print-post-rectors/rector.php b/e2e/print-post-rectors/rector.php new file mode 100644 index 00000000000..e19b5e0a101 --- /dev/null +++ b/e2e/print-post-rectors/rector.php @@ -0,0 +1,13 @@ +paths([ + __DIR__ . '/src/TestClass.php', + ]); + $rectorConfig->importNames(); + $rectorConfig->removeUnusedImports(true); + $rectorConfig->rule(ShortenElseIfRector::class); +}; diff --git a/e2e/print-post-rectors/src/TestClass.php b/e2e/print-post-rectors/src/TestClass.php new file mode 100644 index 00000000000..ec7a67377ee --- /dev/null +++ b/e2e/print-post-rectors/src/TestClass.php @@ -0,0 +1,52 @@ +cond1) { + $this->doSomething(); + } else { + if ($this->dateTime !== null) { + $this->doSomething(); + } + } + } + + private function doSomething() + { + } + + public function getFullyQualified(\PhpParser\Node\Name\FullyQualified $fullyQualified): FullyQualified|null + { + if ($this->fullyQualified === null) { + return $this->fullyQualified; + } + + $this->fullyQualified = new FullyQualified('foo'); + + return new FullyQualified('foo'); + } +} diff --git a/phpstan.neon b/phpstan.neon index 947e43b5a06..bdbea036829 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -237,6 +237,7 @@ parameters: message: '#Avoid static access of constants, as they can change value\. Use interface and contract method instead#' paths: # caching and message of specific child Rector rules + - src/PostRector/Rector/AbstractPostRector.php - src/Rector/AbstractRector.php # for cache - src/Testing/PHPUnit/AbstractRectorTestCase.php diff --git a/src/ChangesReporting/ValueObject/RectorWithLineChange.php b/src/ChangesReporting/ValueObject/RectorWithLineChange.php index b2edf707152..7acfb2bc86a 100644 --- a/src/ChangesReporting/ValueObject/RectorWithLineChange.php +++ b/src/ChangesReporting/ValueObject/RectorWithLineChange.php @@ -5,6 +5,7 @@ namespace Rector\ChangesReporting\ValueObject; use Rector\Contract\Rector\RectorInterface; +use Rector\PostRector\Contract\Rector\PostRectorInterface; use Symplify\EasyParallel\Contract\SerializableInterface; use Webmozart\Assert\Assert; @@ -21,18 +22,18 @@ private const KEY_LINE = 'line'; /** - * @var class-string + * @var class-string */ private string $rectorClass; /** - * @param class-string|RectorInterface $rectorClass + * @param class-string|RectorInterface|PostRectorInterface $rectorClass */ public function __construct( - string|RectorInterface $rectorClass, + string|RectorInterface|PostRectorInterface $rectorClass, private int $line ) { - if ($rectorClass instanceof RectorInterface) { + if ($rectorClass instanceof RectorInterface || $rectorClass instanceof PostRectorInterface) { $rectorClass = $rectorClass::class; } @@ -40,7 +41,7 @@ public function __construct( } /** - * @return class-string + * @return class-string */ public function getRectorClass(): string { @@ -63,7 +64,7 @@ public static function decode(array $json): self } /** - * @return array{rector_class: class-string, line: int} + * @return array{rector_class: class-string, line: int} */ public function jsonSerialize(): array { diff --git a/src/PostRector/Rector/AbstractPostRector.php b/src/PostRector/Rector/AbstractPostRector.php index 77632fbbb03..3c52688e510 100644 --- a/src/PostRector/Rector/AbstractPostRector.php +++ b/src/PostRector/Rector/AbstractPostRector.php @@ -4,8 +4,10 @@ namespace Rector\PostRector\Rector; +use PhpParser\Node; use PhpParser\Node\Stmt; use PhpParser\NodeVisitorAbstract; +use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\PostRector\Contract\Rector\PostRectorInterface; use Rector\ValueObject\Application\File; use Webmozart\Assert\Assert; @@ -33,4 +35,14 @@ public function getFile(): File return $this->file; } + + protected function addRectorClassWithLine(Node $node): void + { + if ($this->file === null) { + return; + } + + $rectorWithLineChange = new RectorWithLineChange(static::class, $node->getStartLine()); + $this->file->addRectorClassWithLine($rectorWithLineChange); + } } diff --git a/src/PostRector/Rector/ClassRenamingPostRector.php b/src/PostRector/Rector/ClassRenamingPostRector.php index 7767b6370a7..2efafbd50aa 100644 --- a/src/PostRector/Rector/ClassRenamingPostRector.php +++ b/src/PostRector/Rector/ClassRenamingPostRector.php @@ -45,6 +45,10 @@ public function beforeTraverse(array $nodes): array if ($node instanceof FileWithoutNamespace || $node instanceof Namespace_) { $removedUses = $this->renamedClassesDataCollector->getOldClasses(); $node->stmts = $this->useImportsRemover->removeImportsFromStmts($node->stmts, $removedUses); + if ($removedUses !== []) { + // notify this rule changing code + $this->addRectorClassWithLine($node); + } break; } diff --git a/src/PostRector/Rector/DocblockNameImportingPostRector.php b/src/PostRector/Rector/DocblockNameImportingPostRector.php index d60bdfea9c4..cd2b5c69efc 100644 --- a/src/PostRector/Rector/DocblockNameImportingPostRector.php +++ b/src/PostRector/Rector/DocblockNameImportingPostRector.php @@ -39,6 +39,9 @@ public function enterNode(Node $node): Node|null return null; } + // notify this rule changing code + $this->addRectorClassWithLine($node); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); return $node; } diff --git a/src/PostRector/Rector/NameImportingPostRector.php b/src/PostRector/Rector/NameImportingPostRector.php index 9c51238b31f..349ee4e4a1b 100644 --- a/src/PostRector/Rector/NameImportingPostRector.php +++ b/src/PostRector/Rector/NameImportingPostRector.php @@ -5,6 +5,7 @@ namespace Rector\PostRector\Rector; use PhpParser\Node; +use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\GroupUse; @@ -42,7 +43,14 @@ public function enterNode(Node $node): Node|null return null; } - return $this->nameImporter->importName($node, $this->getFile(), $this->currentUses); + $name = $this->nameImporter->importName($node, $this->getFile(), $this->currentUses); + if (! $name instanceof Name) { + return null; + } + + // notify this rule changing code + $this->addRectorClassWithLine($node); + return $name; } /** diff --git a/src/PostRector/Rector/UnusedImportRemovingPostRector.php b/src/PostRector/Rector/UnusedImportRemovingPostRector.php index 35395d7aab9..8fdf8c5c048 100644 --- a/src/PostRector/Rector/UnusedImportRemovingPostRector.php +++ b/src/PostRector/Rector/UnusedImportRemovingPostRector.php @@ -93,6 +93,9 @@ public function enterNode(Node $node): ?Node return null; } + // notify this rule changing code + $this->addRectorClassWithLine($node); + $node->stmts = array_values($node->stmts); return $node; } diff --git a/src/Testing/PHPUnit/ValueObject/RectorTestResult.php b/src/Testing/PHPUnit/ValueObject/RectorTestResult.php index c399788a1dc..6df56490c2f 100644 --- a/src/Testing/PHPUnit/ValueObject/RectorTestResult.php +++ b/src/Testing/PHPUnit/ValueObject/RectorTestResult.php @@ -5,6 +5,7 @@ namespace Rector\Testing\PHPUnit\ValueObject; use Rector\Contract\Rector\RectorInterface; +use Rector\PostRector\Contract\Rector\PostRectorInterface; use Rector\ValueObject\ProcessResult; /** @@ -24,7 +25,7 @@ public function getChangedContents(): string } /** - * @return array> + * @return array> */ public function getAppliedRectorClasses(): array { diff --git a/src/ValueObject/Reporting/FileDiff.php b/src/ValueObject/Reporting/FileDiff.php index c9c48592a11..1f15a681bfd 100644 --- a/src/ValueObject/Reporting/FileDiff.php +++ b/src/ValueObject/Reporting/FileDiff.php @@ -8,6 +8,7 @@ use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Contract\Rector\RectorInterface; use Rector\Parallel\ValueObject\BridgeItem; +use Rector\PostRector\Contract\Rector\PostRectorInterface; use Symplify\EasyParallel\Contract\SerializableInterface; use Webmozart\Assert\Assert; @@ -81,7 +82,7 @@ public function getRectorShortClasses(): array } /** - * @return array> + * @return array> */ public function getRectorClasses(): array { diff --git a/tests/Issues/AnnotationToAttributeRenameAutoImport/Fixture/already_attributed.php.inc b/tests/Issues/AnnotationToAttributeRenameAutoImport/Fixture/already_attributed.php.inc index 8c8bd2ac920..032313848f4 100644 --- a/tests/Issues/AnnotationToAttributeRenameAutoImport/Fixture/already_attributed.php.inc +++ b/tests/Issues/AnnotationToAttributeRenameAutoImport/Fixture/already_attributed.php.inc @@ -1,6 +1,6 @@