Skip to content

Commit

Permalink
Updated Rector to commit e3ad355409775a85349aed5ba8e803b7a93fc4dc
Browse files Browse the repository at this point in the history
rectorphp/rector-src@e3ad355 Optimise post rectors (#6240)
  • Loading branch information
TomasVotruba committed Aug 22, 2024
1 parent e6f35f2 commit afeb012
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'be50454d9b4ab919f99471a846f2cd641a2919d1';
public const PACKAGE_VERSION = 'e3ad355409775a85349aed5ba8e803b7a93fc4dc';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-08-22 11:30:51';
public const RELEASE_DATE = '2024-08-22 13:30:44';
/**
* @var int
*/
Expand Down
24 changes: 13 additions & 11 deletions src/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ final class ClassRenamingPostRector extends \Rector\PostRector\Rector\AbstractPo
* @var \Rector\PhpParser\Node\CustomNode\FileWithoutNamespace|\PhpParser\Node\Stmt\Namespace_|null
*/
private $rootNode = null;
/**
* @var array<string, string>
*/
private $oldToNewClasses = [];
public function __construct(ClassRenamer $classRenamer, RenamedClassesDataCollector $renamedClassesDataCollector, UseImportsRemover $useImportsRemover, RenamedNameCollector $renamedNameCollector)
{
$this->classRenamer = $classRenamer;
Expand All @@ -66,16 +70,12 @@ public function enterNode(Node $node) : ?Node
if (!$node instanceof Name) {
return null;
}
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
if ($oldToNewClasses === []) {
return null;
}
/** @var Scope|null $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($node instanceof FullyQualified) {
$result = $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
$result = $this->classRenamer->renameNode($node, $this->oldToNewClasses, $scope);
} else {
$result = $this->resolveResultWithPhpAttributeName($node, $oldToNewClasses, $scope);
$result = $this->resolveResultWithPhpAttributeName($node, $scope);
}
if (!SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES)) {
return $result;
Expand All @@ -96,14 +96,16 @@ public function afterTraverse(array $nodes) : array
$this->renamedNameCollector->reset();
return $nodes;
}
/**
* @param array<string, string> $oldToNewClasses
*/
private function resolveResultWithPhpAttributeName(Name $name, array $oldToNewClasses, ?Scope $scope) : ?FullyQualified
public function shouldTraverse(array $stmts) : bool
{
$this->oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
return $this->oldToNewClasses !== [];
}
private function resolveResultWithPhpAttributeName(Name $name, ?Scope $scope) : ?FullyQualified
{
$phpAttributeName = $name->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
if (\is_string($phpAttributeName)) {
return $this->classRenamer->renameNode(new FullyQualified($phpAttributeName, $name->getAttributes()), $oldToNewClasses, $scope);
return $this->classRenamer->renameNode(new FullyQualified($phpAttributeName, $name->getAttributes()), $this->oldToNewClasses, $scope);
}
return null;
}
Expand Down
23 changes: 14 additions & 9 deletions src/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ final class NameImportingPostRector extends \Rector\PostRector\Rector\AbstractPo
* @var \Rector\PostRector\Guard\AddUseStatementGuard
*/
private $addUseStatementGuard;
/**
* @var array<Use_|GroupUse>
*/
private $currentUses = [];
public function __construct(NameImporter $nameImporter, ClassNameImportSkipper $classNameImportSkipper, UseImportsResolver $useImportsResolver, AliasNameResolver $aliasNameResolver, AddUseStatementGuard $addUseStatementGuard)
{
$this->nameImporter = $nameImporter;
Expand All @@ -51,6 +55,11 @@ public function __construct(NameImporter $nameImporter, ClassNameImportSkipper $
$this->aliasNameResolver = $aliasNameResolver;
$this->addUseStatementGuard = $addUseStatementGuard;
}
public function beforeTraverse(array $nodes)
{
$this->currentUses = $this->useImportsResolver->resolve();
return $nodes;
}
/**
* @return \PhpParser\Node|int|null
*/
Expand All @@ -62,12 +71,11 @@ public function enterNode(Node $node)
if ($node->isSpecialClassName()) {
return null;
}
$currentUses = $this->useImportsResolver->resolve();
if ($this->classNameImportSkipper->shouldSkipName($node, $currentUses)) {
if ($this->classNameImportSkipper->shouldSkipName($node, $this->currentUses)) {
return null;
}
// make use of existing use import
$nameInUse = $this->resolveNameInUse($node, $currentUses);
$nameInUse = $this->resolveNameInUse($node);
if ($nameInUse instanceof Name) {
$nameInUse->setAttribute(AttributeKey::NAMESPACED_NAME, $node->toString());
return $nameInUse;
Expand All @@ -81,20 +89,17 @@ public function shouldTraverse(array $stmts) : bool
{
return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()->getFilePath());
}
/**
* @param array<Use_|GroupUse> $currentUses
*/
private function resolveNameInUse(FullyQualified $fullyQualified, array $currentUses) : ?\PhpParser\Node\Name
private function resolveNameInUse(FullyQualified $fullyQualified) : ?\PhpParser\Node\Name
{
$aliasName = $this->aliasNameResolver->resolveByName($fullyQualified, $currentUses);
$aliasName = $this->aliasNameResolver->resolveByName($fullyQualified, $this->currentUses);
if (\is_string($aliasName)) {
return new Name($aliasName);
}
if (\substr_count($fullyQualified->toCodeString(), '\\') === 1) {
return null;
}
$lastName = $fullyQualified->getLast();
foreach ($currentUses as $currentUse) {
foreach ($this->currentUses as $currentUse) {
foreach ($currentUse->uses as $useUse) {
if ($useUse->name->getLast() !== $lastName) {
continue;
Expand Down
5 changes: 0 additions & 5 deletions src/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use PhpParser\Node\Stmt\UseUse;
use PhpParser\NodeTraverser;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
Expand All @@ -42,9 +40,6 @@ public function enterNode(Node $node) : ?Node
if (!$node instanceof Namespace_ && !$node instanceof FileWithoutNamespace) {
return null;
}
if (!SimpleParameterProvider::provideBoolParameter(Option::REMOVE_UNUSED_IMPORTS)) {
return null;
}
$hasChanged = \false;
$namespaceName = $node instanceof Namespace_ && $node->name instanceof Name ? $node->name : null;
$names = $this->resolveUsedPhpAndDocNames($node);
Expand Down
14 changes: 14 additions & 0 deletions src/PostRector/Rector/UseAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
declare (strict_types=1);
namespace Rector\PostRector\Rector;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeTraverser;
use Rector\CodingStyle\Application\UseImportsAdder;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
Expand Down Expand Up @@ -60,6 +62,18 @@ public function beforeTraverse(array $nodes) : array
}
return $this->resolveNodesWithImportedUses($nodes, $useImportTypes, $constantUseImportTypes, $functionUseImportTypes, $rootNode);
}
public function enterNode(Node $node) : int
{
/**
* We stop the traversal because all the work has already been done in the beforeTraverse() function
*
* Using STOP_TRAVERSAL is usually dangerous as it will stop the processing of all your nodes for all visitors
* but since the PostFileProcessor is using direct new NodeTraverser() and traverse() for only a single
* visitor per execution, using stop traversal here is safe,
* ref https://github.com/rectorphp/rector-src/blob/fc1e742fa4d9861ccdc5933f3b53613b8223438d/src/PostRector/Application/PostFileProcessor.php#L59-L61
*/
return NodeTraverser::STOP_TRAVERSAL;
}
/**
* @param Stmt[] $nodes
* @param FullyQualifiedObjectType[] $useImportTypes
Expand Down

0 comments on commit afeb012

Please sign in to comment.