Testing
Testing is software development.
Software development is writing code. Testing makes sure the code actually works, so in a nutshell: Testing is software development :-)
Contents
Phan[edit | edit source]
Phan is a static analyzer for PHP. It will help you write better PHP code. Please note that your source code To use it, you'll need to install the Abstract Syntax Tree PHP extension. See the tutorial for analyzing a large sloppy codebase
The MediaWiki project uses Phan in its Continuous Integration.
Links[edit | edit source]
- mediawiki-tools-phan on github
- phan/phan on github
- nikic/php-ast on github
- Phan Getting-Started
- Phan CLI-HELP.md
Static Analysis of MediaWiki[edit | edit source]
For some current analysis see https://doc.wikimedia.org/mediawiki-core/master/phpmetrics/complexity.html
- Continuous_integration/Entry_points
- Continuous_integration/Phan
- Continuous_integration/Tutorials/Add_phan_to_a_MediaWiki_extension
- Best_practices_for_extensions#File_structure
The on-wiki documentation (at MediaWiki.org) and even the upstream projects do not exactly provide a usable guide for MediaWiki implementors to use Phan on an extensive codebase (ie. "my whole wiki"). Instead, the documentation describes how MediaWiki extension developers can use Phan by incorporating their code into the larger configuration of MediaWiki Continuous Integration. If you download MediaWiki and composer update (to get dev dependencies) and also install PHP-AST, then you should be able to run composer phan
or ./vendor/bin/phan -p .
But, that analyzes the entire MediaWiki codebase (using a lot of RAM and time), and does not analyze random extensions (found on client's wiki instance) unless those extensions already have their own ./phan/config.php
The traditional form of that[1], for an extension named "MyExtension", which has dependencies on the Echo and SocialProfile extension, is
$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';
$cfg['directory_list'] = array_merge(
$cfg['directory_list'],
[
'../../extensions/Echo',
'../../extensions/SocialProfile',
]
);
$cfg['exclude_analysis_directory_list'] = array_merge(
$cfg['exclude_analysis_directory_list'],
[
'../../extensions/Echo',
'../../extensions/SocialProfile',
]
);
return $cfg;
The reason the normal pattern is to include directories and exclude those same directories is that you include 'dependencies' for class definitions etc. (symbol discovery), but do not warn about issues outside the limits of your own MyExtension extension.
If your Extension does have dependencies on other extensions, and you are hosting your code in gerrit for automated CI checks and fixes (you should), you must create an entry in the so-called parameter-functions.py to declare those dependencies. |
MediaWiki Phan Config[edit | edit source]
The bigger (unanswered) question is what does mediawiki-phan-config do? What is the compiled and complete configuration that you are "running" against your codebase?
MediaWiki Phan Config has a ConfigBuilder.php class that is used to build up the project's phan config. The goto file to see how it's configured is config.php
Phan has a ton of plugins - but their usage in the MediaWiki configuration is not mentioned or described anywhere (that I could find). I found a list in 'base-config-functions' mediawiki-tools-phan/blob/master/src/base-config-functions.php#L68, but why you need to read the entire source code to "reverse engineer" what is happening? It's not fun.
Phan Plugins[edit | edit source]
https://github.com/phan/phan/tree/v5/.phan/plugins#2-general-use-plugins
The writing plugins guide also describes how they work.
Check code for compatibility with a particular PHP version[edit | edit source]
One major reason I wanted to use phan was to upgrade a whole codebase, and check its compatibility with an upgraded PHP (moving from 7.4 to 8.1). There is at least one caveat to doing this: You should be running php 8.1 in order to check code for compatibility with 8.1 You can use phan with the config setting of target_php_version
[2]
Phan with VSCode[edit | edit source]
https://github.com/phan/phan/wiki/Editor-Support supposed to just work (with the plugin). But after I installed the plugin, it was not producing any hint of working or any errors.
Phan checks[edit | edit source]
There are over 1,000 things that phan tests its own code against
https://github.com/phan/phan/tree/v5/tests
https://github.com/phan/phan/tree/v5/tests/files/src
Other Static Analysis tools for PHP[edit | edit source]
PHPStan[edit | edit source]
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.
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 paths are the directories of your code.
The scanDirectories are additional paths used to discover symbols, but not analyze for errors.
For more advanced 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.