diff --git a/rules/DowngradePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector.php b/rules/DowngradePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector.php index ce64c1c9..ee7c05eb 100644 --- a/rules/DowngradePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector.php +++ b/rules/DowngradePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector.php @@ -67,10 +67,11 @@ public function getNodeTypes(): array /** * @param ConstFetch|BitwiseOr|If_ $node */ - public function refactor(Node $node): Expr|If_|null + public function refactor(Node $node): Expr|null { if ($node instanceof If_) { - return $this->refactorIf($node); + $this->markConstantKnownInInnerStmts($node); + return null; } // skip as known @@ -84,20 +85,18 @@ public function refactor(Node $node): Expr|If_|null ]); } - private function refactorIf(If_ $if): ?If_ + private function markConstantKnownInInnerStmts(If_ $if): void { if (! $this->defineFuncCallAnalyzer->isDefinedWithConstants($if->cond, [ JsonConstant::INVALID_UTF8_IGNORE, JsonConstant::INVALID_UTF8_SUBSTITUTE, ])) { - return null; + return; } $this->traverseNodesWithCallable($if, static function (Node $node): null { $node->setAttribute(self::PHP72_JSON_CONSTANT_IS_KNOWN, true); return null; }); - - return $if; } } diff --git a/rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php b/rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php index 660903ab..b4e51b27 100644 --- a/rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php +++ b/rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php @@ -102,12 +102,13 @@ public function getNodeTypes(): array /** * @param ConstFetch|BitwiseOr|If_|TryCatch|Expression $node - * @return null|Expr|If_|array + * @return null|Expr|array */ - public function refactor(Node $node): null|Expr|If_|array + public function refactor(Node $node): null|Expr|array { if ($node instanceof If_) { - return $this->refactorIf($node); + $this->markConstantKnownInInnerStmts($node); + return null; } // skip as known @@ -146,18 +147,16 @@ function (Node $subNode): ?int { return $this->jsonConstCleaner->clean($node, [JsonConstant::THROW_ON_ERROR]); } - private function refactorIf(If_ $if): ?If_ + private function markConstantKnownInInnerStmts(If_ $if): void { if (! $this->defineFuncCallAnalyzer->isDefinedWithConstants($if->cond, [JsonConstant::THROW_ON_ERROR])) { - return null; + return; } $this->traverseNodesWithCallable($if, static function (Node $node): null { $node->setAttribute(self::PHP73_JSON_CONSTANT_IS_KNOWN, true); return null; }); - - return $if; } private function resolveFuncCall(Expression $Expression): ?FuncCall diff --git a/rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php b/rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php index 65dcf3bd..21c42dfc 100644 --- a/rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php +++ b/rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php @@ -77,11 +77,13 @@ public function getNodeTypes(): array public function refactor(Node $node): Node|null { if ($node instanceof Instanceof_) { - return $this->refactorInstanceof($node); + $this->markSkipInstanceof($node); + return null; } if ($node instanceof Ternary) { - return $this->refactorTernary($node); + $this->markSkipTernary($node); + return null; } if ($node->getAttribute(self::SKIP_NODE) === true) { @@ -105,36 +107,34 @@ public function refactor(Node $node): Node|null ); } - private function refactorInstanceof(Instanceof_ $instanceof): ?Instanceof_ + private function markSkipInstanceof(Instanceof_ $instanceof): void { if (! $this->isName($instanceof->class, 'ReflectionNamedType')) { - return null; + return; } if (! $instanceof->expr instanceof MethodCall) { - return null; + return; } // checked typed → safe $instanceof->expr->setAttribute(self::SKIP_NODE, true); - return $instanceof; } - private function refactorTernary(Ternary $ternary): ?Ternary + private function markSkipTernary(Ternary $ternary): void { if (! $ternary->if instanceof Expr) { - return null; + return; } if (! $ternary->cond instanceof FuncCall) { - return null; + return; } if (! $this->isName($ternary->cond, 'method_exists')) { - return null; + return; } $ternary->if->setAttribute(self::SKIP_NODE, true); - return $ternary; } }