{"id":27276,"date":"2017-01-30T00:56:53","date_gmt":"2017-01-29T22:56:53","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=27276"},"modified":"2017-01-30T00:56:53","modified_gmt":"2017-01-29T22:56:53","slug":"generate-php-class-inheritance-diagrams-with-graphviz","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/generate-php-class-inheritance-diagrams-with-graphviz\/","title":{"rendered":"Generate PHP Class Inheritance Diagrams with Graphviz"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>It&#8217;s not a secret that I&#8217;m a big fan of <a href=\"http:\/\/www.graphviz.org\/\">GraphViz<\/a>\u00a0&#8211; powerful, yet easy to use graph visualization software. \u00a0And I&#8217;ve <a href=\"https:\/\/mamchenkov.net\/wordpress\/tag\/graphviz\/\">blog about it<\/a> quite a few times. \u00a0Well, guess what &#8211; today is a perfect day for one more post.<\/p>\n<p>Over the weekend I was working on refactoring some CakePHP code that I had my on for a long time now. \u00a0As with any powerful framework, the projects can get quite big and it might be difficult to make sense of the hierarchy and relationship of certain files, especially if it&#8217;s been a while since you looked at that code, and you are not a fan of IDEs, and you&#8217;ve been non-stop coding for more than 24 hours.<\/p>\n<p>I had this problem once or twice before. \u00a0To the extent that I&#8217;ve written a <a href=\"https:\/\/github.com\/mamchenkov\/CakePHP-GraphViz-Models\">CakePHP plugin<\/a>, which was using GraphViz to visualize models and their relationships. \u00a0But that was a long time ago. \u00a0The plugin got outdated (it was for CakePHP 1 and\/or 2, so not very useful now that we are in version 3). \u00a0Furthermore, today I needed to look not at the models and their relationships, but at a hierarchy of the PHP classes in some other directory.<\/p>\n<p>A quick Google search suggested Nigel Chapman&#8217;s post &#8211;\u00a0<a href=\"http:\/\/chapman.id.au\/generate-php-class-inheritance-diagrams-in-graphviz\">How to Generate PHP Class Inheritance Diagrams in Graphviz<\/a>. \u00a0Not only it was pretty much exactly what I was looking for, but it was also from a fellow CakePHP developer!<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"27277\" data-permalink=\"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/generate-php-class-inheritance-diagrams-with-graphviz\/class-hierarchy\/\" data-orig-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?fit=2470%2C1330&amp;ssl=1\" data-orig-size=\"2470,1330\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"class hierarchy\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?fit=660%2C355&amp;ssl=1\" class=\"aligncenter size-medium wp-image-27277\" src=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy-500x269.png?resize=500%2C269&#038;ssl=1\" alt=\"\" width=\"500\" height=\"269\" srcset=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?resize=500%2C269&amp;ssl=1 500w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?resize=768%2C414&amp;ssl=1 768w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?resize=1024%2C551&amp;ssl=1 1024w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?w=1320 1320w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/01\/class-hierarchy.png?w=1980 1980w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>I&#8217;ve taken (borrowed?) Nigel&#8217;s script and modified it slightly. \u00a0You can find my version <a href=\"https:\/\/github.com\/mamchenkov\/dotfiles\/blob\/master\/roles\/dotfiles\/files\/bin\/phpgraph\">here<\/a>. \u00a0The changes are:<\/p>\n<ol>\n<li>Added two parameters which allow to specify the name of the destination file and the GraphViz tool to use for the generation of the graph (more on that below). \u00a0If omitted, the destination file will be created with the temporary name in the temporary directory, and the <em>sfdp<\/em> script will be used as default.<\/li>\n<li>Added the resulting path to the output at the end of the execution of the script, so that if it is a temporary file, I would know where to look for it.<\/li>\n<li>Added <em>grep<\/em> and <em>sed<\/em> patterns to work correctly with abstract classes.<\/li>\n<li>Changed <em>grep<\/em> to look only for the <em>*.php<\/em> files, instead of anything. \u00a0This can make things much faster on messy projects, and it also removes the need for working around the binary files.<\/li>\n<\/ol>\n<p>Now it&#8217;s so handy that I made it a part of <a href=\"https:\/\/github.com\/mamchenkov\/dotfiles\">my dotfiles<\/a>, so that I can save a few precious moments looking for the solution in 2024.<\/p>\n<p>This also seems like a good opportunity to review the which tools are available for generating the graph and what are the differences between them. \u00a0For that, we don&#8217;t need to look any further than the <a href=\"http:\/\/www.graphviz.org\/\">front page of the GraphViz website<\/a> (in the Roadmap section):<\/p>\n<blockquote><p><strong>dot &#8211; <\/strong>&#8220;hierarchical&#8221; or layered drawings of directed graphs. This is the default tool to use if edges have directionality.<\/p>\n<p><strong>neato &#8211; <\/strong>&#8220;spring model&#8221; layouts.\u00a0 This is the default tool to use if the graph is not too large (about 100 nodes) and you don&#8217;t know anything else about it. Neato attempts to minimize a global energy function, which is equivalent to statistical multi-dimensional scaling.<\/p>\n<p><strong>fdp &#8211; <\/strong>&#8220;spring model&#8221; layouts similar to those of neato, but does this by reducing forces rather than working with energy.<\/p>\n<p><strong>sfdp &#8211; <\/strong>multiscale version of fdp for the layout of large graphs.<\/p>\n<p><strong>twopi &#8211; <\/strong>radial layouts, after Graham Wills 97. Nodes are placed on concentric circles depending their distance from a given root node.<\/p>\n<p><strong>circo &#8211;<\/strong> circular layout, after Six and Tollis 99, Kauffman and Wiese 02. This is suitable for certain diagrams of multiple cyclic structures, such as certain telecommunications networks.<\/p><\/blockquote>\n<p>As they say, GraphViz to the rescue once again!<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>It&#8217;s not a secret that I&#8217;m a big fan of GraphViz\u00a0&#8211; powerful, yet easy to use graph visualization software. \u00a0And I&#8217;ve blog about it quite a few times. \u00a0Well, guess what &#8211; today is a perfect day for one more post. Over the weekend I was working on refactoring some CakePHP code that I had &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/generate-php-class-inheritance-diagrams-with-graphviz\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Generate PHP Class Inheritance Diagrams with Graphviz<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Generate PHP Class Inheritance Diagrams with Graphviz #WebDev #PHP #GraphViz","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62],"tags":[2276,38,1330],"keyring_services":[],"class_list":["post-27276","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","tag-graphviz","tag-php","tag-web-development"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":14838,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/08\/cakephp-graphviz-models\/","url_meta":{"origin":27276,"position":0},"title":"CakePHP GraphViz Models","author":"Leonid Mamchenkov","date":"May 8, 2011","format":false,"excerpt":"I have completely and totally rewritten my old script that generates a graph of CakePHP models and their relationships. \u00a0Instead of pasting the code in here, I pushed all of its development to GitHub where it now enjoys a new repository. \u00a0Please have a look, try it out, and let\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28449,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/03\/26\/php-object-graph-visualizer\/","url_meta":{"origin":27276,"position":1},"title":"PHP object graph visualizer","author":"Leonid Mamchenkov","date":"March 26, 2018","format":false,"excerpt":"koriym\/print_o is an object graph visualizer for PHP.\u00a0 Here's a Wikipedia answer to the question of \"What is an object graph?\": Object-oriented applications contain complex webs of interrelated objects. Objects are linked to each other by one object either owning or containing another object or holding a reference to another\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2018\/03\/object-graph-500x259.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28182,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/11\/06\/object-graph-visualizing-php-objects\/","url_meta":{"origin":27276,"position":2},"title":"object-graph &#8211; visualizing PHP objects","author":"Leonid Mamchenkov","date":"November 6, 2017","format":false,"excerpt":"As you might know, I am a big fan of GraphViz.\u00a0 I've used numerous times for visualizing different parts of the project code and dependencies (see here and here for example). Today I came across a way to visualize PHP objects (not just classes) - object-graph library by Sebastian Bergmann,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/11\/object-graph-500x136.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":14845,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/08\/day-in-brief\/","url_meta":{"origin":27276,"position":3},"title":"Day in brief","author":"Leonid Mamchenkov","date":"May 8, 2011","format":false,"excerpt":"GitHub: mamchenkov pushed to master at mamchenkov\/CakePHP-GraphViz-Models http:\/\/bit.ly\/itj7OI # GitHub: mamchenkov pushed to master at mamchenkov\/CakePHP-GraphViz-Models http:\/\/bit.ly\/mBMhZm # GitHub: mamchenkov pushed to master at mamchenkov\/CakePHP-GraphViz-Models http:\/\/bit.ly\/kDo9Rf # GitHub: mamchenkov merged pull request 1 on mamchenkov\/CakePHP-GraphViz-Models http:\/\/bit.ly\/kgmSF7 # I'm at Limassol Municipality Garden (Limassol) http:\/\/4sq.com\/ljqm0f # I'm at Kirzis Center (Emmanoil\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":14837,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/07\/day-in-brief\/","url_meta":{"origin":27276,"position":4},"title":"Day in brief","author":"Leonid Mamchenkov","date":"May 7, 2011","format":false,"excerpt":"GitHub: mamchenkov created branch master at mamchenkov\/CakePHP-GraphViz-Models http:\/\/bit.ly\/mmbn2E # GitHub: mamchenkov created repository CakePHP-GraphViz-Models http:\/\/bit.ly\/mw9J0o # I'm at Carrefour http:\/\/4sq.com\/jPuyNd #","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12274,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/03\/20\/cakephp-graphviz-making-sense-of-a-numerous-models\/","url_meta":{"origin":27276,"position":5},"title":"CakePHP + GraphViz = making sense of a numerous models","author":"Leonid Mamchenkov","date":"March 20, 2010","format":false,"excerpt":"NOTE: THIS IS VERY MUCH OUTDATED! Read about the update or go directly to GitHub repository for the new version. \u00a0 I have a task at hand.\u00a0 I have to re-introduce myself to a rather large codebase.\u00a0 It's a project that migrated to CakePHP a couple of years ago and\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2010\/03\/graph.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=27276"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27276\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=27276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=27276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=27276"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=27276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}