Skip to content

Commit 42f2c85

Browse files
committed
remove node visitor stop from DowngradeHashAlgorithmXxHashRector
1 parent c9aabad commit 42f2c85

File tree

3 files changed

+12
-77
lines changed

3 files changed

+12
-77
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,7 @@ parameters:
9999
-
100100
identifier: symplify.seeAnnotationToTest
101101
path: rules/DowngradePhp85/Rector/StmtsAwareInterface/DowngradePipeOperatorRector.php
102+
103+
-
104+
identifier: rector.noIntegerRefactorReturn
105+
path: rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php

rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9-
use PhpParser\Node\Expr;
10-
use PhpParser\Node\Expr\Assign;
119
use PhpParser\Node\Expr\ConstFetch;
1210
use PhpParser\Node\Expr\FuncCall;
13-
use PhpParser\Node\Expr\Ternary;
1411
use PhpParser\Node\Scalar\String_;
15-
use PhpParser\Node\Stmt\Expression;
16-
use PhpParser\Node\Stmt\If_;
17-
use PhpParser\Node\Stmt\Return_;
18-
use PhpParser\NodeVisitor;
1912
use PHPStan\Type\IntegerRangeType;
20-
use Rector\DeadCode\ConditionResolver;
21-
use Rector\DeadCode\ValueObject\VersionCompareCondition;
2213
use Rector\NodeAnalyzer\ArgsAnalyzer;
14+
use Rector\NodeTypeResolver\Node\AttributeKey;
2315
use Rector\PhpParser\Node\Value\ValueResolver;
2416
use Rector\PHPStan\ScopeFetcher;
2517
use Rector\Rector\AbstractRector;
@@ -48,7 +40,6 @@ final class DowngradeHashAlgorithmXxHashRector extends AbstractRector
4840
public function __construct(
4941
private readonly ArgsAnalyzer $argsAnalyzer,
5042
private readonly ValueResolver $valueResolver,
51-
private readonly ConditionResolver $conditionResolver
5243
) {
5344
}
5445

@@ -87,32 +78,19 @@ public function run()
8778
*/
8879
public function getNodeTypes(): array
8980
{
90-
return [If_::class, Ternary::class, FuncCall::class];
81+
return [FuncCall::class];
9182
}
9283

9384
/**
94-
* @param If_|Ternary|FuncCall $node
95-
* @return null|NodeVisitor::DONT_TRAVERSE_CHILDREN|Node
85+
* @param FuncCall $node
9686
*/
97-
public function refactor(Node $node): null|int|Node
87+
public function refactor(Node $node): ?FuncCall
9888
{
99-
if ($node instanceof If_) {
100-
if ($this->isVersionCompareIf($node)) {
101-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
102-
}
103-
104-
return null;
105-
}
106-
107-
if ($node instanceof Ternary) {
108-
if ($this->isVersionCompareTernary($node)) {
109-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
110-
}
111-
89+
if ($this->shouldSkip($node)) {
11290
return null;
11391
}
11492

115-
if ($this->shouldSkip($node)) {
93+
if ($node->getAttribute(AttributeKey::PHP_VERSION_CONDITIONED)) {
11694
return null;
11795
}
11896

@@ -134,54 +112,6 @@ public function refactor(Node $node): null|int|Node
134112
return $node;
135113
}
136114

137-
private function isVersionCompareIf(If_ $if): bool
138-
{
139-
if ($if->cond instanceof FuncCall) {
140-
// per use case reported only
141-
if (count($if->stmts) !== 1) {
142-
return false;
143-
}
144-
145-
$versionCompare = $this->conditionResolver->resolveFromExpr($if->cond);
146-
147-
if (! $versionCompare instanceof VersionCompareCondition || $versionCompare->getSecondVersion() !== 80100) {
148-
return false;
149-
}
150-
151-
if ($versionCompare->getCompareSign() !== '>=') {
152-
return false;
153-
}
154-
155-
if ($if->stmts[0] instanceof Expression && $if->stmts[0]->expr instanceof Assign && $if->stmts[0]->expr->expr instanceof FuncCall) {
156-
return $this->isName($if->stmts[0]->expr->expr, 'hash');
157-
}
158-
159-
if ($if->stmts[0] instanceof Return_ && $if->stmts[0]->expr instanceof FuncCall) {
160-
return $this->isName($if->stmts[0]->expr, 'hash');
161-
}
162-
}
163-
164-
return false;
165-
}
166-
167-
private function isVersionCompareTernary(Ternary $ternary): bool
168-
{
169-
if ($ternary->if instanceof Expr && $ternary->cond instanceof FuncCall) {
170-
$versionCompare = $this->conditionResolver->resolveFromExpr($ternary->cond);
171-
if ($versionCompare instanceof VersionCompareCondition && $versionCompare->getSecondVersion() === 80100) {
172-
if ($versionCompare->getCompareSign() === '>=') {
173-
return $ternary->if instanceof FuncCall && $this->isName($ternary->if, 'hash');
174-
}
175-
176-
if ($versionCompare->getCompareSign() === '<') {
177-
return $ternary->else instanceof FuncCall && $this->isName($ternary->else, 'hash');
178-
}
179-
}
180-
}
181-
182-
return false;
183-
}
184-
185115
private function shouldSkip(FuncCall $funcCall): bool
186116
{
187117
if ($funcCall->isFirstClassCallable()) {

rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ function test(array|Traversable $data) {
6868

6969
/**
7070
* @param Ternary|FuncCall $node
71+
* @return null|FuncCall|NodeVisitor::DONT_TRAVERSE_CHILDREN
7172
*/
72-
public function refactor(Node $node): null|FuncCall
73+
public function refactor(Node $node): null|FuncCall|int
7374
{
7475
if ($node instanceof Ternary) {
7576
$hasIsArrayCheck = (bool) $this->betterNodeFinder->findFirst(

0 commit comments

Comments
 (0)