Proper Static Analysis of PHP code in your Continuous Integration process

One simple fix can harden your code and prevent bugs in the future.

.com software
2 min readAug 5, 2022
Photo by NEW DATA SERVICES on Unsplash

Long story short: exclude the dev-dependencies before running the analysis.

Lately, I have been analyzing one of my company’s microservice code and its CI process. We’re using the Psalm tool extensively. However as it turned out, the analyzer was run on the code containing dev-dependencies.

The project has quite a few dev-dependencies with each coming with its own set of dev-dependencies. To name a few:

  • phpunit
  • psalm
  • behat
  • selenium
  • selenium drivers
  • panther browser

This poses a slight risk of using dependencies that will be removed during the installation of production dependencies.

During the build in the CI, the code might return valid, yet it will be invalid in the runtime.

To correctly test the code we’ve modified the pipeline to:

  1. install the Composer with dev-dependencies to install the Psalm phar tool
  2. move the Psalm phar to/bin/psalm.phar
  3. remove dev-dependencies using the --no-dev flag
  4. finally run the analyzer with php /bin/psalm.phar

It even turned out that the code was using the wrong assertion library in some parts of the code, but fortunately, the code did never break because the assertion was inside a condition that did not evaluate yet since it was added.

Quick question: was this story of any value to you? Please support my work by leaving a clap as a token of appreciation. Thank you.

--

--

.com software

Father • PHP developer • entrepreneur • working for a €1bn unicorn startup as a backend engineer >>> https://bit.ly/dotcom-software