Turn a Psalm report to HTML in three easy steps!

CodeWithCaen - Nov 9 '22 - - Dev Community

Introduction

Okay so this kinda blew my mind how easy and magical it is, and am mainly writing down this post for myself to remember it.

Basically, in case you did not know already, Psalm is a great static analysis tool for PHP, but if you have a ton of errors it can be hard to keep track of everything when your terminal gets flooded.

Wouldn't it be much easier if you could get the output as an HTML page? While there's no built-in tool for this as far as I can tell, the workaround is surprisingly simple. Let's get to it.

Prerequisites

This guide assumes you already have Psalm set up. You need no additional tools downloaded for this.

Step 1 - Running Psalm

First off, we need to run Psalm, the key here is to save the report as an XML file. This is easy enough to do by redirecting the output to a new file.



vendor/bin/psalm --output-format=xml > psalm-report.xml

Enter fullscreen mode Exit fullscreen mode




Step 2 - Running the Transformer

This is where the fun part comes in, using something called an XSL Transformer (XSLT) we can transform the XML to HTML. The common way to do this seems to be by using the xsltproc software, but I don't want to have to download a bunch of stuff, and you know what? We don't have to. We can run xsltproc online!

Simply visit freeformatter.com/xsl-transformer, and copy your XML document to the first input. In the second input you'll need to add the XSL stylesheet which you can copy from this file: psalm-html-output.xsl.

Step 3 - Saving as local HTML

After the online conversion, all you need to do is copy the contents into a new HTML file in your local text editor. It's just that easy!

Screenshot of the result

Conclusion and Credit

As a sidenote, if you have xsltproc installed, or are willing to install it, you can run all three steps in one via the command line. For that, I defer to the readme of this repository where we got the XSL from https://github.com/Roave/psalm-html-output. Huge thanks to them for creating the XSL document, they did all the work!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .