[phc-general] Statical code analysis (metrics) with PHC

Paul Biggar paul.biggar at gmail.com
Sun Nov 25 17:14:15 GMT 2007


Hi Matthias,

On Nov 25, 2007 3:10 PM, Matthias Kleine <m.kleine at interexa.de> wrote:
> I'd like to ask you which ready-to-use code analysis plugins
> are known for PHC today. There are the count_statements plugins
> in the "standard release", which I found quite useful. But I
> am asking myself the following questions:
>
> - I want to execute code analysis over a large code base consiting
>   of hundrets of php files and lets say about a million lines of
>   code. The count statements plugin works on a single file. Is
>   there an easy possibility of using the plugin on multiple files
>   (exept using some shell/awk stuff like
>   find . "*.php" |xargs phc --run=myplugin | awk ... asf.

So far, we havent had a need for this. I think we'll need to process
multiple files at a later date, for generating code from a complete
web app, However, this would probably use a different interface, so I
think working around it at the command line is better for your
problem. One command I use for things like this is:

for i in `find . -name "*.php"`; do echo $i; phc --run=myplugin; done

One alternative would be to have a single file which include()s all
your others, and using the --include flag. A simple way at the command
line would be something like:

find . -name "*.php" | sed 's/(.*)/include (\1)/' | phc --run=plugin


> - Which other plugins are known today for statical code analysis, like
>   - number of classes
>   - number of methods
>   - average number of lines of code per class or method
>   - ... ?

These arent too complicated. Plugins to calculate number of classes or
methods would be easy. As for number of lines, unfortunately line
counts are a bit poor at the moment. That said, line count isnt a good
measure of the effort in a method, so perhaps you'd prefer the number
of statements (or even number of expressions?), which is easy.

I believe a good measure of code complexity is called cyclomatic
complexity. I expect this wouldnt be very hard to work out in phc, but
I might be missing something.


> - Are there known any phc based projects related to the issue of
>   statical code analysis? Can you link me to some further
>   information in case you have some available.

I'm not aware of any, which means there probably arent any.


> By the way, I would also be glad to get some hints for statical
> analysing tools for PHP which might not be based on phc (except the
> Code Analysing feature of Zend Studio, which is more then nothing,
> but not even more then just more ...).

I'm not familiar with any, unfortunately. Googling for "php cyclomatic
complexity" shows a few things, but I cant tell from a quick glance
whether they are any good.

Paul

-- 
Paul Biggar
paul.biggar at gmail.com


More information about the phc-general mailing list