Skip to content

Incorrect behavior of LogicalToBooleanRector: missing parentheses around composed assignments when used as right-hand side of binary operator #9808

Description

@glop-glop

Bug Report

Subject Details
Rector version last dev-main (rectorphp/rector-src@d67958f4)

Hello,

My apologies for the double bug report, as this one seems closely related to #9807 : I thought assignments were parenthesized correctly (after being rewritten by LogicalToBooleanRector) when they were part of a binary operation, but that's only the case for simple assignments (=). Composed assignments still get rewritten without the required parentheses (and existing parentheses get removed).

Minimal PHP Code Causing Issue

See: https://getrector.com/demo/13531d14-0f84-413a-a23d-3d1337f0e8ab

<?php

$x = 0;
2 * $x += 42 and true; # → true, and $x is 42

$x = 0;
2 * ($x += 42) and true; # → true, and $x is 42

(The issue is the same with all composed assignments.)

This example gets rewritten as:

<?php

$x = 0;
2 * $x += 42 && true; # → 2 instead of true, and $x is 1 instead of 42

$x = 0;
2 * $x += 42 && true; # → 2 instead of true, and $x is 1 instead of 42

Responsible rule: LogicalToBooleanRector

Expected Behaviour

Rector should have added parentheses (or kept the existing ones) around the composed assignment:

<?php

$x = 0;
2 * ($x += 42) && true; # → true, and $x is 42 (OK)

$x = 0;
2 * ($x += 42) && true; # → true, and $x is 42 (OK)

Maybe the test on line 487 of BetterStandardPrinter.php should also handle in a similar fashion the case where the right-hand side of the binary operator is a composed assignment (AssignOp)?

Again, thank you very much! :)

Cheers,
Glop

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions