mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-04 02:10:51 +00:00
7d36c3b0b9
a80cf5292d
revert to working scoper 0.14
68 lines
1.9 KiB
PHP
68 lines
1.9 KiB
PHP
<?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 \Rector\Core\Rector\AbstractRector
|
|
{
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove ternary that duplicated return value of true : false', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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 [\PhpParser\Node\Expr\Ternary::class];
|
|
}
|
|
/**
|
|
* @param Ternary $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$this->nodeTypeResolver->isStaticType($node->cond, \PHPStan\Type\BooleanType::class)) {
|
|
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;
|
|
}
|
|
}
|