mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-25 04:03:55 +01:00
Merge pull request #1120 from rectorphp/dead-ternary
[CodeQuality] Add SimplifyDuplicatedTernaryRector
This commit is contained in:
commit
e76e1adab8
@ -26,3 +26,4 @@ services:
|
||||
Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector: ~
|
||||
Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector: ~
|
||||
Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector: ~
|
||||
Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector: ~
|
||||
|
@ -0,0 +1,69 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\Ternary;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class SimplifyDuplicatedTernaryRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Remove ternary that duplicated return value of true : false', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(bool $value, string $name)
|
||||
{
|
||||
$isTrue = $value ? true : false;
|
||||
$isName = $name ? true : false;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(bool $value, string $name)
|
||||
{
|
||||
$isTrue = $value;
|
||||
$isName = $name ? true : false;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Ternary::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Ternary $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isBoolType($node->cond)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node->if === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isTrue($node->if) || ! $this->isFalse($node->else)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $node->cond;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\SimplifyDuplicatedTernaryRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run(bool $value, string $name)
|
||||
{
|
||||
$isTrue = $value ? true : false;
|
||||
$isName = $name ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\SimplifyDuplicatedTernaryRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run(bool $value, string $name)
|
||||
{
|
||||
$isTrue = $value;
|
||||
$isName = $name ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\SimplifyDuplicatedTernaryRector;
|
||||
|
||||
use Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class SimplifyDuplicatedTernaryRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return SimplifyDuplicatedTernaryRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user