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

Arrow functions and and, or and xor, precedence differs from PHP #769

Closed
sebsel opened this issue Apr 10, 2021 · 1 comment
Closed

Arrow functions and and, or and xor, precedence differs from PHP #769

sebsel opened this issue Apr 10, 2021 · 1 comment

Comments

@sebsel
Copy link

sebsel commented Apr 10, 2021

I am here because I filed this bug at PHPStan: phpstan/phpstan#4824. It got labelled as 'easy fixes', so I looked into it. While I still don't understand that codebase at all, it seems to use PHP-Parser as parser, so the obvious place was to start here.

See also the PHPStan playground that I made for that issue: https://phpstan.org/r/35cfa468-01c1-4aab-b8e8-816b0b3a4b66


Basicly when I use an arrow function in combination with the and, or and xor keywords, PHP places the operator inside of the function, but PHP-Parser places the function inside of the operator...

So the example:

fn($a, $b) => $a and $b;
fn($a, $b) => $a && $b;

... is interpreted as:

// by PHP (it seems?)
fn($a, $b) => ($a and $b);
fn($a, $b) => ($a && $b);

// by PHP-Parser
(fn($a, $b) => $a) and $b;
fn($a, $b) => ($a && $b);

Current parsed result (I removed some properties to shorten the snippet):

array(
    0: Stmt_Expression(
        expr: Expr_BinaryOp_LogicalAnd(
            left: Expr_ArrowFunction(
                params: array(
                    0: Param(
                        var: Expr_Variable(
                            name: a
                        )
                    )
                    1: Param(
                        var: Expr_Variable(
                            name: b
                        )
                    )
                )
                expr: Expr_Variable(
                    name: a
                )
            )
            right: Expr_Variable(
                name: b
            )
        )
    )
    1: Stmt_Expression(
        expr: Expr_ArrowFunction(
            params: array(
                0: Param(
                    var: Expr_Variable(
                        name: a
                    )
                )
                1: Param(
                    var: Expr_Variable(
                        name: b
                    )
                )
            )
            expr: Expr_BinaryOp_BooleanAnd(
                left: Expr_Variable(
                    name: a
                )
                right: Expr_Variable(
                    name: b
                )
            )
        )
    )
)

Edit: cleaned up the example

@nikic
Copy link
Owner

nikic commented Apr 10, 2021

Nice find!

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

No branches or pull requests

2 participants