Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] LogicalToBooleanRector fix left or right argument causing recursion error #786

Conversation

Bl00D4NGEL
Copy link
Contributor

refs rectorphp/rector#6667
closes rectorphp/rector#6667

The problem originated from $node->left and/or $node->right being an instance of PhpParser\Node\Expr\BinaryOp\LogicalAnd or PhpParser\Node\Expr\BinaryOp\LogicalOr which made rector think it detected a recursion.

Fixing this by recursively calling the refactor method on the left and right properties of the operator until it is not an instance of LogicalOr or LogicalAnd anymore

@Bl00D4NGEL
Copy link
Contributor Author

All checks have passed 🎉 Please review @TomasVotruba

Co-authored-by: GitHub Action <action@github.com>
Comment on lines 67 to 70
if ($this->shouldRecursivelyCall($right)) {
/** @var BooleanAnd|BooleanOr $right */
$right = $this->refactor($right);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recurse call should not be needed, as Rector rule works untill there is a code to be fixed. It's recursive by nature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original problem stems from the "infinite loop detection" in \Rector\Core\Validation\InfiniteLoopValidator::process.

The test fixtures multi_or and multi_and will run into an InfiniteLoopTraversingException.

If I use print_node on the offending node(s) this is the output:
'(1 === $val1 or 1 === $val2) || 1 === $val3'
and
'(1 === $val1 and 1 === $val2) && 1 === $val3'

Not sure if this is now a bug in the InfiniteLoopValidator or if it is intended and my recursion is approach is the correct one in this case

samsonasik and others added 5 commits August 30, 2021 10:16
…UnusedAssignVariableRector (rectorphp#789)

Co-authored-by: Bl00D4NGEL <kuhlesdominik@gmx.de>
…ctor (rectorphp#791)

* failing fixture null item on ManualJsonStringToJsonEncodeArrayRector

* fix null item on ManualJsonStringToJsonEncodeArrayRector

* phpstan
…ctorphp#792)

* [CodingStyle] Add more statements for NewlineAfterStatementRector

* cs fix
}

return new BooleanAnd($node->left, $node->right);
return new BooleanAnd($left, $right);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole code inside refactor can be moved to separate method, eg: processLogicalToBoolean(), so the recursive method is in the new method.

For clean rebase, I created a blogpost about it: https://samsonasik.wordpress.com/2015/09/16/practical-git-4-rebasing-conflicted-task-branch-against-primary-branch/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the code, is this what you wanted?

…n NewlineAfterStatementRector (rectorphp#793)

* [CodingStyle] Failing fixture complex if on NewlineAfterStatementRector

* [CodingStyle] Handle complex If_ ElseIf_ Else_ on on NewlineAfterStatementRector

* more example

* refactor

* naming

* fix
@samsonasik
Copy link
Member

@Bl00D4NGEL there is conflict now, please clean rebase to avoid conflict with unrelated files.

… + ChangeAndIfToEarlyReturnRector + DateTimeToDateTimeInterfaceRector (rectorphp#794)
…EarlyReturnRector + ChangeAndIfToEarlyReturnRector + AddArrayReturnDocTypeRector + NarrowUnionTypeDocRector (rectorphp#795)
…ector (rectorphp#797)

* [EarlyReturn] Skip same return expr on ChangeOrIfReturnToEarlyReturnRector

* fixture update

* phpstan

* rollback handling tweak on NarrowUnionTypeDocRector

* tweak
…arlyReturnRector (rectorphp#799)

* [EarlyReturn] Handle left or right is BooleanAnd on ChangeOrIfReturnToEarlyReturnRector

* skip

* remove tweak in DateTimeToDateTimeInterfaceRector

* naming

* remove tweak in RemoveAlwaysElseRector

* rename fixture
samsonasik and others added 11 commits August 31, 2021 22:07
…rectorphp#800)

* [EarlyReturn] Clean up ChangeOrIfReturnToEarlyReturnRector skip check

* more cleaning up
…or' of https://github.com/Bl00D4NGEL/rector-src into prevent-recursion-error-in-LogicalToBooleanRector"

This reverts commit 35d2f8e, reversing
changes made to 15cce15.
…ogicalToBooleanRector' into prevent-recursion-error-in-LogicalToBooleanRector

# Conflicts:
#	rules-tests/CodingStyle/Rector/Stmt/NewlineAfterStatementRector/Fixture/no_new_line_switch.php.inc
#	rules/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector.php
#	rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php
#	tests/Issues/IssueEarlyReturnAndOrDatetime/IssueEarlyReturnAndOrDatetimeTest.php
#	tests/Issues/IssueEarlyReturnAndOrNarrow/IssueEarlyReturnAndOrNarrowTest.php
…ror-in-LogicalToBooleanRector' into prevent-recursion-error-in-LogicalToBooleanRector"

This reverts commit 5ee4a2a, reversing
changes made to 740fd3c.
@Bl00D4NGEL
Copy link
Contributor Author

I seem to be struggling way too much with git.. I hope I did it correctly now?

@TomasVotruba TomasVotruba enabled auto-merge (squash) August 31, 2021 21:55
@TomasVotruba
Copy link
Member

Thank you, looks very clean now 👍

@TomasVotruba
Copy link
Member

TomasVotruba commented Aug 31, 2021

@Bl00D4NGEL I tried to rebase locally, but failed after 30 mins :D could you re-open new PR with single commit? It would save us some rebase hell

@samsonasik
Copy link
Member

samsonasik commented Sep 1, 2021

@Bl00D4NGEL I rebased your commits at PR #808

@Bl00D4NGEL Bl00D4NGEL deleted the prevent-recursion-error-in-LogicalToBooleanRector branch September 1, 2021 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error from LogicalToBooleanRector
3 participants