diff --git a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php index be8e73a7458..73892d533ae 100644 --- a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php +++ b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php @@ -71,6 +71,7 @@ public function refactor(Node $node): ?Node $oldTokens = $this->getFile() ->getOldTokens(); + if ($this->isIfConditionFollowedByOpeningCurlyBracket($node, $oldTokens)) { return null; } diff --git a/src/PostRector/Rector/AbstractPostRector.php b/src/PostRector/Rector/AbstractPostRector.php index 341100666b9..b12d58aeed9 100644 --- a/src/PostRector/Rector/AbstractPostRector.php +++ b/src/PostRector/Rector/AbstractPostRector.php @@ -29,7 +29,7 @@ public function setFile(File $file): void $this->file = $file; } - public function getFile(): File + protected function getFile(): File { Assert::isInstanceOf($this->file, File::class); @@ -41,6 +41,7 @@ protected function addRectorClassWithLine(Node $node): void Assert::isInstanceOf($this->file, File::class); $rectorWithLineChange = new RectorWithLineChange(static::class, $node->getStartLine()); - $this->file->addRectorClassWithLine($rectorWithLineChange); + $this->getFile() + ->addRectorClassWithLine($rectorWithLineChange); } } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 63770a565f5..36ab2ae666d 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -99,24 +99,22 @@ public function autowire( } /** - * @final Avoid override to prevent unintended side-effects. Use enterNode() or @see \Rector\Contract\PhpParser\DecoratingNodeVisitorInterface instead. + * @return Node[]|null * * @internal - * - * @return Node[]|null */ - public function beforeTraverse(array $nodes): ?array + final public function beforeTraverse(array $nodes): ?array { - // workaround for file around refactor() - $file = $this->currentFileProvider->getFile(); - if (! $file instanceof File) { - throw new ShouldNotHappenException( - 'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.' - ); - } - - $this->file = $file; + return null; + } + /** + * @return Node[]|null + * + * @internal + */ + final public function afterTraverse(array $nodes) + { return null; } @@ -125,12 +123,14 @@ public function beforeTraverse(array $nodes): ?array */ final public function enterNode(Node $node): int|Node|null|array { - if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->getFile()->containsHTML()) { + // keep $this->file populated for BC; refactor() is only ever reached through here + $this->file = $this->getFile(); + + if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) { return null; } - $filePath = $this->getFile() - ->getFilePath(); + $filePath = $this->file->getFilePath(); if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) { return null; } @@ -163,8 +163,7 @@ final public function enterNode(Node $node): int|Node|null|array // notify this rule changed code $rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine()); - $this->getFile() - ->addRectorClassWithLine($rectorWithLineChange); + $this->file->addRectorClassWithLine($rectorWithLineChange); return $refactoredNodeOrState; } @@ -274,8 +273,7 @@ private function postRefactorProcess( $this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class); $rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine()); - $this->getFile() - ->addRectorClassWithLine($rectorWithLineChange); + $this->file->addRectorClassWithLine($rectorWithLineChange); /** @var MutatingScope|null $currentScope */ $currentScope = $node->getAttribute(AttributeKey::SCOPE);