Static Analysis Gets Better With Entity Type Storage In PHPStan-Drupal 1.1.0.

Software Code
Unsplash

PHPStan-drupal 1.1.0 got released yesterday. It’s full of static analysis goodies for entity storage methods. PHP Stan is a PHP Static Analysis Tool that helps discover bugs in the code without running it.

The new release is a minor version due to a breaking change in the configuration options for phpstan-drupal.  Brambaud helped untangle Drupal’s magical bits for static analysis. Eirik Stanghelle Morland helped Matt Glaman fix some of the Drupal issues when fetching field value properties.

Here is a summary of the significant improvements in Matt Glaman’s own words:

  • On analysis level 2, PHPStan would consider $entity->get('my_field')->value invalid but $entity->get('my_field')->first()->value valid. The former is allowed due field item list classes defaulting to the first value when using the magically __get call.
  • When an entity query is executed, the return type may be an int or array of entities, depending on if the count method was called before execution, creating a count query. PHPStan read the return type from the methods docblock, and that was it. Now, it will properly return int, array<int, string> for content entities, and array<string, string> for configuration entities.
  • Ensure that entity storage methods return the appropriate entity class if it has been defined in the configuration. Before this release, phpstan-drupal only allowed configuring the entity storage class to improve static analysis. With 1.1.0, the entity class can also be defined. If a storage class is not provided, but the entity class is, it will infer the appropriate default storage based on if it is a content or configuration entity

All of these features are provided by implementing dynamic return type extensions. A dynamic return type extension is used to help PHPStan understand what will be returned to a method based on arguments passed to it.

To know more read: Matt Glaman’s blog

PHPStan

PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.

Read more about PHPStan at Medium.com

Source

https://github.com/mglaman/phpstan-drupal/releases/tag/1.1.0
https://mglaman.dev/blog/better-static-analysis-entity-type-storages-phpstan-drupal-110
https://mglaman.dev/tags/phpstan-drupal

Note: The vision of this web portal is to help promote news and stories around the Drupal community and promote and celebrate the people and organizations in the community. We strive to create and distribute our content based on these content policy. If you see any omission/variation on this please let us know in the comments below and we will try to address the issue as best we can.

Advertisement Here

Call for Support