📜 ⬆️ ⬇️

Visualization of intra-class relationships using GraphViz

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"); //,     $c2g->showVariables(true); //-   ,      $svg = $c2g->convert("Class2GV.php", "dot"); //   dot,    header("Content-type: image/svg+xml"); readfile($svg); 


Class2GV.png


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.
Class2GV_wv.png


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.

Source: https://habr.com/ru/post/127288/


All Articles