2019-10-13 05:59:52 +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\CodeQuality\Rector\LogicalAnd;
|
2018-12-16 16:35:01 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-16 16:35:01 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://stackoverflow.com/a/5998330/1348344
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector\LogicalToBooleanRectorTest
|
2018-12-16 16:35:01 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class LogicalToBooleanRector extends AbstractRector
|
2018-12-16 16:35:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-16 16:35:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change OR, AND to ||, && with more common understanding', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-16 16:35:01 +00:00
|
|
|
if ($f = false or true) {
|
|
|
|
return $f;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-16 16:35:01 +00:00
|
|
|
if (($f = false) || true) {
|
|
|
|
return $f;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-16 16:35:01 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-16 16:35:01 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-16 16:35:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [LogicalOr::class, LogicalAnd::class];
|
2018-12-16 16:35:01 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param LogicalOr|LogicalAnd $node
|
2018-12-16 16:35:01 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-16 16:35:01 +00:00
|
|
|
{
|
2021-09-01 04:09:40 +00:00
|
|
|
return $this->refactorLogicalToBoolean($node);
|
|
|
|
}
|
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Expr\BinaryOp\LogicalOr|\PhpParser\Node\Expr\BinaryOp\LogicalAnd $node
|
2021-09-01 04:09:40 +00:00
|
|
|
* @return \PhpParser\Node\Expr\BinaryOp\BooleanAnd|\PhpParser\Node\Expr\BinaryOp\BooleanOr
|
|
|
|
*/
|
|
|
|
private function refactorLogicalToBoolean($node)
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->left instanceof LogicalOr || $node->left instanceof LogicalAnd) {
|
2021-09-01 04:09:40 +00:00
|
|
|
$node->left = $this->refactorLogicalToBoolean($node->left);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->right instanceof LogicalOr || $node->right instanceof LogicalAnd) {
|
2021-09-01 04:09:40 +00:00
|
|
|
$node->right = $this->refactorLogicalToBoolean($node->right);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof LogicalOr) {
|
|
|
|
return new BooleanOr($node->left, $node->right);
|
2019-02-23 10:53:44 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new BooleanAnd($node->left, $node->right);
|
2018-12-16 16:35:01 +00:00
|
|
|
}
|
|
|
|
}
|