Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/print-post-rectors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
5 changes: 5 additions & 0 deletions e2e/print-post-rectors/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8.1"
}
}
63 changes: 63 additions & 0 deletions e2e/print-post-rectors/expected-output.diff
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions e2e/print-post-rectors/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src/TestClass.php',
]);
$rectorConfig->importNames();
$rectorConfig->removeUnusedImports(true);
$rectorConfig->rule(ShortenElseIfRector::class);
};
52 changes: 52 additions & 0 deletions e2e/print-post-rectors/src/TestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace E2e\Parallel\Print\Post\Rectors;

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Expr\New_;

class TestClass
{
/**
* @var \PhpParser\Node\Name\FullyQualified|null
*/
private $fullyQualified = null;

/**
* @var bool
*/
private $cond1;

/**
* @var \DateTime|null
*/
private $dateTime = null;

public function run()
{
if ($this->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');
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions src/ChangesReporting/ValueObject/RectorWithLineChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,26 +22,26 @@
private const KEY_LINE = 'line';

/**
* @var class-string<RectorInterface>
* @var class-string<RectorInterface|PostRectorInterface>
*/
private string $rectorClass;

/**
* @param class-string<RectorInterface>|RectorInterface $rectorClass
* @param class-string<RectorInterface|PostRectorInterface>|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;
}

$this->rectorClass = $rectorClass;
}

/**
* @return class-string<RectorInterface>
* @return class-string<RectorInterface|PostRectorInterface>
*/
public function getRectorClass(): string
{
Expand All @@ -63,7 +64,7 @@ public static function decode(array $json): self
}

/**
* @return array{rector_class: class-string<RectorInterface>, line: int}
* @return array{rector_class: class-string<RectorInterface|PostRectorInterface>, line: int}
*/
public function jsonSerialize(): array
{
Expand Down
12 changes: 12 additions & 0 deletions src/PostRector/Rector/AbstractPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions src/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== []) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already never empty since checked on shouldTraverse method.

The $this->useImportsRemover->removeImportsFromStmts($node->stmts, $removedUses); can be updated to return bool, use flag like $hasChanged.

Yes, object is by reference, so you can verify if it has changed, no need to reassign on
This case.

Copy link
Contributor Author

@simonschaufi simonschaufi Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please do this refactoring in a separate isolated PR so that I can then use it in my PR? The return value is an array and unsetting a value in an array is not done by reference afaik. I would appreciate your help here!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the code, this PR seems cause various warning on unit test

WARNING: On fixture file "conflict_aliased_with_docblock.php.inc" for test "Rector\Tests\Issues\AutoImport\AutoImportTest"
File not changed but some Rector rules applied:
 * Rector\PostRector\Rector\ClassRenamingPostRector
.....
WARNING: On fixture file "skip_doctrine_aliased.php.inc" for test "Rector\Tests\Issues\AutoImport\AutoImportTest"
File not changed but some Rector rules applied:
 * Rector\PostRector\Rector\ClassRenamingPostRector

see:

https://github.com/rectorphp/rector-src/actions/runs/17867691953/job/50813890782?pr=7300#step:5:20

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try create alternative fresh PR, since NameImporter may also needs refactoring as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// notify this rule changing code
$this->addRectorClassWithLine($node);
}

break;
}
Expand Down
3 changes: 3 additions & 0 deletions src/PostRector/Rector/DocblockNameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion src/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Testing/PHPUnit/ValueObject/RectorTestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Testing\PHPUnit\ValueObject;

use Rector\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\ValueObject\ProcessResult;

/**
Expand All @@ -24,7 +25,7 @@ public function getChangedContents(): string
}

/**
* @return array<class-string<RectorInterface>>
* @return array<class-string<RectorInterface|PostRectorInterface>>
*/
public function getAppliedRectorClasses(): array
{
Expand Down
3 changes: 2 additions & 1 deletion src/ValueObject/Reporting/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -81,7 +82,7 @@ public function getRectorShortClasses(): array
}

/**
* @return array<class-string<RectorInterface>>
* @return array<class-string<RectorInterface|PostRectorInterface>>
*/
public function getRectorClasses(): array
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Issues\RenameAnnotationToAttributeAutoImport\Fixture;
namespace Rector\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -15,7 +15,7 @@ class WithExistingAttribute extends AbstractController
-----
<?php

namespace Rector\Tests\Issues\RenameAnnotationToAttributeAutoImport\Fixture;
namespace Rector\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand Down
Loading