merge SimplifyDuplicatedTernaryRector to UnnecessaryTernaryExpressionRector, almost identical rule (#2046)

This commit is contained in:
Tomas Votruba 2022-04-10 21:01:30 +02:00 committed by GitHub
parent a2ee46d16e
commit 9b8e161878
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 6 additions and 135 deletions

View File

@ -68,7 +68,6 @@ use Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector;
use Rector\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector;
use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector;
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
use Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector;
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
@ -110,7 +109,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ExplicitBoolCompareRector::class);
$services->set(CombineIfRector::class);
$services->set(UseIdenticalOverEqualWithSameTypeRector::class);
$services->set(SimplifyDuplicatedTernaryRector::class);
$services->set(SimplifyBoolIdenticalTrueRector::class);
$services->set(SimplifyRegexPatternRector::class);
$services->set(BooleanNotIdenticalToNotIdenticalRector::class);

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class SimplifyDuplicatedTernaryRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SimplifyDuplicatedTernaryRector::class);
};

View File

@ -1,8 +1,8 @@
<?php
namespace Rector\Tests\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
class Fixture
final class SimpleExample
{
public function run(bool $value, string $name)
{
@ -15,14 +15,14 @@ class Fixture
-----
<?php
namespace Rector\Tests\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
class Fixture
final class SimpleExample
{
public function run(bool $value, string $name)
{
$isTrue = $value;
$isName = $name ? true : false;
$isName = (bool) $name;
}
}

View File

@ -1,83 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Ternary;
use PhpParser\Node;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Type\BooleanType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector\SimplifyDuplicatedTernaryRectorTest
*/
final class SimplifyDuplicatedTernaryRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'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 array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Ternary::class];
}
/**
* @param Ternary $node
*/
public function refactor(Node $node): ?Node
{
$condType = $this->getType($node->cond);
if (! $condType instanceof BooleanType) {
return null;
}
if ($node->if === null) {
return null;
}
if (! $this->valueResolver->isTrue($node->if)) {
return null;
}
if (! $this->valueResolver->isFalse($node->else)) {
return null;
}
return $node->cond;
}
}

View File

@ -29,7 +29,7 @@ final class UnnecessaryTernaryExpressionRector extends AbstractRector
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove unnecessary ternary expressions.',
'Remove unnecessary ternary expressions',
[new CodeSample('$foo === $bar ? true : false;', '$foo === $bar;')]
);
}