Skip to content

PrettyPrinter brokes nested ternaries #718

@rentalhost

Description

@rentalhost

Code like:

<?php ( $a ? $b : $c ) ? $d : $e;

Is parsed correctly:

array(
    0: Stmt_Expression(
        expr: Expr_Ternary(
            cond: Expr_Ternary(
                cond: Expr_Variable( name: a )
                if: Expr_Variable( name: b )
                else: Expr_Variable( name: c )
            )
            if: Expr_Variable( name: d )
            else: Expr_Variable( name: e )
        )
    )
)

But PrettyPrinter will outputs:

<?php $a ? $b : $c ? $d : $e;

So if you run the code it will gives:

Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions