2019-12-26 19:29:10 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\Ternary;
|
2019-12-26 19:29:10 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-12-26 19:29:10 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector\TernaryToBooleanOrFalseToBooleanAndRectorTest
|
2019-12-26 19:29:10 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class TernaryToBooleanOrFalseToBooleanAndRector extends AbstractRector
|
2019-12-26 19:29:10 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-12-26 19:29:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change ternary of bool : false to && bool', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-12-26 19:29:10 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
return $value ? $this->getBool() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getBool(): bool
|
|
|
|
{
|
|
|
|
return (bool) 5;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-12-26 19:29:10 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
return $value && $this->getBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getBool(): bool
|
|
|
|
{
|
|
|
|
return (bool) 5;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-12-26 19:29:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-12-26 19:29:10 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-12-26 19:29:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Ternary::class];
|
2019-12-26 19:29:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Ternary $node
|
2019-12-26 19:29:10 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-12-26 19:29:10 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$node->if instanceof Expr) {
|
2019-12-26 19:29:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isFalse($node->else)) {
|
2019-12-26 19:29:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isTrue($node->if)) {
|
2020-02-25 21:00:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-07 17:46:41 +00:00
|
|
|
$ifType = $this->getType($node->if);
|
2023-04-08 16:37:58 +00:00
|
|
|
if (!$ifType->isBoolean()->yes()) {
|
2019-12-26 19:29:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new BooleanAnd($node->cond, $node->if);
|
2019-12-26 19:29:10 +00:00
|
|
|
}
|
|
|
|
}
|