PHPStan 2.0: Stricter type checks and new analysis level
The PHP code analysis tool takes a closer look at types, introduces a new list type and overhauls memory management.
(Image: Sashkin/Shutterstock.com)
After almost three years of further development, version 2.0 of the open source code analysis tool PHPStan is now available. The update brings stricter type checks, revised memory management, a new list type, more precise validation of the @var-PHPDoc tag and a reduced dependency on caching.
Level 10 for a strict type check
One of the most significant changes in the update is the introduction of Level 10, a new analysis level that, according to the announcement post, performs a particularly strict type check. It aims to minimize the use of the mixed type in PHP code by explicitly detecting and reporting typed mixed values.
Level 10 encourages developers to provide more precise type specifications and ensure that code does not rely on indeterminate types that can potentially lead to runtime errors.
Videos by heise
New list type and optimized memory management
Another focus is the introduction of a so-called List type, an array of consecutive integer keys that start at 0 and have no gaps. According to the announcement article, List is one of the advanced types that can be represented in PHPDocs. The following example can be found in the release announcement:
/** @param list<int> $listOfIntegers */
public function doFoo(array $listOfIntegers): void
{
}
Developers should find it easier to work with arrays thanks to a clearer definition of specific data structures.
PHPStan 2.0 optimizes memory management. According to the development team, it was possible to reduce memory consumption by up to 70 percent, which apparently has a positive effect on performance and the speed of analyzing large code bases.
(Image:Â AntonKhrupinArt/Shutterstock.com)
The betterCode() PHP 2024 will take place on November 21. The online conference on the PHP programming language supports developers in freeing their PHP applications from legacy issues and checking them for best practices. Be ready for the upcoming innovations: PHP 8.4, PHPUnit 12 and much more! Tickets and further information can be found on the conference website.
Validation of the PHPDoc tag @var
The update also revises the validation of the @var PHPDoc tag, i.e. annotations in PHP documentation comments that specify the data type of a variable or expression, to improve readability and facilitate type checks by developers and analysis tools. PHPStan 2.0 matches these tags against the native types of the assigned expressions, which should help identify inconsistent and potentially erroneous type declarations. In addition, PHPStan checks the purity of functions tagged with @phpstan-pure and reports invalid markup as errors.
Finally, PHPStan 2.0 reduces the dependency on caching, which speeds up the analysis and reduces the memory footprint. The development aims to further increase the reliability and efficiency of code analysis.
More information about PHPStan 2.0 can be found in the announcement post on the PHPStan blog.
(mdo)