Are there any PHP code visualization tools?

15,448

Solution 1

  • KCachegrind - With Xdebug you can profile the execution of your scripts, KCachegrind can generate some pretty awesome call graphs from this
  • nwire for Eclipse

Solution 2

[UPDATE: This answer does not handle namespaces, so is basically obsolete. I'll leave it here in case anyone finds the DOT approach interesting.]

Here's a simple way to graph class inheritance in PHP.

Grep for class definitions and then transform the grep output to DOT syntax. NOTE: This process WILL require trial and error in your situation. Run the grep separately, and tweak it to show the right class definition lines before putting it in the script!

This script was for PHP on standard *nix (I used Ubuntu), with graphviz installed, and using grep -v to exclude some directories that were of no interest because I was looking at a CakePHP codebase. Fdp worked better than sfdp, dot, circo or neato in this situation.

Create generateClassHierarchy.sh

#!/bin/bash
echo 'digraph code {' > code.dot;
grep -r "^class " * | grep -v "^app/vendors" | grep -v "^cake/" | grep -v "Binary file" | sed 's/.*://' | sed 's/class /    /' | sed 's/ extends / -> /' | sed 's/ implements .*//'  | sed 's/ \?{.*$//' | sort >> code.dot  
echo '}' >> code.dot; 
fdp -Tpng -ocode.fdp.png code.dot 2> /dev/null # Ignore syntax error
echo "OK"; 

Then just:

cd /var/www/my_app/                     # or wherever
bash ~/shell/generateClassHierarchy.sh  # or wherever
eog code.fdp.png 

Replace eog with your preferred image viewer. I have run this on Zend Framework as a test, and produced a 22 megabyte PNG graph. Running it on just Zend_Db shows you this (example is on my site):

http://chapman.id.au/generate-php-class-inheritance-diagrams-in-graphviz

Solution 3

Maybe http://phpcallgraph.sourceforge.net/ for static analysis.

It can output to various formats.

Solution 4

BOUML can make UML diagrams out of existing PHP code

Share:
15,448

Related videos on Youtube

Simon
Author by

Simon

Updated on April 06, 2022

Comments

  • Simon
    Simon about 2 years

    Looking for software that will analyze php code (i.e. all of wordpress or the thematic theme) and show me pretty pictures (perhaps a block diagram) of all the connections to help me more quickly get an understanding of where things are and what's connected to what.

    Ideally, this software would run on a Mac, but I'll take anything: Windows, Linux, web-based, etc.

    • John Feminella
      John Feminella over 14 years
      I think there's something fundamentally wrong about the design of blogging software that requires you to have a PHP code visualizer to understand its themes.
    • Gordon
      Gordon over 14 years
      nwiresoftware.com comes to mind. But it is commercial.
    • Adriano Varoli Piazza
      Adriano Varoli Piazza over 14 years
      @John not really, but when you just start it can be hard to districate yourself from the tangle. I've wished for something like this in some projects, but then I memorized their structures myself.
    • John Feminella
      John Feminella over 14 years
      @Adriano: I guess my point is that themes for blogging software are fundamentally supposed to be easy to adapt and modify -- ease of customization is usually a major selling point. If you have to bust out a code visualizer just to get started, that tells me that whoever designed the plugin system for the blog platform didn't think things all the way through.
    • Gordon
      Gordon over 14 years
      @John while I agree, the author also said he's using Wordpress aka the blog system with the codebase from hell ;)
    • John Feminella
      John Feminella over 14 years
      @Gordon: Actually, I don't think he said that anywhere, but I think it's a safe assumption on your part.
    • Viktor Borítás
      Viktor Borítás about 4 years
      Hi Simon, (or others) have you found proper PHP visualizer since then? The question is still totally relevant in 2020 at least for me! I'm looking for software, tools to do this for me instead me manually drawing/mapping out (quite slowly) the complex wp plugins' architecture in some mindmapper tools and so..(it can take hours, days otherwise) Cheers for any fresh hints!
  • emmanuel honore
    emmanuel honore over 10 years
    @Myles The old version was free, maybe you find it?
  • dbu
    dbu about 10 years
    nice one, thanks. i tweaked things by also including abstract classes. first bit becomes egrep -r "^(abstract class|class) ". note that you can chain further grep and grep -v things to limit what classes you want to operate on.
  • BlackBurn027
    BlackBurn027 about 6 years
    This suggestion is purely far from context of the question and bad understanding of the question.
  • James
    James about 2 years
    Version 7.0 and later are now free. New URL for project: bouml.fr