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

Fix Typed Class Constants parsing #1140

Closed
wants to merge 9 commits into from

Conversation

genintho
Copy link
Collaborator

Fix #1133

  • make sure all variation of class constant are still being supported
  • Throw a clear error when the parser detect typed class constants by the PHP version configured does not match

"line": 31,
"offset": 544,
},
},
"name": Identifier {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks as though the source code location is lost during parsing as well

Comment on lines 311 to 322
"attrGroups": [
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"name": "B",
},
],
"kind": "attrgroup",
},
],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attribute groups are lost too

Comment on lines 328 to 337
AttrGroup {
"attrs": [
Attribute {
"args": [],
"kind": "attribute",
"name": "B",
},
],
"kind": "attrgroup",
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make sense either, nullable should be a bool value? looks like attrGroups and nullable are switched around somewhere

Copy link
Collaborator

@cseufert cseufert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the refactor has significantly changed the AST output around attrgroups, as the attrGroups should not be affected, also I would expect nullable to be a boolean type.

@genintho
Copy link
Collaborator Author

@cseufert Yeah there are bugs in the logic.
Can you look at this different PR I made that fix some other bugs I found while working on this #1141

@genintho
Copy link
Collaborator Author

genintho commented Nov 30, 2024

I figured out the core issue: the AST ClassConstant object constants which is an array of AST "Constant" object.

ClassConstant {
          "attrGroups": undefined,
          "constants": [
            Constant {
              "kind": "constant",
              "name": Identifier {
                "kind": "identifier",
                "name": "CON_2",
              },
              "value": String {
                "isDoubleQuote": true,
                "kind": "string",
                "raw": ""Hello world!"",
                "unicode": false,
                "value": "Hello world!",
              },
            },
          ],
          "final": false,
          "kind": "classconstant",
          "nullable": [],
          "type": null,
          "visibility": "",
        },

The code is reading a list of constant, to support this syntax.

class Foo {
    const PRI1 = 'baz', PRI2 = 'baz2';
}

But the implementations made the assumption that all constant have the same type/nullable: type and nullable was added to "ClassConstant", not "Constant".

That's incorect, because this code is valid:

class Foo {
	private const PRI1 = 'baz', string PRI2 = 'baz2', int PRI3 = 5;
}

The last commit I pushed, 94d4069 keep the type on the "ClassConstant" object and create only 1 constant object per ClassConstant. That works, but it is a less accurate representation of the code being parsed.

Now I am going to fix that by moving the type to the AST "Constant" object.

@@ -53,6 +57,8 @@ Program {
"kind": "identifier",
"name": "CONSTANT",
},
"nullable": null,
"type": undefined,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do not want those to be part of the AST, we need to completly decouple the ClassConstant object from the Constant object.

@genintho
Copy link
Collaborator Author

@cseufert code is ready for another review

@genintho genintho requested a review from cseufert November 30, 2024 21:37
@genintho
Copy link
Collaborator Author

genintho commented Dec 1, 2024

Actually, they are still bug. I will reopen when it is fully ready.

@genintho genintho closed this Dec 1, 2024
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.

Fails to parse classes with typed constants
2 participants