Open main menu

Changes

Testing is software development.
Software development is writing code. Testing makes sure the code actually works, so in a nutshell: '''Testing is software development ''' :-)
== Phan ==Phan is a '''[[wp:Static program analysis|static analyzer ]]''' for PHP. [https://github.com/phan/phan Phan project on GitHub] It will help you write better PHP code. YouPlease note that your source code To use it, you'll need to install the [https://github.com/nikic/php-ast Abstract Syntax Tree] PHP extension. You can read a See the [https://github.com/phan/phan/wiki/Tutorial-for-Analyzing-a-Large-Sloppy-Code-Base tutorial for how to get Phan working in your projectanalyzing a large sloppy codebase]
The MediaWiki project uses Phan. See the article [[mwhttps:Continuous_integration/Phan|Continuous_integration/github.com/phan/phan Phan]project on GitHub]
==Links==
#https://github.com/wikimedia/mediawiki-tools-phan#https://github.com/phan/phan#https://www.mediawiki.org/wiki/Continuous_integration/Entry_points#https://www.mediawiki.org/wiki/Continuous_integration/The [[MediaWiki]] project uses Phan#https://www.mediawiki.org/wiki/Best_practices_for_extensions#File_structure#https://github.com/nikic/php-ast#https://github.com/phan/phan/wiki/Getting-Started#https://github.com/phan/phan/blob/v5/internal/CLI-HELPin its [[Continuous Integration]].md
===Links=== #[https://github.com/wikimedia/mediawiki-tools-phan mediawiki-tools-phan] on github#[https://github.com/phan/phan phan/phan] on github#[https://github.com/nikic/php-ast nikic/php-ast] on github#Phan [https://github.com/phan/phan/wiki/Getting-Started Getting-Started]#Phan [https://github.com/phan/phan/blob/v5/internal/CLI-HELP.md CLI-HELP.md] ===Static Analysis of MediaWiki===
For some current analysis see https://doc.wikimedia.org/mediawiki-core/master/phpmetrics/complexity.html
#[https://www.mediawiki.org/wiki/Continuous_integration/Entry_points Continuous_integration/Entry_points]#[https://www.mediawiki.org/wiki/Continuous_integration/Phan Continuous_integration/Phan]#[[mediawikiwiki:Continuous_integration/Tutorials/Add_phan_to_a_MediaWiki_extension|Continuous_integration/Tutorials/Add_phan_to_a_MediaWiki_extension]]#[https://www.mediawiki.org/wiki/Best_practices_for_extensions#File_structure 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 to actually can use Phanby 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 <code>composer phan </code> or <code>./vendor/bin/phan -p . </code> But, that analyzes the entire MediaWiki codebase(using a lot of RAM and time), and does '''not''' analyze your extension random extensions (found on client's wiki instance) unless you specifically add a proper those extensions already have their own <code>./phan/config.php file to your extension).</code>
[[Mark Hershberger]] provides an example of what that might look like in his CommentStreams configuration<syntaxhighlight lang="php"><?php$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';$extensions = [ "Echo", "SocialProfile" ];// Assume this extension exists in a directory with a bunch of other extensions that may, or may not be, $IP/extensions$extDir = realpath( __DIR__ . "/../.." );$dirList = array_filter( array_map( fn ( $dir ): string => "$extDir/$dir", $extensions ), fn ( $dir ): bool => is_dir( $dir ));$cfg['directory_list'] = array_merge( $cfg['directory_list'] ?? [], $dirList );$cfg['exclude_analysis_directory_list'] = array_merge( $cfg['exclude_analysis_directory_list'] ?? [], $dirList);$cfg['suppress_issue_types'][] = 'PhanUndeclaredConstant';return $cfg;</syntaxhighlight>The more traditional form of that<ref>https://codesearch.wmcloud.org/things/?q=exclude_analysis_directory_list&files=.phan%2Fconfig.php&excludeFiles=&repos=</ref> , for an extension named "MyExtension", which has dependencies on the Echo and SocialProfile extension, is<syntaxhighlight lang="php">
$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';
return $cfg;
</syntaxhighlight>But why is The reason the normal pattern is to include directories and exclude those same directories? The answer ''seems'' to be is that you include 'dependencies' for class definitiondefinitions etc. (symbol discovery), but do not warn about issues outside the limits of your own MyExtension extension. ==== MediaWiki Phan Config ====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 [https://github.com/wikimedia/mediawiki-tools-phan/blob/master/src/ConfigBuilder.php#L8 ConfigBuilder.php] class that is used to build up the project's phan config. The bigger (unanswered) question goto file to see how it's configured is what does '''[https://github.com/wikimedia/mediawiki-tools-phan-/blob/master/src/config.php config do? What is the compiled and complete configuration that you are "running" against your codebase?.php]'''
== Plugins ==Phan ha has a ton of plugins - but they don't seem to be used their usage in the MediaWiki configurationis not mentioned or described anywhere (that I could find). Or are theyI found a list in 'base-config-functions' [https://github.com/wikimedia/mediawiki-tools-phan/blob/master/src/base-config-functions.php#L68 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 obvious to me yetfun.
===Phan Plugins===
https://github.com/phan/phan/tree/v5/.phan/plugins#2-general-use-plugins
The [https://github.com/phan/phan/wiki/Writing-Plugins-for-Phan writing plugins guide] also describes how they work.
=== Check code for compatibility with a particular PHP version ===
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 <code>target_php_version</code><ref>https://github.com/phan/phan/wiki/Phan-Config-Settings#analysis-of-a-php-version</ref>
=== Phan with VSCode ===
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 ===
There are over 1,000 things that phan tests its own code against
<br />
== Other Static Analysis tools for PHP ==
=== PHPStan ===
https://phpstan.org/
 
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:<syntaxhighlight lang="yaml">
parameters:
level: 1
paths:
- src
- tests
scanDirectories:
- ../../includes
- ../../tests/phpunit
- ../../vendor
</syntaxhighlight>
 
 
The [https://phpstan.org/user-guide/rule-levels level can be 0 - 9]
 
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 [https://github.com/ProfessionalWiki/Maps/blob/ee88211fadb3573b646cce005383450e96c3054e/phpstan.neon 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.
===Psalm===