Plugin-php: Removes parentheses when "and" is combined with ternary expression

Created on 11 Dec 2019  路  7Comments  路  Source: prettier/plugin-php

@prettier/plugin-php v0.11.2
Playground link

Input:

<?php

$someVeryVeryLongValue1 = 1;
$someVeryVeryLongValue2 = 2;

var_dump(
    ($someVeryVeryLongValue1 and $someVeryVeryLongValue2)
        ? 'true value'
        : 'false value'
);

Output:

<?php

$someVeryVeryLongValue1 = 1;
$someVeryVeryLongValue2 = 2;

var_dump(
    $someVeryVeryLongValue1 and $someVeryVeryLongValue2
        ? 'true value'
        : 'false value'
);

The above two are not equal in execution.
It would be fine to do this for the && operator, as that has a higher precedence than the ternary, but the same is not true for "and":
https://www.php.net/manual/en/language.operators.precedence.php

bug

All 7 comments

PR welcome

Any starting points you would recommend?

@czosel @evilebottnawi this is still an issue in v12.

@prettier/plugin-php v0.12.0
Playground link

Strange. I鈥檒l try to look into this in the coming days.

Ah, the answer is simple: The fix is not part of the 0.12.0 release - it should be working in master though.

All right, guess I'll just wait for the next release and re-test then.

Was this page helpful?
0 / 5 - 0 ratings