-
-
Notifications
You must be signed in to change notification settings - Fork 938
Description
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.
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.
