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

PHP8: Union types support #656

Closed
Wulfheart opened this issue Feb 5, 2021 · 3 comments
Closed

PHP8: Union types support #656

Wulfheart opened this issue Feb 5, 2021 · 3 comments

Comments

@Wulfheart
Copy link

#486

@czosel
Copy link
Collaborator

czosel commented Feb 7, 2021

We're parsing function (Foo $foo): Bar {} as

{
      "kind": "function",
      "name": {
        "kind": "identifier",
        "name": false
      },
      "arguments": [
        {
          "kind": "parameter",
          "name": {
            "kind": "identifier",
            "name": "foo"
          },
          "value": null,
          "type": {
            "kind": "name",
            "resolution": "uqn",
            "name": "Foo"
          },
          "byref": false,
          "variadic": false,
          "nullable": false
        }
      ],
      "byref": false,
      "type": {
        "kind": "name",
        "resolution": "uqn",
        "name": "Bar"
      },
      "nullable": false,
      "body": {
        "kind": "block",
        "children": []
      }

PHP-Parser parses function (Foo|Bar $foo): Baz|Buzz {}; as

expr: Expr_Closure(
            attrGroups: array(
            )
            static: false
            byRef: false
            params: array(
                0: Param(
                    attrGroups: array(
                    )
                    flags: 0
                    type: UnionType(
                        types: array(
                            0: Name(
                                parts: array(
                                    0: Foo
                                )
                            )
                            1: Name(
                                parts: array(
                                    0: Bar
                                )
                            )
                        )
                    )
                    byRef: false
                    variadic: false
                    var: Expr_Variable(
                        name: foo
                    )
                    default: null
                )
            )
            uses: array(
            )
            returnType: UnionType(
                types: array(
                    0: Name(
                        parts: array(
                            0: Baz
                        )
                    )
                    1: Name(
                        parts: array(
                            0: Buzz
                        )
                    )
                )
            )
            stmts: array(
            )
        )

We could introduce a uniontype node, e.g.

      "type": {
        "kind": "uniontype",
        "types": [{
          "kind": "name",
          "resolution": "uqn",
          "name": "Foo"
        }, {
          "kind": "name",
          "resolution": "uqn",
          "name": "Foo"
        }]
      },

See also https://github.com/php/php-src/blob/493524454d66adde84e00d249d607ecd540de99f/Zend/zend_language_parser.y#L650

cc @ichiriac

@cseufert
Copy link
Collaborator

I have had a go at implementing this a changing type to an array, rather than a single value. Sorry I missed this before I started. I can update it to return a union type when there are multiple types.

@alexander-akait
Copy link
Collaborator

Fixed in php8 branch

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

4 participants