PHPStan

From Freephile Wiki
Jump to navigation Jump to search

https://phpstan.org/ has a quarter of a million installs as of early 2024 (just from packagist). Written largely by Ondřej Mirtes.

PHPStan seems more polished (perhaps because it's commercial and has a 'pro' version that adds a GUI) whereas phan is the original PHP static analysis tool Rasmus Ledorf uses.

PHPStan has some very notable projects that integrate it: Symphony, Doctrine, Monolog, and many more. (A common refrain: there are PHPStan integrations for Drupal and Wordpress, but not MediaWiki.)

Adding a configuration file for your MediaWiki extension is straightforward and would look like this:

parameters:
	level: 1
	paths:
		- src
		- tests
	scanDirectories:
		- ../../includes
		- ../../tests/phpunit
		- ../../vendor


The level can be 0 - 9 and are "intuitive" with zero being the lowest "lax" level, and 9 being the highest level of strictness. This is the opposite of Phan levels where zero is the highest level of strictness.

The paths are the directories of your code.

The scanDirectories are additional paths used to discover symbols, but not analyze for errors.

Analyzing a forest[edit | edit source]

See PHPStan/configuration for a sample configuration file used for auditing a large installed base of MediaWiki extensions.

Analyzing in depth[edit | edit source]

For an example of best-practices, single extension usage, see the example of Professional Wiki's Maps extension which illustrates configuration file includes of a 'baseline'; error message suppression; and directory exclusions of problem code.

Adding Extensions to PHPStan[edit | edit source]

The PHPStan project has built a Composer plugin for the automatic installation of PHPStan extensions, phpstan/extension-installer.

All you have to do is composer require --dev phpstan/extension-installer in your project and then you can e.g. composer require --dev phpstan/phpstan-phpunit to also add the PHPUnit extension to PHPStan rather than having to manually edit the include section of your phpstan.neon configuration.

There are extensions for Doctrine, PHPUnit, Symfony Framework, Mockery, Nette Framework, Laravel, Drupal, WordPress etc. See the extension library