-
-
Notifications
You must be signed in to change notification settings - Fork 692
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
RichParser error on dev-main on PHP 7.4 #6779
Comments
I'm not sure why it can't parse PHP 7.4. What exact parsed code is causing it? |
The example parsed code is here: https://github.com/codeigniter4/CodeIgniter4/blob/develop/tests/system/View/TableTest.php |
Hi, I think the issue is that you're running on PHP 7.4 but you're trying to parse code for PHP 8. While the code of the project might not use PHP 8 syntax, something in Rector uses it, most likely the thing that parses jetbrains/phpstorm-stubs. PHPStan uses a different parser for this scenario. phpstorm-stubs are always parsed with a parser that contains the Emulative lexer. I think that if you actually tried to test rector-src on PHP 7.4, it would fail in a similar manner. You're "supporting" PHP 7.x, but you're not running most tests on these supported versions. That should be fixed. |
Just note for step to reproduce: git clone [email protected]:samsonasik/CodeIgniter4.git
cd CodeIgniter4
git checkout upgrade-to-phpstan1
composer update Use PHP 7.4 to run rector: /opt/homebrew/Cellar/[email protected]/7.4.24_1/bin/php vendor/bin/rector process --clear-cache --dry-run |
Another note: the error seems still happen even all services and set disabled: /opt/homebrew/Cellar/[email protected]/7.4.24_1/bin/php vendor/bin/rector process --clear-cache --dry-run |more
673/673 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
[ERROR] Could not process "app/Config/Boot/development.php" file, due to:
"Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:206, Syntax error, unexpected T_FUNCTION, expecting
T_VARIABLE:467". On line: 38
[ERROR] Could not process "app/Config/Boot/production.php" file, due to:
"Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:206, Syntax error, unexpected T_FUNCTION, expecting
T_VARIABLE:467". On line: 38
[ERROR] Could not process "app/Config/Boot/testing.php" file, due to:
"Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:206, Syntax error, unexpected T_FUNCTION, expecting
T_VARIABLE:467". On line: 38
[ERROR] Could not process "app/Config/Events.php" file, due to:
"Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:318, Syntax error, unexpected T_FUNCTION, expecting
T_VARIABLE:385, Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:427, Syntax error, unexpected '}',
expecting T_VARIABLE:610, Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:682, Syntax error,
unexpected T_FUNCTION, expecting T_VARIABLE:744, Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:817,
Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:836, Syntax error, unexpected T_FUNCTION, expecting
T_VARIABLE:884, Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:1181, Syntax error, unexpected
T_FUNCTION, expecting T_VARIABLE:1217, Syntax error, unexpected '}', expecting T_VARIABLE:1236, Syntax error,
unexpected T_FUNCTION, expecting T_VARIABLE:1414, Syntax error, unexpected T_FUNCTION, expecting
T_VARIABLE:1481, Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:1517, Syntax error, unexpected
T_FUNCTION, expecting T_VARIABLE:1585, Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:1657, Syntax |
thank you @ondrejmirtes for the hint, @TomasVotruba it probably related with The Looking at the Lexer directory, there is |
@TomasVotruba I created repository example with minimal code that reproduce it https://github.com/samsonasik/rector-issue-php74 It has Github action to show the error at https://github.com/samsonasik/rector-issue-php74/runs/4097757321?check_suite_focus=true#step:5:6 show error: vendor/bin/rector process src/Test.php --dry-run
shell: /usr/bin/bash -e {0}
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
Error: ] Could not process "src/Test.php" file, due to:
"Syntax error, unexpected T_FUNCTION, expecting T_VARIABLE:1043". On
line: 38 Step to reproduce locally: git clone [email protected]:samsonasik/rector-issue-php74.git
cd rector-issue-php74
composer update
# run with php 7.4
/opt/homebrew/Cellar/[email protected]/7.4.24_1/bin/php vendor/bin/rector process src/Test.php |
@TomasVotruba I tried rector/rector dev-main in my other repository as well and it show error as well, on php 7.3 For minimal reproducible repo, https://github.com/samsonasik/rector-issue-php74 already cover it as only contains single |
@TomasVotruba I added https://github.com/rectorphp/rector/runs/4106986477?check_suite_focus=true#step:5:7 for PHP 7.x |
It seems the provided lexer is using different PHP that current active one: |
See The solution might be to override this lexer service to work with our PHP version somehow |
@TomasVotruba I tried to override that, but it seems the // services.php
$services->set(\Rector\Php\PhpVersion::class)
->factory([service(\Rector\Core\PHPStan\RectorPhpVersionFactory::class), 'create']);
$services->alias(\PHPStan\Php\PhpVersion::class, \Rector\Php\PhpVersion::class); <?php
// src/PHPStan/RectorPhpVersionFactory.php
declare (strict_types=1);
namespace Rector\Core\PHPStan;
final class RectorPhpVersionFactory
{
public function __construct(private \Rector\Core\Php\PhpVersionProvider $phpVersionProvider)
{
}
public function create(): \PHPStan\Php\PhpVersion
{
$factory = new \PHPStan\Php\PhpVersionFactory(null, $this->phpVersionProvider->resolve());
return $factory->create();
}
} |
that's still not works even moved to final class PHPStanServicesFactory
{
// ...
public function createPHPStanPhpVersion(): \PHPStan\Php\PhpVersion
{
$phpVersionProvider = $this->container->gerService(\Rector\Core\Php\PhpVersionProvider::class);
$factory = new \PHPStan\Php\PhpVersionFactory(null, $phpVersionProvider->resolve());
return $factory->create();
}
} with services: $services->set(\PHPStan\Php\PhpVersion::class)
->factory([service(PHPStanServicesFactory::class), 'createPHPStanPhpVersion']); as it seems the |
This must happen in config that is used by PHPStan, e.g. here: |
@TomasVotruba I tried register the service with: services:
defaultAnalysisParser:
factory: @currentPhpVersionRichParser
arguments!: []
-
class: PHPStan\Php\PhpVersion
factory: @Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory::createPHPStanPhpVersion and got error: Next _PHPStan_bb49b3bd7\Nette\DI\ServiceCreationException:
Service of type PHPStan\Parser\LexerFactory: Multiple services of type PHPStan\Php\PhpVersion found : 07, 0242 (needed by $phpVersion in __construct()) in phar:///Users/samsonasik/www/rector-src/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/di/src/DI/Resolver.php:442 any way to set alias for it? |
@samsonasik I think the previous must be overriden. Not sure if NEON allows it, in case of anonymous service: cc @ondrejmirtes Any ideas how to give PHPStan different PHP version? |
@TomasVotruba Hi, I think you're taking the wrong approach here and doing the wrong thing. I'd much rather have a phone call with you again and solved it together :) |
@ondrejmirtes Hey, that would be the best :) |
@TomasVotruba I'm busy this weekend, what about Monday morning? |
Sounds good. 10 AM? |
Alright 😊 |
Just submitted a fix for this: rectorphp/rector-src#1184 The problem was that Rector uses (based on my advice which I haven't realized might be wrong for this use-case) "current version PHP parser". And that doesn't work when phpstorm-stubs need to be parsed. PHPStan doesn't have this problem thanks to "PathRoutingParser": https://github.com/phpstan/phpstan-src/blob/master/src/Parser/PathRoutingParser.php (PHPStan wants to get parse errors when user writes PHP 8 code on PHP 7.x but still needs to parse phpstorm-stubs with PHP 8 parser). I think for Rector it's best to just always use PHP 8 parser. |
Thank you for the explanation and solution Ondra 👍 Rector tries to parse any code use has and then handle the refactoring that user wants. Parse error and current version are not relevant in this process. Maybe the naming is confusing to me, but out of curiosity, is PHP 8 parser able to parse PHP 5-only code? |
Yeah, I think so, there's no syntax in PHP 5 that was removed later. |
Parsing stubs and user's code with same parser version causes bug in #6799 |
Bug Report
@TomasVotruba I tried rector dev-main on codeigniter4/CodeIgniter4#5269 to test the compatibility with PHPStan 1.0, and this only show this error on PHP 7.4, ref https://github.com/codeigniter4/CodeIgniter4/runs/4066534747?check_suite_focus=true#step:10:5
while it is working ok on PHP 8.0. It seems there is invalid syntax in PHP 7.4 for PHPStan.
/cc @ondrejmirtes do you think it is PHPStan issue on downgrading syntax ?
The trace exception message are like this:
The text was updated successfully, but these errors were encountered: