Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class Fixture
{
public function go(bool $value)
{
return $value ?: false;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class Fixture
{
public function go(bool $value)
{
return $value;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class SkipDifferentType
{
public function go(bool $value)
{
return $value ?: 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class SkipDocblockType
{
/**
* @param bool $value
*/
public function go($value)
{
return $value ?: false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class SkipNegatedCond
{
public function go(bool $value)
{
return ! $value ?: false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class SkipUnion
{
public function go(bool|string $value)
{
return $value ?: false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class SkipWithIfNotEqual
{
public function go(bool $value)
{
return $value ? 1 : false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class TrueFalseOnBool
{
public function go(bool $value)
{
return $value ? true : false;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class TrueFalseOnBool
{
public function go(bool $value)
{
return $value;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class WithArrayEmpty
{
public function go(array $value)
{
return $value ?: [];
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class WithArrayEmpty
{
public function go(array $value)
{
return $value;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class WithIfEqual
{
public function go(bool $value)
{
return $value ? $value : false;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class WithIfEqual
{
public function go(bool $value)
{
return $value;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class WithIntegerZero
{
public function go(int $value)
{
return $value ?: 0;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;

class WithIntegerZero
{
public function go(int $value)
{
return $value;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveUselessTernaryRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Ternary\RemoveUselessTernaryRector;

return RectorConfig::configure()
->withRules([RemoveUselessTernaryRector::class]);
100 changes: 100 additions & 0 deletions rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Rector\Ternary;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\Int_;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\IntegerType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\RemoveUselessTernaryRectorTest
*/
final class RemoveUselessTernaryRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove useless ternary if fallback is falsey of left code', [
new CodeSample(
<<<'CODE_SAMPLE'
function go(bool $value)
{
return $value ?: false;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function go(bool $value)
{
return $value;
}
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Ternary::class];
}

/**
* @param Ternary $node
*/
public function refactor(Node $node): ?Node
{
/**
* if condition is negated, skip
* switch negated ternary condition early via SwitchNegatedTernaryRector for that
* if needed
*/
if ($node->cond instanceof BooleanNot) {
return null;
}

$nativeType = $this->nodeTypeResolver->getNativeType($node->cond);
if ($nativeType instanceof BooleanType
&& $node->if instanceof Expr
&& $this->valueResolver->isTrue($node->if)
&& $this->valueResolver->isFalse($node->else)) {
return $node->cond;
}

if ($node->if instanceof Expr && ! $this->nodeComparator->areNodesEqual($node->if, $node->cond)) {
return null;
}

if ($nativeType instanceof BooleanType && $this->valueResolver->isFalse($node->else)) {
return $node->cond;
}

if ($nativeType instanceof ArrayType && $node->else instanceof Array_ && $node->else->items === []) {
return $node->cond;
}

if ($nativeType instanceof IntegerType && $node->else instanceof Int_ && $node->else->value === 0) {
return $node->cond;
}

return null;
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/DeadCodeLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use Rector\DeadCode\Rector\Stmt\RemoveNextSameValueConditionRector;
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
use Rector\DeadCode\Rector\Ternary\RemoveUselessTernaryRector;
use Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector;
use Rector\DeadCode\Rector\TryCatch\RemoveDeadCatchRector;
use Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector;
Expand Down Expand Up @@ -100,6 +101,7 @@ final class DeadCodeLevel

RemoveTypedPropertyDeadInstanceOfRector::class,
TernaryToBooleanOrFalseToBooleanAndRector::class,
RemoveUselessTernaryRector::class,
RemoveDoubleAssignRector::class,
RemoveUselessAssignFromPropertyPromotionRector::class,
RemoveConcatAutocastRector::class,
Expand Down
Loading