Back to Phpstan

Extension Library

website/src/user-guide/extension-library.md

2.1.545.5 KB
Original Source

Unique feature of PHPStan is the ability to define and statically check "magic" behaviour of classes - accessing properties that are not defined in the class but are created in __get and __set and invoking methods using __call.

PHPStan also allows writing custom rules for situations that aren't objective bugs or other problems in the code, but allow people avoid tricky situations or enforce the way they want to write their code. These custom rules take advantage of the abstract syntax tree, advanced type inference engine, PHPDoc parser, and class reflection data.

Learn more about writing custom extensions »

Installing extensions

Users can install various PHPStan extensions to enhance the capabilities of the static analyser. Many extensions already support phpstan/extension-installer Composer plugin, so in order to enable an extension, it's sufficient to require it in your composer.json:

bash
composer require --dev phpstan/extension-installer && \
composer require --dev phpstan/phpstan-beberlei-assert

If you can't or don't want to use phpstan/extension-installer, include the extension's configuration file manually in the includes section:

yaml
includes:
	- vendor/phpstan/phpstan-beberlei-assert/extension.neon
<div class="bg-blue-100 border-l-4 border-blue-500 text-blue-700 p-4 mb-4" role="alert">

Why wouldn't I want to always use phpstan/extension-installer?

It always enables all the functionality that an extension offers. If you for example want to use only some of the rules from phpstan-strict-rules, or if you only want to use extension.neon (but not rules.neon) from phpstan-doctrine, you can't use the extension installer plugin and must include chosen files manually.

</div>

Official extensions

Check out phpstan-strict-rules repository for extra strict and opinionated rules for PHPStan.

Check out as well phpstan-deprecation-rules for rules that detect usage of deprecated classes, methods, properties, constants and traits!

Framework-specific extensions

Unofficial extensions

3rd party rules

Find more on Packagist!