From 01d87573f701dfc09861e03327e0f954e9d9dbb2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 17 Oct 2026 16:48:12 +0200 Subject: [PATCH 1/2] deprecate TernaryFalseExpressionToIfRector, too niche for wide application --- .../include_no_side_effect_expr.php.inc | 41 ------------------- .../Fixture/negated_value.php.inc | 29 ------------- .../skip_increment_variable_on_else.php.inc | 30 -------------- .../Fixture/skip_method_call_on_else.php.inc | 22 ---------- ...ip_method_call_with_assign_on_else.php.inc | 22 ---------- .../Fixture/skip_null_ternary.php.inc | 11 ----- .../Fixture/skip_sort_ksort.php.inc | 21 ---------- .../Fixture/skip_with_html.php.inc | 4 -- .../Fixture/some_class.php.inc | 29 ------------- .../TernaryFalseExpressionToIfRectorTest.php | 28 ------------- .../config/configured_rule.php | 9 ---- .../TernaryFalseExpressionToIfRector.php | 40 +++--------------- src/Config/Level/CodeQualityLevel.php | 2 - .../config/configured_rule.php | 3 +- 14 files changed, 6 insertions(+), 285 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/include_no_side_effect_expr.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/negated_value.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_increment_variable_on_else.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_on_else.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_with_assign_on_else.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_null_ternary.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_sort_ksort.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_with_html.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/some_class.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/TernaryFalseExpressionToIfRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/config/configured_rule.php diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/include_no_side_effect_expr.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/include_no_side_effect_expr.php.inc deleted file mode 100644 index 15251bb4368..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/include_no_side_effect_expr.php.inc +++ /dev/null @@ -1,41 +0,0 @@ -call($value) : true; - } - - public function run2($value, $someMethod, $variable) - { - $value ? $someMethod->call($value) : $variable; - } -} - -?> ------ -call($value); - } - } - - public function run2($value, $someMethod, $variable) - { - if ($value) { - $someMethod->call($value); - } - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/negated_value.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/negated_value.php.inc deleted file mode 100644 index aebb2154725..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/negated_value.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_increment_variable_on_else.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_increment_variable_on_else.php.inc deleted file mode 100644 index 8a7291e065e..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_increment_variable_on_else.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - $failed) { - return $success; - } - - return $failed; - } - - public function run2($id) - { - $success = $failed = 0; - ! $id ? ++$failed : ++$success; - - if ($success > $failed) { - return $success; - } - - return $failed; - } -} \ No newline at end of file diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_on_else.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_on_else.php.inc deleted file mode 100644 index 97b4351625b..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_on_else.php.inc +++ /dev/null @@ -1,22 +0,0 @@ -a() : $this->b(); - } - - public function a(): void - { - new HeavyObject1(); - } - - public function b(): null - { - new HeavyObject2(); - return 'b'; - } -} diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_with_assign_on_else.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_with_assign_on_else.php.inc deleted file mode 100644 index f585e3eb474..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_method_call_with_assign_on_else.php.inc +++ /dev/null @@ -1,22 +0,0 @@ -a() : $x = $this->b(); - } - - public function a(): void - { - new HeavyObject1(); - } - - public function b(): null - { - new HeavyObject2(); - return 'b'; - } -} diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_null_ternary.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_null_ternary.php.inc deleted file mode 100644 index e4838167739..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_null_ternary.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -call($value); - } -} diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_sort_ksort.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_sort_ksort.php.inc deleted file mode 100644 index 764174ff393..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_sort_ksort.php.inc +++ /dev/null @@ -1,21 +0,0 @@ - $result && is_numeric($item), true); - $isIndexedArray - ? sort($array) - : ksort($array); - - foreach ($array as &$value) { - if (is_array($value)) { - self::mksort($value); - } - } - } -} diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_with_html.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_with_html.php.inc deleted file mode 100644 index 04db19849be..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_with_html.php.inc +++ /dev/null @@ -1,4 +0,0 @@ - -call($a, 'v') ? ' class="x"' : '' ?>> diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/some_class.php.inc b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/some_class.php.inc deleted file mode 100644 index 0aa00170ea4..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,29 +0,0 @@ -call($value) : false; - } -} - -?> ------ -call($value); - } - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/TernaryFalseExpressionToIfRectorTest.php b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/TernaryFalseExpressionToIfRectorTest.php deleted file mode 100644 index b34002f7e3a..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/TernaryFalseExpressionToIfRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/config/configured_rule.php deleted file mode 100644 index 13fe1623e29..00000000000 --- a/rules-tests/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([TernaryFalseExpressionToIfRector::class]); diff --git a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php index 97beb61566d..7d23e1a12ec 100644 --- a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php +++ b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php @@ -5,26 +5,18 @@ namespace Rector\CodeQuality\Rector\Expression; use PhpParser\Node; -use PhpParser\Node\Expr; -use PhpParser\Node\Expr\Ternary; -use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Expression; -use PhpParser\Node\Stmt\If_; -use Rector\NodeAnalyzer\ExprAnalyzer; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\TernaryFalseExpressionToIfRectorTest + * @deprecated This rule is deprecated, as too niche for wide application. */ -final class TernaryFalseExpressionToIfRector extends AbstractRector +final class TernaryFalseExpressionToIfRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly ExprAnalyzer $exprAnalyzer - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change ternary with false to if and explicit call', [ @@ -63,30 +55,8 @@ public function getNodeTypes(): array return [Expression::class]; } - /** - * @param Expression $node - */ public function refactor(Node $node): ?Node { - if ($this->getFile()->containsHTML()) { - return null; - } - - if (! $node->expr instanceof Ternary) { - return null; - } - - $ternary = $node->expr; - if (! $ternary->if instanceof Expr) { - return null; - } - - if (! $ternary->else instanceof Variable && $this->exprAnalyzer->isDynamicExpr($ternary->else)) { - return null; - } - - return new If_($ternary->cond, [ - 'stmts' => [new Expression($ternary->if)], - ]); + throw new ShouldNotHappenException(sprintf('"%s" rule is deprecated, as too niche for wide application', self::class)); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index fe242112a26..2e491f9774c 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -27,7 +27,6 @@ use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector; use Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector; use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector; -use Rector\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector; use Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector; use Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector; use Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector; @@ -146,7 +145,6 @@ final class CodeQualityLevel VariableConstFetchToClassConstFetchRector::class, SingularSwitchToIfRector::class, InlineIfToExplicitIfRector::class, - TernaryFalseExpressionToIfRector::class, SimplifyIfNullableReturnRector::class, InlineArrayReturnAssignRector::class, RemoveAlwaysElseRector::class, diff --git a/tests/Issues/ChangeSwitchTernary/config/configured_rule.php b/tests/Issues/ChangeSwitchTernary/config/configured_rule.php index 9bfc3eec3ea..fde969b0bf0 100644 --- a/tests/Issues/ChangeSwitchTernary/config/configured_rule.php +++ b/tests/Issues/ChangeSwitchTernary/config/configured_rule.php @@ -2,12 +2,11 @@ declare(strict_types=1); -use Rector\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector; use Rector\Config\RectorConfig; use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector; use Rector\Tests\Issues\ChangeSwitchTernary\Source\AnotherExpressionRector; return RectorConfig::configure() ->withRules( - [ChangeSwitchToMatchRector::class, TernaryFalseExpressionToIfRector::class, AnotherExpressionRector::class] + [ChangeSwitchToMatchRector::class, AnotherExpressionRector::class] ); From b8c036e0b0b220d54541accdebd1570a6ad1b9d7 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 17 Oct 2026 14:49:20 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Rector/Expression/TernaryFalseExpressionToIfRector.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php index 7d23e1a12ec..6e34b2b0b81 100644 --- a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php +++ b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php @@ -55,6 +55,9 @@ public function getNodeTypes(): array return [Expression::class]; } + /** + * @param Expression $node + */ public function refactor(Node $node): ?Node { throw new ShouldNotHappenException(sprintf('"%s" rule is deprecated, as too niche for wide application', self::class));