Skip to content

Pin php-parser to version 3.4.0#2501

Open
LordSalmon wants to merge 2 commits intoprettier:mainfrom
LordSalmon:pin-parser-3.4.0
Open

Pin php-parser to version 3.4.0#2501
LordSalmon wants to merge 2 commits intoprettier:mainfrom
LordSalmon:pin-parser-3.4.0

Conversation

@LordSalmon
Copy link
Copy Markdown
Contributor

@LordSalmon LordSalmon commented May 11, 2026

Cheers :)

Last week we ran into a strange situation, where our PHP code was formatted weirdly. Some examples are illustrated below:

$foo = 5 * -1 . "foo";
$foo = 5 * -1 + 2;

// would be formatted to

$foo = 5 * -(1 . "foo"); // results in a "Warning: A non-numeric value encountered"
$foo = 5 * -(1 + 2); // results in a completely different result

This happened due to an unpinned version of the php-parser package where in version 3.4.0+ (for sure 3.5.1) a bug occurred which produces the wrong AST. Now, every part after a Unary gets grouped inside that unary.

Examples are shown below.

Unfortunately I don't have the time right now to try and fix the bug in php-parser, but for now, pinning the version should suffice.

AST Representation at version 3.4.0:

{
  "kind": "program",
  "loc": {
    "source": null,
    "start": {
      "line": 1,
      "column": 0,
      "offset": 0
    },
    "end": {
      "line": 1,
      "column": 11,
      "offset": 11
    }
  },
  "children": [
    {
      "kind": "expressionstatement",
      "loc": {
        "source": null,
        "start": {
          "line": 1,
          "column": 0,
          "offset": 0
        },
        "end": {
          "line": 1,
          "column": 11,
          "offset": 11
        }
      },
      "expression": {
        "kind": "bin",
        "loc": {
          "source": null,
          "start": {
            "line": 1,
            "column": 0,
            "offset": 0
          },
          "end": {
            "line": 1,
            "column": 11,
            "offset": 11
          }
        },
        "type": "+",
        "left": {
          "kind": "bin",
          "loc": {
            "source": null,
            "start": {
              "line": 1,
              "column": 0,
              "offset": 0
            },
            "end": {
              "line": 1,
              "column": 6,
              "offset": 6
            }
          },
          "type": "*",
          "left": {
            "kind": "number",
            "loc": {
              "source": null,
              "start": {
                "line": 1,
                "column": 0,
                "offset": 0
              },
              "end": {
                "line": 1,
                "column": 1,
                "offset": 1
              }
            },
            "value": "5"
          },
          "right": {
            "kind": "unary",
            "loc": {
              "source": null,
              "start": {
                "line": 1,
                "column": 4,
                "offset": 4
              },
              "end": {
                "line": 1,
                "column": 6,
                "offset": 6
              }
            },
            "type": "-",
            "what": {
              "kind": "number",
              "loc": {
                "source": null,
                "start": {
                  "line": 1,
                  "column": 5,
                  "offset": 5
                },
                "end": {
                  "line": 1,
                  "column": 6,
                  "offset": 6
                }
              },
              "value": "1"
            }
          }
        },
        "right": {
          "kind": "number",
          "loc": {
            "source": null,
            "start": {
              "line": 1,
              "column": 9,
              "offset": 9
            },
            "end": {
              "line": 1,
              "column": 11,
              "offset": 11
            }
          },
          "value": "2"
        }
      }
    }
  ],
  "errors": [],
  "comments": []
}

AST Representation at version 3.5.1:

{
  "kind": "program",
  "loc": {
    "source": null,
    "start": {
      "line": 1,
      "column": 0,
      "offset": 0
    },
    "end": {
      "line": 1,
      "column": 11,
      "offset": 11
    }
  },
  "children": [
    {
      "kind": "expressionstatement",
      "loc": {
        "source": null,
        "start": {
          "line": 1,
          "column": 0,
          "offset": 0
        },
        "end": {
          "line": 1,
          "column": 11,
          "offset": 11
        }
      },
      "expression": {
        "kind": "bin",
        "loc": {
          "source": null,
          "start": {
            "line": 1,
            "column": 0,
            "offset": 0
          },
          "end": {
            "line": 1,
            "column": 11,
            "offset": 11
          }
        },
        "type": "*",
        "left": {
          "kind": "number",
          "loc": {
            "source": null,
            "start": {
              "line": 1,
              "column": 0,
              "offset": 0
            },
            "end": {
              "line": 1,
              "column": 1,
              "offset": 1
            }
          },
          "value": "5"
        },
        "right": {
          "kind": "unary",
          "loc": {
            "source": null,
            "start": {
              "line": 1,
              "column": 4,
              "offset": 4
            },
            "end": {
              "line": 1,
              "column": 10,
              "offset": 10
            }
          },
          "type": "-",
          "what": {
            "kind": "bin",
            "loc": {
              "source": null,
              "start": {
                "line": 1,
                "column": 5,
                "offset": 5
              },
              "end": {
                "line": 1,
                "column": 10,
                "offset": 10
              }
            },
            "type": "+",
            "left": {
              "kind": "number",
              "loc": {
                "source": null,
                "start": {
                  "line": 1,
                  "column": 5,
                  "offset": 5
                },
                "end": {
                  "line": 1,
                  "column": 6,
                  "offset": 6
                }
              },
              "value": "1"
            },
            "right": {
              "kind": "number",
              "loc": {
                "source": null,
                "start": {
                  "line": 1,
                  "column": 9,
                  "offset": 9
                },
                "end": {
                  "line": 1,
                  "column": 10,
                  "offset": 10
                }
              },
              "value": "2"
            }
          }
        }
      }
    }
  ],
  "errors": [],
  "comments": []
}

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

Successfully merging this pull request may close these issues.

1 participant