PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Bug: Concatenation changes the order of elements by prettyprinter

Open jorgsowa opened this issue 1 year ago • 2 comments

Given the code:

<?php
('abc' . 1) + 2;

And running the parser on the original and PrettyPrinter format we get different AST.

Original:

array(
    0: Stmt_Expression(
        expr: Expr_BinaryOp_Plus(
            left: Expr_BinaryOp_Concat(
                left: Scalar_String(
                    value: abc
                )
                right: Scalar_Int(
                    value: 1
                )
            )
            right: Scalar_Int(
                value: 2
            )
        )
    )
)

Prettied:

array(
    0: Stmt_Expression(
        expr: Expr_BinaryOp_Concat(
            left: Scalar_String(
                value: abc
            )
            right: Expr_BinaryOp_Plus(
                left: Scalar_Int(
                    value: 1
                )
                right: Scalar_Int(
                    value: 2
                )
            )
        )
    )
)

jorgsowa avatar Nov 18 '24 21:11 jorgsowa

I assume you actually meant:

<?php
('abc' . 1) + 2;

The print is correct for PHP 7, but not for PHP 8, which changed concat precedence.

nikic avatar Nov 19 '24 20:11 nikic

Yes, I meant concatenation, sorry.

Even for a single PHP version, the most recent one 8.4.1, PrettyPrinter changes the precedence of operations and the output is different in two cases: https://3v4l.org/KD1RL#v8.4.1

jorgsowa avatar Nov 24 '24 00:11 jorgsowa