Skip to content

[dx] Add "if" set with if/else/ternary rules#8199

Merged
TomasVotruba merged 1 commit into
mainfrom
if-set
Jul 22, 2026
Merged

[dx] Add "if" set with if/else/ternary rules#8199
TomasVotruba merged 1 commit into
mainfrom
if-set

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 21, 2026

Copy link
Copy Markdown
Member

Moves if-focused rules out of the code quality set into a dedicated SetList::IF set, also available via withPreparedSets(if: true).

The set groups rules that handle conditions on nullable/object/array values, if/else to ternary, ternary to if, and if merging - 10 rules in total. They are removed from CodeQualityLevel::RULES, so withCodeQualityLevel() positions shift accordingly.

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return RectorConfig::configure()
    ->withSets([SetList::IF]);

Rules and their changes

AlternativeIfToBracketRector - alternative syntax to brackets

-if ($value) :
-    $result = 1;
-else :
-    $result = 2;
-endif;
+if ($value) {
+    $result = 1;
+} else {
+    $result = 2;
+}

CompleteMissingIfElseBracketRector - complete missing brackets

-if ($value)
-    return 1;
+if ($value) {
+    return 1;
+}

InlineIfToExplicitIfRector - inline if to explicit if

-is_null($userId) && $userId = 5;
+if (is_null($userId)) {
+    $userId = 5;
+}

TernaryFalseExpressionToIfRector - ternary with false to if

-$value ? $someMethod->call($value) : false;
+if ($value) {
+    $someMethod->call($value);
+}

ArrayExplicitBoolCompareRector - explicit array condition

 public function run(array $items)
 {
-    if (! $items) {
+    if ($items === []) {
         return 'no items';
     }
 }

ObjectExplicitBoolCompareRector - explicit nullable object condition

 public function run(?\stdClass $item)
 {
-    if (! $item) {
+    if (! $item instanceof \stdClass) {
         return 'empty';
     }
 }

ExplicitBoolCompareRector - explicit bool condition

-if (! count($items)) {
+if (count($items) === 0) {
     return 'no items';
 }

CombineIfRector - merge nested ifs

-if ($cond1) {
-    if ($cond2) {
-        return 'foo';
-    }
+if ($cond1 && $cond2) {
+    return 'foo';
 }

ShortenElseIfRector - else + if to elseif

 if ($cond1) {
     return $action1;
-} else {
-    if ($cond2) {
-        return $action2;
-    }
+} elseif ($cond2) {
+    return $action2;
 }

SimplifyIfElseToTernaryRector - if/else assign to ternary

-if (empty($value)) {
-    $this->arrayBuilt[][$key] = true;
-} else {
-    $this->arrayBuilt[][$key] = $value;
-}
+$this->arrayBuilt[][$key] = empty($value) ? true : $value;

@TomasVotruba TomasVotruba changed the title Add "if" set with if/else/ternary rules [dx] Add "if" set with if/else/ternary rules Jul 21, 2026
@TomasVotruba
TomasVotruba merged commit 29c7d78 into main Jul 22, 2026
66 checks passed
@TomasVotruba
TomasVotruba deleted the if-set branch July 22, 2026 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant