From c053b9795a81bb9fd5b8d59e05c7556f19267893 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 26 Jun 2024 16:42:44 +0300 Subject: [PATCH] CurrentFileProvider should never be access by rule (#226) * FileProvider should never be access by rule * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action --- ...wngradeNullsafeToTernaryOperatorRector.php | 41 +++++-------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php index aec684f8..24e9e78b 100644 --- a/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php +++ b/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php @@ -12,9 +12,7 @@ use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; -use Rector\Provider\CurrentFileProvider; use Rector\Rector\AbstractRector; -use Rector\ValueObject\Application\File; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -25,13 +23,16 @@ final class DowngradeNullsafeToTernaryOperatorRector extends AbstractRector { private int $counter = 0; - private ?string $previousFileName = null; - - private ?string $currentFileName = null; - - public function __construct( - private readonly CurrentFileProvider $currentFileProvider - ) { + /** + * Hack-ish way to reset counter for a new file, to avoid rising counter for each file + * + * @param Node[] $nodes + * @return array|Node[]|null + */ + public function beforeTraverse(array $nodes): ?array + { + $this->counter = 0; + return parent::beforeTraverse($nodes); } public function getRuleDefinition(): RuleDefinition @@ -62,22 +63,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Ternary { - if ($this->previousFileName === null) { - $previousFile = $this->currentFileProvider->getFile(); - if (! $previousFile instanceof File) { - return null; - } - - $this->previousFileName = $previousFile->getFilePath(); - } - - $currentFile = $this->currentFileProvider->getFile(); - if (! $currentFile instanceof File) { - return null; - } - - $this->currentFileName = $currentFile->getFilePath(); - $nullsafeVariable = $this->createNullsafeVariable(); $methodCallOrPropertyFetch = $node instanceof NullsafeMethodCall @@ -91,13 +76,7 @@ public function refactor(Node $node): ?Ternary private function createNullsafeVariable(): Variable { - if ($this->previousFileName !== $this->currentFileName) { - $this->counter = 0; - $this->previousFileName = $this->currentFileName; - } - $nullsafeVariableName = 'nullsafeVariable' . ++$this->counter; - return new Variable($nullsafeVariableName); } }