Skip to content

Commit

Permalink
[Core] Always reset stopTraversal to false on next Rector visit (#4182)
Browse files Browse the repository at this point in the history
* [Core] Always reset stopTraversal to false on next Rector visit

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* rename property

 Conflicts:
	src/PhpParser/NodeTraverser/RectorNodeTraverser.php

* update fixture

* Fix phpstan

* remove just reprinted verify on RectifiedAnalyzer

* increase kernel cache key

* clean up

* Revert "remove just reprinted verify on RectifiedAnalyzer"

This reverts commit 66ba1ba.

* remove docblock update after refactor

* [ci-review] Rector Rectify

* update kernel cache key

* final touch: comment

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 11, 2023
1 parent 405d9f8 commit bb609b2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 37 deletions.
17 changes: 0 additions & 17 deletions packages/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,6 @@ public function updateNodeWithPhpDocInfo(Node $node): void
$node->setDocComment(new Doc($phpDoc));
}

public function updateRefactoredNodeWithPhpDocInfo(Node $node): void
{
// nothing to change? don't save it
$phpDocInfo = $this->resolveChangedPhpDocInfo($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}

$phpDocNode = $phpDocInfo->getPhpDocNode();
if ($phpDocNode->children === []) {
$this->setCommentsAttribute($node);
return;
}

$node->setDocComment(new Doc((string) $phpDocNode));
}

private function setCommentsAttribute(Node $node): void
{
if ($node->hasAttribute(AttributeKey::PREVIOUS_DOCS_AS_COMMENTS)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v94';
private const CACHE_KEY = 'v96';

private ContainerInterface|null $container = null;

Expand Down
6 changes: 5 additions & 1 deletion src/PhpParser/NodeFinder/PropertyFetchFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ private function isInAnonymous(PropertyFetch $propertyFetch, Class_|Trait_ $clas
return false;
}

return $classReflection->getName() !== $this->nodeNameResolver->getName($class) && ! $hasTrait;
if ($classReflection->getName() === $this->nodeNameResolver->getName($class)) {
return false;
}

return ! $hasTrait;
}

private function isNamePropertyNameEquals(
Expand Down
19 changes: 15 additions & 4 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\VersionBonding\PhpVersionedFilter;

final class RectorNodeTraverser extends NodeTraverser
{
private bool $areNodeVisitorsPrepared = false;

/** @var PhpRectorInterface[]|NodeVisitor[] */
private array $activePhpRectors = [];

/**
* @param PhpRectorInterface[] $phpRectors
*/
Expand All @@ -31,7 +35,16 @@ public function __construct(
public function traverse(array $nodes): array
{
$this->prepareNodeVisitors();
return parent::traverse($nodes);

foreach ($this->activePhpRectors as $activePhpRector) {
$this->visitors = [$activePhpRector];

// call parent::traverse() on loop to ensure
// stopTraversal always reset to false before run on next Rector rule
$nodes = parent::traverse($nodes);
}

return $nodes;
}

/**
Expand All @@ -48,9 +61,7 @@ private function prepareNodeVisitors(): void

// filer out by version
$activePhpRectors = $this->phpVersionedFilter->filter($this->phpRectors);
$this->visitors = $this->visitors === []
? $activePhpRectors
: array_merge($this->visitors, $activePhpRectors);
$this->activePhpRectors = array_merge($this->visitors, $activePhpRectors);

$this->areNodeVisitorsPrepared = true;
}
Expand Down
11 changes: 0 additions & 11 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Application\ChangedNodeScopeRefresher;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Console\Output\RectorOutputStyle;
Expand Down Expand Up @@ -104,8 +103,6 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn

private FilePathHelper $filePathHelper;

private DocBlockUpdater $docBlockUpdater;

private NodeConnectingTraverser $nodeConnectingTraverser;

private ?string $toBeRemovedNodeHash = null;
Expand All @@ -130,7 +127,6 @@ public function autowire(
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
RectorOutputStyle $rectorOutputStyle,
FilePathHelper $filePathHelper,
DocBlockUpdater $docBlockUpdater,
NodeConnectingTraverser $nodeConnectingTraverser
): void {
$this->nodeNameResolver = $nodeNameResolver;
Expand All @@ -151,7 +147,6 @@ public function autowire(
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
$this->rectorOutputStyle = $rectorOutputStyle;
$this->filePathHelper = $filePathHelper;
$this->docBlockUpdater = $docBlockUpdater;
$this->nodeConnectingTraverser = $nodeConnectingTraverser;
}

Expand Down Expand Up @@ -392,12 +387,6 @@ private function refreshScopeNodes(array | Node $node, string $filePath, ?Mutati
$nodes = $node instanceof Node ? [$node] : $node;

foreach ($nodes as $node) {
/**
* Early refresh Doc Comment of Node before refresh Scope to ensure doc node is latest update
* to make PHPStan type can be correctly detected
*/
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

$this->changedNodeScopeRefresher->refresh($node, $mutatingScope, $filePath);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class NewInArray
/**
* @param array<string, object> $property
*/
public function __construct(array $property = null)
public function __construct(?array $property = null)
{
$property ??= [
$this->property = $property;
$this->property = $property ?? [
'a' => new stdClass()
];
$this->property = $property;
}
}

Expand Down

0 comments on commit bb609b2

Please sign in to comment.