From 4cd13ba498bd9d0889bfa16219a59c871d427145 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 12 May 2022 15:49:26 +0200 Subject: [PATCH 1/4] exact attribute type --- src/Rector/AbstractRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 93f6af6db0a..9d8f8ba66a8 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -50,7 +50,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorInterface { /** - * @var string[] + * @var array */ private const ATTRIBUTES_TO_MIRROR = [ AttributeKey::SCOPE, From 43638271b8b9f9d53dce24ad1d58ef456340f3f2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 12 May 2022 15:53:33 +0200 Subject: [PATCH 2/4] add debug file + rule on --debug for easier output --- src/Rector/AbstractRector.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 9d8f8ba66a8..1a567ba4f56 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -21,6 +21,7 @@ use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Core\Application\ChangedNodeScopeRefresher; use Rector\Core\Configuration\CurrentNodeProvider; +use Rector\Core\Console\Output\RectorOutputStyle; use Rector\Core\Contract\Rector\PhpRectorInterface; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Exclusion\ExclusionManager; @@ -123,6 +124,8 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn private UnreachableStmtAnalyzer $unreachableStmtAnalyzer; + private RectorOutputStyle $rectorOutputStyle; + #[Required] public function autowire( NodesToRemoveCollector $nodesToRemoveCollector, @@ -145,7 +148,8 @@ public function autowire( RectifiedAnalyzer $rectifiedAnalyzer, CreatedByRuleDecorator $createdByRuleDecorator, ChangedNodeScopeRefresher $changedNodeScopeRefresher, - UnreachableStmtAnalyzer $unreachableStmtAnalyzer + UnreachableStmtAnalyzer $unreachableStmtAnalyzer, + RectorOutputStyle $rectorOutputStyle, ): void { $this->nodesToRemoveCollector = $nodesToRemoveCollector; $this->nodesToAddCollector = $nodesToAddCollector; @@ -168,6 +172,7 @@ public function autowire( $this->createdByRuleDecorator = $createdByRuleDecorator; $this->changedNodeScopeRefresher = $changedNodeScopeRefresher; $this->unreachableStmtAnalyzer = $unreachableStmtAnalyzer; + $this->rectorOutputStyle = $rectorOutputStyle; } /** @@ -216,6 +221,8 @@ final public function enterNode(Node $node) $originalAttributes = $node->getAttributes(); + $this->printDebugCurrentFileAndRule(); + $node = $this->refactor($node); // nothing to change → continue @@ -435,4 +442,18 @@ private function connectParentNodes(Node $node): void $nodeTraverser->addVisitor(new ParentConnectingVisitor()); $nodeTraverser->traverse([$node]); } + + private function printDebugCurrentFileAndRule(): void + { + if (! $this->rectorOutputStyle->isDebug()) { + return; + } + + $this->rectorOutputStyle->writeln('[file] ' . $this->file->getRelativeFilePath()); + + // prevent spamming with the same class over and over + // indented on purpose to improve log nesting under [refactoring] + $this->rectorOutputStyle->writeln('[rule] ' . static::class); + $this->rectorOutputStyle->newLine(1); + } } From b4bb8ae4123a4d60a952f3ec1dab3408075639be Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 12 May 2022 16:17:19 +0200 Subject: [PATCH 3/4] make DowngradeNumericLiteralSeparatorRector keep the type --- ...DowngradeNumericLiteralSeparatorRector.php | 39 ++++++++++++------- src/Rector/AbstractRector.php | 5 +-- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/rules/DowngradePhp74/Rector/LNumber/DowngradeNumericLiteralSeparatorRector.php b/rules/DowngradePhp74/Rector/LNumber/DowngradeNumericLiteralSeparatorRector.php index b5d006cac32..bfd8ae97437 100644 --- a/rules/DowngradePhp74/Rector/LNumber/DowngradeNumericLiteralSeparatorRector.php +++ b/rules/DowngradePhp74/Rector/LNumber/DowngradeNumericLiteralSeparatorRector.php @@ -64,17 +64,21 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->shouldRefactor($node)) { + if ($this->shouldSkip($node)) { return null; } - $numberNode = clone $node; - $numberNodeValue = (string) $numberNode->value; - if (\str_contains($numberNodeValue, '+')) { + $numberValueAsString = (string) $node->value; + if (\str_contains($numberValueAsString, '+')) { return null; } - $node->value = (string) $node->value; + // trigger reprint + $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + + if ($node instanceof LNumber) { + return $node; + } /** * This code follows a guess, to avoid modifying floats needlessly. @@ -83,24 +87,33 @@ public function refactor(Node $node): ?Node * by adding ".0" at the end (eg: 0.0). * Then, add it again. */ - if ($node instanceof DNumber && ! \str_contains($node->value, '.')) { - $node->value .= '.0'; + if (! \str_contains($numberValueAsString, '.')) { + $numberValueAsString .= '.0'; } - if (! str_contains($node->value, '_')) { - return null; - } + $node->value = (float) $numberValueAsString; return $node; } - public function shouldRefactor(LNumber | DNumber $node): bool + private function shouldSkip(LNumber | DNumber $node): bool { // "_" notation can be applied to decimal numbers only if ($node instanceof LNumber) { - return $node->getAttribute(AttributeKey::KIND) === LNumber::KIND_DEC; + $numberKind = $node->getAttribute(AttributeKey::KIND); + if ($numberKind !== LNumber::KIND_DEC) { + return true; + } + } + + // we have to hack around tokens to get original value, see https://github.com/nikic/PHP-Parser/pull/832 + $oldTokens = $this->file->getOldTokens(); + $tokenValue = $oldTokens[$node->getStartTokenPos()][1] ?? null; + + if ($tokenValue === null) { + return true; } - return true; + return ! str_contains((string) $tokenValue, '_'); } } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 1a567ba4f56..b24b256f32b 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -51,7 +51,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorInterface { /** - * @var array + * @var array */ private const ATTRIBUTES_TO_MIRROR = [ AttributeKey::SCOPE, @@ -450,9 +450,6 @@ private function printDebugCurrentFileAndRule(): void } $this->rectorOutputStyle->writeln('[file] ' . $this->file->getRelativeFilePath()); - - // prevent spamming with the same class over and over - // indented on purpose to improve log nesting under [refactoring] $this->rectorOutputStyle->writeln('[rule] ' . static::class); $this->rectorOutputStyle->newLine(1); } From 0f0d5f419db9c8d279df2cf8e285c9762d2a1a86 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 12 May 2022 14:25:34 +0000 Subject: [PATCH 4/4] [ci-review] Rector Rectify --- src/Rector/AbstractRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index b24b256f32b..76eb50dad02 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -51,7 +51,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorInterface { /** - * @var array + * @var string[] */ private const ATTRIBUTES_TO_MIRROR = [ AttributeKey::SCOPE,