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

"regular expression is too large" when handling large arrays #10614

Closed
kingkero opened this issue Feb 21, 2024 · 9 comments
Closed

"regular expression is too large" when handling large arrays #10614

kingkero opened this issue Feb 21, 2024 · 9 comments

Comments

@kingkero
Copy link
Sponsor

kingkero commented Feb 21, 2024

Bug report

Currently introducing PHPStan to a larger Laminas project (already using slam/phpstan-laminas-framework), when generating the baseline one entry is:

    -
      message: "#^Cannot access offset 'grpc' on 'https\\://…'\\|'https\\://…'\\|\\(array\\<int\\|\\(literal\\-string&non\\-falsy\\-string\\), 0\\|0\\.25\\|0\\.66\\|1\\|3\\|5\\|10\\|14\\|15\\|20\\|29\\|50\\|60\\|73\\|74\\|75\\|76\\|79\\|80\\|99\\|100\\|123\\|124\\|260\\|300\\|500\\|600\\|660\\|1000\\|2000\\|3600\\|4000\\|9002\\|10000\\|11211\\|24575\\|''\\|'/'\\|'/api/v1/'\\|'/api/v2/'\\|'/api/v3/'\\|'/backup/…'\\|'/collect'\\|'/overload_fp\\.html'\\|'/var/www/bigassets…'\\|'/var/www/…'\\|'/var/www/…'\\|'/var/www/|false\\.|HERE THE REGEX CONTINUES FOR VERY LONG$#"
      count: 3 
      path: ../../../module/src/MyService.php

I already shortened the line. The original line in the baseline is 60733 characters long.

Running phpstan analyse with that baseline results in

In Strings.php line 447:
                                                                                                                                                                                                                                                                           
  [_PHPStan_156ee64ba\Nette\Utils\RegexpException]                                                                                                                                                                                                                         
  Compilation failed: regular expression is too large at offset 53891 in pattern: #^Cannot access offset 'grpc' on 'https\://…'\|'https\://…'\|\(array<int\|\(literal\-string...HERE THE LONG REGEX IS LISTED FROM BASELINE

Exception trace:
  at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/utils/src/Utils/Strings.php:447
 _PHPStan_156ee64ba\Nette\Utils\Strings::_PHPStan_156ee64ba\Nette\Utils\{closure}() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/utils/src/Utils/Callback.php:63
 _PHPStan_156ee64ba\Nette\Utils\Callback::_PHPStan_156ee64ba\Nette\Utils\{closure}() at n/a:n/a
 preg_match() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/utils/src/Utils/Callback.php:70
 _PHPStan_156ee64ba\Nette\Utils\Callback::invokeSafe() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/utils/src/Utils/Strings.php:448
 _PHPStan_156ee64ba\Nette\Utils\Strings::pcre() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/utils/src/Utils/Strings.php:410
 _PHPStan_156ee64ba\Nette\Utils\Strings::match() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/IgnoredError.php:47
 PHPStan\Analyser\IgnoredError::shouldIgnore() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/IgnoredErrorHelperResult.php:83
 PHPStan\Analyser\IgnoredErrorHelperResult->PHPStan\Analyser\{closure}() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/IgnoredErrorHelperResult.php:139
 PHPStan\Analyser\IgnoredErrorHelperResult->PHPStan\Analyser\{closure}() at n/a:n/a
 array_filter() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/IgnoredErrorHelperResult.php:168
 PHPStan\Analyser\IgnoredErrorHelperResult->process() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseApplication.php:120
 PHPStan\Command\AnalyseApplication->analyse() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseCommand.php:200
 PHPStan\Command\AnalyseCommand->execute() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php:259
 _PHPStan_156ee64ba\Symfony\Component\Console\Command\Command->run() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php:870
 _PHPStan_156ee64ba\Symfony\Component\Console\Application->doRunCommand() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php:261
 _PHPStan_156ee64ba\Symfony\Component\Console\Application->doRun() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php:157
 _PHPStan_156ee64ba\Symfony\Component\Console\Application->run() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan:124
 _PHPStan_156ee64ba\{closure}() at phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan:125
 require() at /var/www/vendor/phpstan/phpstan/phpstan:8
 include() at /var/www/vendor/bin/phpstan:119

The line in code that triggers this error is:

$config = $this->serviceManager->get('config');

$hostname = $config['...']['grpc']['hostname'];
$grpcOptions = $config['...']['grpc']['options'];
$channel = $config['...']['grpc']['channel'];

This issue only happens when using the baseline. If I have an empty baseline, generate it new - it runs perfectly. But after generation trying to re-run I get the above posted issue.

I tried reproducing the issue in the playground, but the playground crashes. The code used can be found here (could not post it in the issue).

Code snippet that reproduces the problem

No response

Expected output

PHPStan doesn't crash parsing baselines it previously generated.

Did PHPStan help you today? Did it make you happy in any way?

No response

Copy link

mergeable bot commented Feb 21, 2024

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Oh my god, what in the hell is that code!? Doesn't look like a maintainable piece of code. My smartphone almost crashed trying to load the page.

I'm inclined to say that this kind of bending and hacking is not supported 😊

@kingkero
Copy link
Sponsor Author

Honestly I'm not sure if that example fits. I'm not sure who generates that long regex, probably some magic from the Laminas connector that tries to set all config keys it finds or something. It is not something part of our codebase.

The issue is that PHPStan generates a baseline entry it can't parse.

@ondrejmirtes
Copy link
Member

I tried to make the problem less worse: phpstan/phpstan-src@039a3dc

Please try composer require --dev phpstan/phpstan:1.10.x-dev and run PHPStan, generate the baseline and use the baseline. Thanks.

@ondrejmirtes
Copy link
Member

I'll reopen if you find out it still doesn't work.

@kingkero
Copy link
Sponsor Author

kingkero commented Mar 4, 2024

@ondrejmirtes Sorry, but cannot confirm the fix. Not sure if its a version mis-match (I am behind a packagist proxy), but even cloning phpstan-src and compiling the phar, I still saw the issue.

Could create a reproduction repo for this: https://github.com/kingkero/phpstan-error-too-long/, info in README.

Basically this (https://github.com/kingkero/phpstan-error-too-long/blob/main/module/Application/config/module.config.php#L8-L11) simulates the long config that we have in the prod setup.

@ondrejmirtes
Copy link
Member

@kingkero Hi, I verified that the fix works. But you misunderstood the nature of the fix. You need to delete your current baseline that's commited here https://github.com/kingkero/phpstan-error-too-long/blob/main/phpstan.baseline.neon and generate it again with the newer PHPStan dev version. It's going to contain a shorter error message and the regex machine is not going to have a problem with that.

You can install that version with composer require --dev phpstan/phpstan:1.10.x-dev.

@kingkero
Copy link
Sponsor Author

kingkero commented Mar 5, 2024

You're right, I'm sorry for the extra noise. Can confirm it works as described.

Copy link

github-actions bot commented Apr 6, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants