Skip to content

Commit

Permalink
[CodeQuality] Handle recursive on LogicalToBooleanRector (#808)
Browse files Browse the repository at this point in the history
* fix: recursively call rector until no node is a LogicalOr or LogicalAnd

* added fixture for mixed logical and binary

* phpstan

* inline instanceof checks

* fix: recursively call rector until no node is a LogicalOr or LogicalAnd

* phpstan

* inline instanceof checks

* rebase + apply rector

* fix: recursively call rector until no node is a LogicalOr or LogicalAnd

* added fixture for mixed logical and binary

* phpstan

* inline instanceof checks

* rebase + apply rector

* move refactor logic into new function

* move refactor logic into new function

* eol

Co-authored-by: Bl00D4NGEL <kuhlesdominik@gmx.de>
  • Loading branch information
samsonasik and Bl00D4NGEL committed Sep 1, 2021
1 parent 511a076 commit 0f9e4f8
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

function run($val1, $val2, $val3, $val4)
{
if (
(
1 === $val1 or
1 === $val2 ||
1 === $val3 or
1 === $val4
)
) {
//code
}

if (
(
1 === $val1 and
1 === $val2 &&
1 === $val3 and
1 === $val4
)
) {
//code
}
}

?>
-----
<?php

function run($val1, $val2, $val3, $val4)
{
if (
(
1 === $val1 || (1 === $val2 ||
1 === $val3) || 1 === $val4
)
) {
//code
}

if (
(
1 === $val1 && (1 === $val2 &&
1 === $val3) && 1 === $val4
)
) {
//code
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

function run($val1, $val2, $val3, $val4)
{
if (
(
1 === $val1 and
1 === $val2 and
1 === $val3 and
1 === $val4
)
) {
//code
}
}

?>
-----
<?php

function run($val1, $val2, $val3, $val4)
{
if (
(
1 === $val1 && 1 === $val2 && 1 === $val3 && 1 === $val4
)
) {
//code
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

function run($val1, $val2, $val3, $val4)
{
if (
(
1 === $val1 or
1 === $val2 or
1 === $val3 or
1 === $val4
)
) {
//code
}
}

?>
-----
<?php

function run($val1, $val2, $val3, $val4)
{
if (
(
1 === $val1 || 1 === $val2 || 1 === $val3 || 1 === $val4
)
) {
//code
}
}

?>
17 changes: 17 additions & 0 deletions rules/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
return $this->refactorLogicalToBoolean($node);
}

private function refactorLogicalToBoolean(LogicalOr|LogicalAnd $node): BooleanAnd|BooleanOr
{
if ($node->left instanceof LogicalOr || $node->left instanceof LogicalAnd) {
$node->left = $this->refactorLogicalToBoolean(
$node->left,
);
}

if ($node->right instanceof LogicalOr || $node->right instanceof LogicalAnd) {
$node->right = $this->refactorLogicalToBoolean(
$node->right,
);
}

if ($node instanceof LogicalOr) {
return new BooleanOr($node->left, $node->right);
}
Expand Down

0 comments on commit 0f9e4f8

Please sign in to comment.