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 issue of parsing PHP 8 sources on PHP 7.x #1184

Merged
merged 1 commit into from
Nov 8, 2021

Conversation

ondrejmirtes
Copy link
Contributor

Copy link
Member

@samsonasik samsonasik left a comment

Choose a reason for hiding this comment

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

@ondrejmirtes I confirm that it works 🎉 Thank you

Screen Shot 2021-11-08 at 15 21 53

@TomasVotruba
Copy link
Member

Awesome, thank you 👏

@TomasVotruba TomasVotruba merged commit b2d1749 into rectorphp:main Nov 8, 2021
@TomasVotruba
Copy link
Member

TomasVotruba commented Dec 13, 2021

Related bug: rectorphp/rector#6799

We'll need 2 parsers:

  • one for user's code that respects their PHP version.
  • one for the stubs parsing

@TomasVotruba
Copy link
Member

@ondrejmirtes Any ideas how to make those 2 parser like PHPStan has? I'm trying to play with neon syntax, but I keep failing as I never used it without autowiring.

@ondrejmirtes
Copy link
Contributor Author

@TomasVotruba You need a PathRoutingParser like PHPStan has (/~https://github.com/phpstan/phpstan-src/blob/master/src/Parser/PathRoutingParser.php), but probably not exactly the same one, but custom for your needs.

This is how it's configured: /~https://github.com/phpstan/phpstan-src/blob/ce0b04308d4761f6efd8ce3fec90be59e1197ced/conf/config.neon#L1744-L1750

@ondrejmirtes ondrejmirtes deleted the php8 branch December 13, 2021 21:22
@TomasVotruba
Copy link
Member

Not sure how to configure it and how that would help.

So far I think the problem is the default parser is used for stubs that are in PHP 8.
But in analysis we need PHP based default rich parser.

@ondrejmirtes
Copy link
Contributor Author

That's exactly how PathRoutingParser can help you.

@TomasVotruba
Copy link
Member

TomasVotruba commented Dec 13, 2021

Any ideas how exacly it should be configured? What does "path routing" mean?

This parses PHP 7.4 code, but fails to parse stubs:

#parameters:
#    phpVersion: 80100

services:
    defaultAnalysisParser:
        factory: @cachedRectorParser
        arguments!: []

    cachedRectorParser:
        class: PHPStan\Parser\CachedParser
        arguments:
            originalParser: @rectorParser
            cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
        autowired: no

    rectorParser:
        class: PHPStan\Parser\RichParser
        arguments:
            parser: @currentPhpVersionPhpParser
            lexer: @currentPhpVersionLexer
        autowired: no

    pathRoutingParser:
        class: PHPStan\Parser\PathRoutingParser
        arguments:
            currentPhpVersionRichParser: @php8PhpParser
            currentPhpVersionSimpleParser: @php8PhpParser
            php8Parser: @php8Parser
        autowired: false

@ondrejmirtes
Copy link
Contributor Author

This calls for a phone call again :) I have time on Friday.

@TomasVotruba
Copy link
Member

TomasVotruba commented Dec 13, 2021

It fails on FileTypeMapper that is using defaultAnalysisParser (PHP 7.4) /~https://github.com/phpstan/phpstan-src/blob/ce0b04308d4761f6efd8ce3fec90be59e1197ced/conf/config.neon#L864-L867

But it seems Nette DI is unable to override anonymous services :/

	-
		class: PHPStan\Type\FileTypeMapper
		arguments:
-			phpParser: @defaultAnalysisParser
+			phpParser: @pathRoutingParser

PHP Fatal error:  Uncaught _PHPStan_c862bb974\Nette\DI\ServiceCreationException: 
Multiple services of type PHPStan\Type\FileTypeMapper found: 0114, 0246 in phar:///var/www/rector-syntax-bug/vendor/rector/rector/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/di/src/DI/Autowiring.php:53

@ondrejmirtes
Copy link
Contributor Author

The defaultAnalysisParser must be pathRoutingParser, that's the whole point. And you should use it to route parser requests either to current version PHP parser (analysed code, dependencies), or to PHP 8 parser (stubs).

@TomasVotruba
Copy link
Member

TomasVotruba commented Dec 13, 2021

I'd prefer to solve it now :D it seems extreemely hard for such a simple task.
I'll give it couple more hours. If I fail, we can call.

@TomasVotruba
Copy link
Member

Something like this?

services:
    pathRoutingParser:
        class: PHPStan\Parser\PathRoutingParser
        arguments:
            currentPhpVersionRichParser: @rectorParser
            currentPhpVersionSimpleParser: @rectorParser
            php8Parser: @php8Parser
        autowired: false

    rectorParser:
        class: PHPStan\Parser\RichParser
        arguments:
            parser: @currentPhpVersionPhpParser
            lexer: @currentPhpVersionLexer
        autowired: no

@ondrejmirtes
Copy link
Contributor Author

I'd prefer if you wrote your own PathRoutingParser, the one from PHPStan is too complex and unsuitable for your needs.

You also need to include:

    defaultAnalysisParser:
        factory: @pathRoutingParser
        arguments!: []

@TomasVotruba
Copy link
Member

I'd prefer if you wrote your own PathRoutingParser, the one from PHPStan is too complex and unsuitable for your needs.

If I understand what it does and why, I'll try 👍

It looks that this - #1184 (comment) - works for both fixture and CI in this repository.

You also need to include:

I'll try

@TomasVotruba
Copy link
Member

TomasVotruba commented Dec 13, 2021

The work is done in #1480

@TomasVotruba
Copy link
Member

Looks good, the tests pass here, the repository with bug is fixed too: /~https://github.com/cjunge-work/rector-syntax-bug/pull/3

Thanks @ondrejmirtes 👍 👏

I can sleep in peace today :)

@cjunge-work
Copy link

Thank you @TomasVotruba!!! 🍻

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.

RichParser error on dev-main on PHP 7.4
4 participants