From 3a029efff37e2e0fc5d6e8baf940b38eb990f5ea Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 24 Oct 2026 00:23:39 +0200 Subject: [PATCH] add local VariableNaming service, drop dependency on rector-src class --- .../FuncCall/DowngradeStreamIsattyRector.php | 2 +- .../DowngradeArrayKeyFirstLastRector.php | 2 +- .../DowngradeNonCapturingCatchesRector.php | 2 +- .../DowngradeMatchToSwitchRector.php | 2 +- ...eflectionClassGetConstantsFilterRector.php | 2 +- ...eSetAccessibleReflectionPropertyRector.php | 2 +- ...radeReflectionMethodHasPrototypeRector.php | 2 +- .../FuncCall/DowngradeJsonValidateRector.php | 2 +- .../Expression/DowngradeArrayAllRector.php | 2 +- .../Expression/DowngradeArrayAnyRector.php | 2 +- .../DowngradeArrayFindKeyRector.php | 2 +- .../Expression/DowngradeArrayFindRector.php | 2 +- .../Expression/DowngradeVoidCastRector.php | 2 +- src/DowngradePhp/Naming/VariableNaming.php | 32 +++++++++++++++++++ src/NodeFactory/NamedVariableFactory.php | 2 +- 15 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/DowngradePhp/Naming/VariableNaming.php diff --git a/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php b/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php index fbd5b27f..df166500 100644 --- a/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php +++ b/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php @@ -14,8 +14,8 @@ use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PHPStan\Analyser\Scope; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\Exception\ShouldNotHappenException; -use Rector\Naming\Naming\VariableNaming; use Rector\NodeAnalyzer\ExprInTopStmtMatcher; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Enum\NodeGroup; diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php index d4ab3a17..b8ed8fdd 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php @@ -25,7 +25,7 @@ use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PHPStan\Analyser\Scope; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\NodeAnalyzer\ExprInTopStmtMatcher; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Enum\NodeGroup; diff --git a/rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php b/rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php index 1179fb14..96538d6b 100644 --- a/rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php +++ b/rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Catch_; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; diff --git a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php index 26d08ffe..db708c51 100644 --- a/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php +++ b/rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php @@ -31,7 +31,7 @@ use PhpParser\Node\Stmt\Switch_; use PhpParser\NodeVisitor; use PHPStan\Analyser\Scope; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php72\NodeFactory\AnonymousFunctionFactory; use Rector\PHPStan\ScopeFetcher; diff --git a/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionClassGetConstantsFilterRector.php b/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionClassGetConstantsFilterRector.php index 3121edda..a42660db 100644 --- a/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionClassGetConstantsFilterRector.php +++ b/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionClassGetConstantsFilterRector.php @@ -21,7 +21,7 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use PHPStan\Type\ObjectType; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\NodeCollector\BinaryOpConditionsCollector; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; diff --git a/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php b/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php index 00c0c77a..f602c4d7 100644 --- a/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php +++ b/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php @@ -20,7 +20,7 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; use PHPStan\Type\ObjectType; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PhpParser\Enum\NodeGroup; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; diff --git a/rules/DowngradePhp82/Rector/MethodCall/DowngradeReflectionMethodHasPrototypeRector.php b/rules/DowngradePhp82/Rector/MethodCall/DowngradeReflectionMethodHasPrototypeRector.php index 9cec9933..d72de116 100644 --- a/rules/DowngradePhp82/Rector/MethodCall/DowngradeReflectionMethodHasPrototypeRector.php +++ b/rules/DowngradePhp82/Rector/MethodCall/DowngradeReflectionMethodHasPrototypeRector.php @@ -20,7 +20,7 @@ use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\TryCatch; use PHPStan\Type\ObjectType; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; diff --git a/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php b/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php index 2fe5b4fe..a2dad32d 100644 --- a/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php +++ b/rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php @@ -15,8 +15,8 @@ use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PHPStan\Analyser\Scope; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\Exception\ShouldNotHappenException; -use Rector\Naming\Naming\VariableNaming; use Rector\NodeAnalyzer\ExprInTopStmtMatcher; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Enum\NodeGroup; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 2228ef25..ac7292f1 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -18,7 +18,7 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php index be3d05ed..c8849be2 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php @@ -17,7 +17,7 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php index 1fed7b9c..aaf8e58d 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php @@ -17,7 +17,7 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php index 525b2688..ad138a89 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php @@ -17,7 +17,7 @@ use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; diff --git a/rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php b/rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php index f5cfc271..be43d5d2 100644 --- a/rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php +++ b/rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php @@ -9,7 +9,7 @@ use PhpParser\Node\Expr\Cast\Void_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Expression; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; diff --git a/src/DowngradePhp/Naming/VariableNaming.php b/src/DowngradePhp/Naming/VariableNaming.php new file mode 100644 index 00000000..b0dea877 --- /dev/null +++ b/src/DowngradePhp/Naming/VariableNaming.php @@ -0,0 +1,32 @@ +hasVariableType($valueName)->yes()) { + return $valueName; + } + + // we need to add number suffix until the variable is unique + $i = 2; + $countedValueNamePart = $valueName; + while ($scope->hasVariableType($valueName)->yes()) { + $valueName = $countedValueNamePart . $i; + ++$i; + } + + return $valueName; + } +} diff --git a/src/NodeFactory/NamedVariableFactory.php b/src/NodeFactory/NamedVariableFactory.php index 0bdad2cd..7aaa45c5 100644 --- a/src/NodeFactory/NamedVariableFactory.php +++ b/src/NodeFactory/NamedVariableFactory.php @@ -7,7 +7,7 @@ use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Expression; -use Rector\Naming\Naming\VariableNaming; +use Rector\DowngradePhp\Naming\VariableNaming; use Rector\NodeTypeResolver\Node\AttributeKey; final readonly class NamedVariableFactory