PHPStan, a static analysis tool for PHP, recently released version 2.0. This release introduces new features and improvements that enhance code analysis, performance, and user experience.

Ond艡ej Mirtes, founder & developer at PHPStan, discussed the release in a blog post. PHPStan 2.0 introduces a new analysis level, Level 10, which is an increment to the existing Level 9. The Level 9 focused on identifying unsafe uses of the mixed type, while Level 10 detects implicitly-typed mixed values that might get missed through previous checks. Level 10 is beneficial for developers aimed at achieving maximum code safety by eliminating ambiguities associated with the mixed type.

The new version also improves PHP's array handling by introducing a dedicated list type. By definition, a list in PHPStan is an array with sequential integer keys starting from zero without gaps. This feature enables developers to specify clear data structures in their PHPDocs, facilitating better code documentation and analysis.

Another important improvements in PHPStan 2.0 is its reduced memory consumption. This further leads to faster performance. Earlier version of PHPStan's had high memory usage which caused it to be terminated by continuous integration (CI) runners. The new version fixes this by virtue of efficient memory management. This involves cleaning up reference cycles in abstract syntax tree (AST) nodes. As a result, memory consumption is reduced by approximately 50-70% on large projects, thereby speeding up the analysis process.

Ond艡ej also engaged with the PHPStan enthusiasts on Reddit. As of this writing, the post has 234 upvotes and 34 comments. One of the Reddit users welcomed the reduced memory usage feature and commented,

The reduced memory usage is huge. PHPStan is a very memory hungry beast

Ond艡ej responded to multiple queries on the posts, providing guidance and promoting best practices. One of the threads discussed making the property read only and the other discussed excluding whole files.

PHPStan 2.0 also introduces validation for inline @var PHPDoc tags against the native types of assigned expressions. This feature helps identify discrepancies and potential errors in type declarations that could lead to runtime issues. By ensuring that the declared types in @var tags align with actual code behavior, PHPStan promotes more accurate and reliable documentation practices.

The concept of pure functions—those that always return the same output for the same input without side effects—is now enforced in PHPStan 2.0. The tool identifies impure points within the pure functions and reports them as errors. This enforcement helps in maintaining clean, predictable codebases and assists in identifying dead code that can be safely removed.

PHPStan has simplified its caching mechanism to reduce disk space usage without compromising performance. Eliminating the unnecessary cache files related to variadic function checks, the tool now frees up significant disk space. It relies on result caching for efficient analysis.

With ongoing support from PHPStan Pro and GitHub Sponsors, PHPStan plans to continue enhancing the tool's capabilities. One of the immediate goals post-2.0 release is to add support for PHP 8.4 by the end of 2024.

The team is committed to cultivating an active community of contributors and expanding PHPStan's ecosystem of extensions.

微信扫一扫