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

Internal error: Cannot assign an empty string to a string offset in ConstantStringType #6231

Closed
mglaman opened this issue Dec 22, 2021 · 8 comments

Comments

@mglaman
Copy link
Contributor

mglaman commented Dec 22, 2021

Bug report

Quick note: It's hard to quickly reproduce the same code, I realized after opening that this sample crashes in https://3v4l.org/. I'll try to work on an example that runs there BUT crashes for PHPStan. In the mean time, there is the phpstan-drupal test which shows the failure.

Drupal has a wonderful class that implements \ArrayAccess and __get and __set which delegate to classes inside of its values. These are our FieldItemList and FieldItem classes.

We experienced PHPStan crashing when trying to analyze Drupal core on the following example of code for this magical madness. I have annotated to help make any sense of this (yes, I know.)

$entity = EntityTest::create();
// Sets list[0] item to the array.
$entity->field_test = ['uri' => 'internal:/node/add'];
// Updates // Sets list[0] item to the array.
$entity->field_test = [
  'uri' => 'internal:/node/add',
  'options' => ['query' => NULL],
];
// Resets list[0] and delegates the string value to the FieldItem (which sets string to its `uri` property)
$entity->field_test = 'internal:/node/add';
// This _should_ reset list[0] to NULL but it causes PHPStan to crash
$entity->field_test[0] = NULL;

I tried debugging this in mglaman/phpstan-drupal#279. I have no idea how I could try to fix this, even with property reflections.

We have a property reflection to ensure FieldItemList is returned for dynamic entity properties, but that doesn't seem to help.

Here's a screenshot from Xdebug. The property is considered ConstantStringType and breaks when processing the offset value.

Screen Shot 2021-12-22 at 3 28 32 PM

Here's the full stack trace when running the test in linked phpstan-drupal PR locally

Error : Cannot assign an empty string to a string offset
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Type/Constant/ConstantStringType.php:236
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:2652
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:1371
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:455
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:253
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:418
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:253
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:502
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:253
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:468
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:221
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php:164
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/Analyser.php:52
 phar:///Users/mglaman/Projects/php/phpstan-drupal/vendor/phpstan/phpstan/phpstan.phar/src/Testing/RuleTestCase.php:56

Code snippet that reproduces the problem

https://phpstan.org/r/95032316-ae1e-4d4c-829c-810ae66fdcbe

Expected output

PHPStan may error but not crash.

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

@T2L
Copy link

T2L commented Jan 19, 2022

The minimum code that reproduces the issue: https://phpstan.org/r/c40ad6c1-f8d7-4161-86b1-b07e86cb0aa5

@mglaman
Copy link
Contributor Author

mglaman commented Jan 19, 2022

@T2L the problem with that snippet is that it also crashes in PHP, as expected: https://3v4l.org/jne6e

Drupal is adding magic that makes it not crash and requires a more complex sample code

@T2L
Copy link

T2L commented Jan 20, 2022

@mglaman LOL, clearly missed that point, but isn't PHPStan not supposed to actually execute the code, but only parse it ?

@mglaman
Copy link
Contributor Author

mglaman commented Jan 20, 2022

That is true. I do think it's quite brilliant that it's the same either occuring when parsing though

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@54f67c5

@T2L
Copy link

T2L commented Jan 20, 2022

@ondrejmirtes wow, thanks!

But I think for PHP7.* fix does not work: warning vs error, see https://3v4l.org/jne6e

@ondrejmirtes
Copy link
Member

Yeah, the test suite showed us that: https://github.com/phpstan/phpstan-src/runs/4883009041?check_suite_focus=true

I'm gonna look into it.

@github-actions
Copy link

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 Feb 21, 2022
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

3 participants