Skip to content

Conversation

mamazu
Copy link

@mamazu mamazu commented Mar 17, 2023

Why?

The old version of the parser was looking for the class keyword followed by a {. The problem is the parser doesn't take into account that the class keyword can also be used in a different context like in self::class which then will crash because if you have something like expression like

$a = (self::class);
{
}

this it will take the token that is two after the class keyword which in this case would be the semicolon. Which is not correct.

What?

I have added a check that if the token preceding the class keyword is the '::' operator, then it will skip it. (It's not a good fix though).

Further things to consider

  • Maybe return after finding the first instance of the class keyword (however this would change the behavior as we are currently taking the last class in the file
  • What about anonymous classes inside the migration? This will also break the code

@dantleech
Copy link
Member

This code was originally baed on a StackOverflow answer and I've been maintaining it in two other palces:

Probably all have diverged in some ways, should really create a package.

@mamazu
Copy link
Author

mamazu commented Mar 17, 2023

Yes, they did diverge. But the biggest difference between the version in this repo is that the other examples is that your code breaks after finding the first instance. This would fix the problem here as well (assuming there is no code before the class declaration).

@mamazu mamazu closed this Dec 27, 2023
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.

2 participants