Visualize PHP code with Checkstyle report (from CodeSniffer)

1.8k views Asked by At

PHP CodeSniffer is a very good tool to help us check our PHP source code. But the report from CodeSniffer is not easy to read.

I found that CodeSniffer can output 'Checkstyle' xml report. Is that any way to Visualize PHP code with Checkstyle xml report, so that every developer can read code and report in one page?

In fact, I found a tool named phpUnderControl, which looks like a very good Continuous Integration tool and something inside it could cover my requirement. But I have no plan to change my Continuous Integration tool (I'm using Apache Continuum).

So if anyone can tell me a simple tool or plug-in, that would be the best.

Thanks.

1

There are 1 answers

0
Peter On

This is really more of a comment/question, but SO thinks it is too long so I'll phrase it as an answer:

Assuming you want to take output like this:

$ phpcs --report=checkstyle /path/to/code

<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="1.0.0">
 <file name="/path/to/code/myfile.php">
  <error line="2" column="1" severity="error" message="Missing file doc comment" source="PEAR.Commenting.FileComment"/>
  <error line="20" column="43" severity="error" message="PHP keywords must be lowercase; expected &quot;false&quot; but found &quot;FALSE&quot;" source="Generic.PHP.LowerCaseConstant"/>
  <error line="47" column="1" severity="error" message="Line not indented correctly; expected 4 spaces but found 1" source="PEAR.WhiteSpace.ScopeIndent"/>
  <error line="47" column="20" severity="warning" message="Equals sign not aligned with surrounding assignments" source="Generic.Formatting.MultipleStatementAlignment"/>
  <error line="51" column="4" severity="error" message="Missing function doc comment" source="PEAR.Commenting.FunctionComment"/>
 </file>
</checkstyle>

and render a version of the original source code with the indicated sections highlighted somehow, then I think you're going to have "to roll your own".

You'd have to write a script which takes the path to a source file in your code repository as an input parameter (e.g. path/to/code) and a chunk of "checkstyle" XML as input (via STDIN), and renders the contents of the file (to STDOUT) as HTML markup.

The body of the file inside should be in a PRE element (to preserve formatting) and each specified line + column with an HREF link to an error/warning "list element" would be at the bottom of the HTML page (I'm not sure what kind of addition color/highlighting can work inside a PRE element).

This is a good idea - I would like to have such a script/tool/utility myself! If I ever get around to writing one I promise to publish it on Github and add a link to it here.

And if you ever find/write one, PLEASE answer your own question, OK?