Skip to content

Commit 96b55a7

Browse files
committed
skip negated
1 parent 5f00ac7 commit 96b55a7

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class SkipNegatedCond
6+
{
7+
public function go(bool $value)
8+
{
9+
return ! $value ?: false;
10+
}
11+
}

rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\Array_;
10+
use PhpParser\Node\Expr\BooleanNot;
1011
use PhpParser\Node\Expr\Ternary;
1112
use PhpParser\Node\Scalar\Int_;
1213
use PHPStan\Type\ArrayType;
@@ -65,6 +66,12 @@ public function refactor(Node $node): ?Node
6566
return null;
6667
}
6768

69+
// if condition is negated
70+
// switch negated ternary condition via SwitchNegatedTernaryRector for that
71+
if ($node->cond instanceof BooleanNot) {
72+
return null;
73+
}
74+
6875
$nativeType = $this->nodeTypeResolver->getNativeType($node->cond);
6976
if ($nativeType instanceof BooleanType && $this->valueResolver->isFalse($node->else)) {
7077
return $node->cond;

0 commit comments

Comments
 (0)