Published 17/02/2022 at 11:29pm
We can use some tools to fix our code; poorly formatted code, find code which is overly complex, check for duplicate code etc. This is really powerful, it will make code easier to understand and easier to maintain. Let's give our code the respect it deserves.
Analysis of software that is performed without executing the program or programs. This means that you take the source code, run tests and analysis on it and the analysis tool knows how to interpret the code.
Having much experience with PHP I'll talk you through PHP examples.
As I write this article phan requires a version of PHP 7.1+
phpmdThere are many ways to install it. This is a really easy setup but others such as composer are just as easy.
https://github.com/phan/phan/wiki/Getting-Started
I'm going to use guide through the brew method. Brew is a package manager - if you don’t have it then you can download it using the link below
Then run these commands. Replace your PHP version as appropriate. e.g. PHP 7.2 would be php72
brew update
brew install php74 php74-ast phan
brew -h
A little note, incase you wondered what you were installing, ast stands for abstract syntax tree (i.e. the structure of the code) and is an intermediary structure in the compilation process.
Phan needs a list of files so let’s get all of the file which have PHP in them
grep -R -l "<?php" * > filelist.txt
sed -n "/vendor/\!p" filelist.txt > filelist_excluding_vendor.txt
phan -f filelist_excluding_vendor.txt -o iaptus_phan_output.csv -b -p -m csv
Further manipulation may be required in order to put the output in to a more queryable format. Consider grouping the same problem so that you can fix all of the same type rather than one by one. It make more sense to do it file by file if you want to limit the impact of the area of the system.
Note: Use an IDE to fix the issues for you (e.g. PHPStorm has an option "Run inspection by name") which will work for some of the inspections. Have a play and see what it can do.
phpmd and phpcs to highlight issues in the file you’re working onThanks and I hope you found it useful.
© Louis Rickman 2025