Skip to content

Update parser create method for PHP-Parser v5 #7

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

Merged
merged 2 commits into from
Apr 11, 2024

Conversation

matthewpaulking
Copy link
Contributor

@matthewpaulking matthewpaulking commented Mar 6, 2024

I had a weird bug where tinkeray just stopped working all together:

CleanShot 2024-03-05 at 21 49 29@2x

After digging around a bit, I found that the ParserFactory::create() method was removed in nikic/PHP-Parser v5 (see: https://github.com/nikic/PHP-Parser/blob/master/UPGRADE-5.0.md#changes-to-the-parser-factory). They propose using one of three methods, so I picked out createForHostVersion() as probably the best replacement. This method also exists in v4 of nikic/PHP-Parser, so this should still work in older versions.

I think this is probably related to laravel/tinker#170

Manually changing this line in my installed version of tinkeray seems to have fixed the problem, but I'm not sure how to do more thorough testing.

create method was removed in nikic/PHP-Parser v5
@neilgilmour
Copy link

I was getting the same issue, and @matthewpaulking's change worked for me.

@uportnykh
Copy link

@matthewpaulking Thanks for sharing the fix! I ended up using a similar code, even though it doesn't seem to work in v4 of nikic/PHP-Parser for me, so I use this:

if (! method_exists('PhpParser\ParserFactory', 'create')) {
    $parser = (new PhpParser\ParserFactory)->createForHostVersion();
} else {
    $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7);
}

@jesseleite
Copy link
Owner

Interesting yeah. I guess we can't guarantee which version they'll be on, so I threw a ternary in there to cover both. Eventually we can just use the new method 👍

Thanks fellas!

@jesseleite jesseleite merged commit b3f996e into jesseleite:master Apr 11, 2024
@matthewpaulking
Copy link
Contributor Author

Good catch @uportnykh, and thanks very much @jesseleite!

@matthewpaulking matthewpaulking deleted the patch-1 branch April 11, 2024 02:43
@jesseleite
Copy link
Owner

Team effort!

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.

4 participants