After reading R. Martin's Pure Code book, I was filled with determination and began to refactor my old, big and dirty project.
And I wanted to see how methods and fields are interconnected in one of the simplest classes.
PhpCallGraph , quickly googled, could not be adjusted (some problems with xdebug), and besides, judging by the examples, it shows connections in the whole project, tracing it, and I needed to explore one class.
I decided to write my own solution, and that's what happened.
Download the code for the experiments , you will need
GraphViz .
')
Using it is very simple:
First, let's analyze the draftsman class itself.
require "Class2GV.php"; $c2g = new Class2GV("C:\\Program Files\\GraphViz 2.28\\bin");
Green rectangles are public methods, yellowish rhombuses are protected (they are not there), red ellipses are private, blue rectangles are not described (inherited or dynamically called, they are not here either). Gray rectangles are variable.
Using variables is always displayed with one arrow, calls to methods - depending on their number.
There is quite a complex, although quite logical class structure. By the way, the more “correct” classes look good exactly when using dot (it tries to draw a graph hierarchically), and the more chaotic ones are better located neato or fdp / sfdp (does not take into account the direction of arrows).
However, if you take a closer look, you can see that the rather coherent area related to parsing the PHP file (using the built-in function
get_all_tokens()
) is highlighted on the right, and you could select it into a separate class. This is especially well seen on the graph without variables.
I didn’t optimize so much, but if the class would grow, it would have to be done.
Here are some more examples of classes.
- The MySQL module from Kohana 3.0 (modules / database / classes / kohana / database / mysql.php) is a great class.

- And here it is Idiorm , simple ORM all-in-one, it can be seen very well (drawn by neato)

- Some class from phpBB 3.0 (includes / functions_module.php), drawn by sfdp, c and without class variables.

I didn’t like phpBB insides, and now I can clearly see that this class answers too much (see chapter 10 of the Clean Code book) and doesn’t hide anything at all. It turns out that these are just functions that are brought together that are related to approximately one area.
- And this is the class for which it was created:
