Skip to content

Commit 6f458fe

Browse files
authored
Make exposed downgrade rules idempotent on repeated application (#409)
1 parent f2ec312 commit 6f458fe

7 files changed

Lines changed: 69 additions & 4 deletions

File tree

‎phpstan.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ parameters:
116116
- rules/DowngradePhp80/Rector/FuncCall/DowngradeSubstrFalsyRector.php
117117
- rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php
118118
- rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php
119+
- rules/DowngradePhp81/Rector/MethodCall/DowngradeIsEnumRector.php
119120
- rules/DowngradePhp73/Rector/String_/DowngradeFlexibleHeredocSyntaxRector.php
120121
- rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php
121122

‎rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ private function refactorExpression(Expression $expression): ?array
169169
return null;
170170
}
171171

172+
// clean the flag from the call args here; returning an array replaces the Expression,
173+
// so the traverser never descends into the ConstFetch to clean it, and the expression
174+
// would match again every pass and keep appending the error check
175+
foreach ($funcCall->getArgs() as $arg) {
176+
if (! $arg->value instanceof ConstFetch && ! $arg->value instanceof BitwiseOr) {
177+
continue;
178+
}
179+
180+
$cleanedValue = $this->jsonConstCleaner->clean($arg->value, [JsonConstant::THROW_ON_ERROR]);
181+
if ($cleanedValue instanceof Expr) {
182+
$arg->value = $cleanedValue;
183+
}
184+
}
185+
172186
$nodes = [$expression];
173187
$nodes[] = new If_(
174188
new NotIdentical(

‎rules/DowngradePhp73/Rector/String_/DowngradeFlexibleHeredocSyntaxRector.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ final class DowngradeFlexibleHeredocSyntaxRector extends AbstractRector
2323
*/
2424
private const array HERENOW_DOC_KINDS = [String_::KIND_HEREDOC, String_::KIND_NOWDOC];
2525

26+
/**
27+
* Marker left on the doc indentation attribute once flattened, so a later pass skips the node
28+
*/
29+
private const string REMOVED_DOC_INDENTATION = '__REMOVED__';
30+
2631
public function __construct(
2732
private readonly FollowedByNewlineOnlyMaybeWithSemicolonAnalyzer $followedByNewlineOnlyMaybeWithSemicolonAnalyzer
2833
) {
@@ -80,7 +85,12 @@ public function refactor(Node $node): ?Node
8085
return null;
8186
}
8287

83-
$node->setAttribute(AttributeKey::DOC_INDENTATION, '__REMOVED__');
88+
// already flattened on a previous pass, re-running would keep appending newlines
89+
if ($docIndentation === self::REMOVED_DOC_INDENTATION) {
90+
return null;
91+
}
92+
93+
$node->setAttribute(AttributeKey::DOC_INDENTATION, self::REMOVED_DOC_INDENTATION);
8494
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
8595

8696
$tokens = $this->getFile()

‎rules/DowngradePhp74/Rector/ClassMethod/DowngradeCovariantReturnTypeRector.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ private function resolveMatchingReturnType(
244244
continue;
245245
}
246246

247+
// only downgrade to a wider ancestor type; skipping a narrower one keeps the rule
248+
// idempotent, otherwise a multi-level hierarchy flips the type back and forth
249+
if (! $parentReturnType->accepts($returnType, true)->yes()) {
250+
continue;
251+
}
252+
247253
// This is an ancestor class with a different return type
248254
return $parentReturnType;
249255
}

‎rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public function refactor(Node $node): ?FuncCall
5959

6060
$firstArg = $node->getArgs()[0];
6161

62+
// already wrapped by this rule on a previous pass; the wrapped ternary type is string|array,
63+
// so the string check below does not catch it and the rule would keep nesting the wrap
64+
if ($firstArg->value instanceof Ternary
65+
&& $firstArg->value->cond instanceof FuncCall
66+
&& $this->isName($firstArg->value->cond, 'is_array')) {
67+
return null;
68+
}
69+
6270
$commandType = $this->getType($firstArg->value);
6371
if ($commandType->isString()->yes()) {
6472
return null;

‎rules/DowngradePhp81/Rector/MethodCall/DowngradeIsEnumRector.php‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr;
10+
use PhpParser\Node\Expr\FuncCall;
911
use PhpParser\Node\Expr\MethodCall;
1012
use PhpParser\Node\Expr\Ternary;
1113
use PhpParser\Node\Scalar\String_;
@@ -19,6 +21,8 @@
1921
*/
2022
final class DowngradeIsEnumRector extends AbstractRector
2123
{
24+
private const string IS_IF_TERNARY = 'is_if_ternary';
25+
2226
public function getRuleDefinition(): RuleDefinition
2327
{
2428
return new RuleDefinition('Downgrades isEnum() on class reflection', [
@@ -52,18 +56,33 @@ public function run(ReflectionClass $reflectionClass)
5256
*/
5357
public function getNodeTypes(): array
5458
{
55-
return [MethodCall::class];
59+
return [Ternary::class, MethodCall::class];
5660
}
5761

5862
/**
59-
* @param MethodCall $node
63+
* @param Ternary|MethodCall $node
6064
*/
6165
public function refactor(Node $node): ?Node
6266
{
67+
if ($node instanceof Ternary) {
68+
if ($node->if instanceof Expr
69+
&& $node->cond instanceof FuncCall
70+
&& $this->isName($node->cond, 'method_exists')) {
71+
$node->if->setAttribute(self::IS_IF_TERNARY, true);
72+
}
73+
74+
return null;
75+
}
76+
6377
if (! $this->isName($node->name, 'isEnum')) {
6478
return null;
6579
}
6680

81+
// already wrapped by this rule on a previous pass
82+
if ($node->getAttribute(self::IS_IF_TERNARY) === true) {
83+
return null;
84+
}
85+
6786
if (! $this->isObjectType($node->var, new ObjectType('ReflectionClass'))) {
6887
return null;
6988
}

‎rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ public function refactor(Node $node): ?Node
6262
return null;
6363
}
6464

65-
if ((string) $oldTokens[$node->getStartTokenPos()] === '(') {
65+
// source already wraps the new in parentheses
66+
if ((string) $oldTokens[$startTokenPos] === '(') {
67+
return null;
68+
}
69+
70+
// idempotency: on a previous pass the opening paren was prepended to the New_ var token,
71+
// so check that token too, otherwise the rule keeps wrapping the same call every pass
72+
if (str_starts_with((string) $oldTokens[$node->var->getStartTokenPos()], '(')) {
6673
return null;
6774
}
6875

0 commit comments

Comments
 (0)