PHPackages                             smeghead/php-class-diagram - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. smeghead/php-class-diagram

ActiveLibrary

smeghead/php-class-diagram
==========================

A CLI tool that parses the PHP source directory and outputs PlantUML scripts.

v1.6.1(4mo ago)14713.8k↓28.8%12[3 PRs](https://github.com/smeghead/php-class-diagram/pulls)3Apache-2.0PHPPHP &gt;=8.2CI passing

Since Nov 8Pushed 4mo ago4 watchersCompare

[ Source](https://github.com/smeghead/php-class-diagram)[ Packagist](https://packagist.org/packages/smeghead/php-class-diagram)[ RSS](/packages/smeghead-php-class-diagram/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (44)Used By (3)

php-class-diagram
=================

[](#php-class-diagram)

A CLI tool that parses the PHP source directory and generates PlantUML class diagram scripts as output.

[![Testing](https://github.com/smeghead/php-class-diagram/actions/workflows/php.yml/badge.svg?event=push)](https://github.com/smeghead/php-class-diagram/actions/workflows/php.yml/badge.svg?event=push) [![Latest Stable Version](https://camo.githubusercontent.com/51e977e1c6bb7ae3f647f81e669dbeddefdb315b9b7bd02618beeaa2ec555245/68747470733a2f2f706f7365722e707567782e6f72672f736d6567686561642f7068702d636c6173732d6469616772616d2f76)](https://packagist.org/packages/smeghead/php-class-diagram) [![Total Downloads](https://camo.githubusercontent.com/2c672e1dd37698928f55486c7e46da933b0db1f91dceb04ab4cbfbdcdf1eaeb4/68747470733a2f2f706f7365722e707567782e6f72672f736d6567686561642f7068702d636c6173732d6469616772616d2f646f776e6c6f616473)](https://packagist.org/packages/smeghead/php-class-diagram) [![Latest Unstable Version](https://camo.githubusercontent.com/e7184d16fd9aa323c66797ebe8df1ee7deba08acd76a5b3a2c8ad19483aa6f2a/68747470733a2f2f706f7365722e707567782e6f72672f736d6567686561642f7068702d636c6173732d6469616772616d2f762f756e737461626c65)](https://packagist.org/packages/smeghead/php-class-diagram) [![License](https://camo.githubusercontent.com/85530b3cb4089295d1093673583cfb254dbbd85dd091a3d31ae61ace10d11913/68747470733a2f2f706f7365722e707567782e6f72672f736d6567686561642f7068702d636c6173732d6469616772616d2f6c6963656e7365)](https://packagist.org/packages/smeghead/php-class-diagram) [![PHP Version Require](https://camo.githubusercontent.com/8cf1c6dabe0f1b411e844ac86d816fe06441eeecb01169b96a682fe30f312094/68747470733a2f2f706f7365722e707567782e6f72672f736d6567686561642f7068702d636c6173732d6469616772616d2f726571756972652f706870)](https://packagist.org/packages/smeghead/php-class-diagram)

Features
--------

[](#features)

- ♻️ Generating class diagrams from source code contributes to continuous design improvement.
- 🔖 It produces expressive class diagrams with a focus on namespaces and relationships.
- 🌐 Diagrams are interactive hypermedia tools, allowing you to click on class boxes to navigate directly to the source code, enhancing understanding both visually and interactively.
- 🔧 This simple CLI tool is easy to use.
- 💡 Additionally, it can generate a package relationship diagram to visualize dependencies on external namespaces.

What is PlantUML
----------------

[](#what-is-plantuml)

> [PlantUML - Wikipedia](https://en.wikipedia.org/wiki/PlantUML)PlantUML is an open-source tool allowing users to create diagrams from a plain text language. Besides various UML diagrams, PlantUML has support for various other software development related formats (such as Archimate, Block diagram, BPMN, C4, Computer network diagram, ERD, Gantt chart, Mind map, and WBD), as well as visualisation of JSON and YAML files.

Hypermedia Feature
------------------

[](#hypermedia-feature)

One of the standout features of this tool is the ability to generate class diagrams with clickable links that navigate directly to the source code of those classes. This transforms the diagrams into interactive hypermedia tools, enhancing the understanding of the source code visually and interactively.

To enable this feature, generate the diagrams in SVG format and use the `--svg-topurl` option to specify the base URL for the links:

```
$ vendor/bin/php-class-diagram \
    --svg-topurl='https://github.com/your-username/your-repo/blob/main/path/to/source' \
    path/to/php/files
```

To embed the SVG diagrams in HTML while preserving the clickable links, use the `embed` or `object` tags instead of `img`. Here’s an example:

```

    PHP Class Diagram

        embed {
            max-width: 100%;
            max-height: 200%;
            width: auto;
            height: auto;
        }

```

This ensures that the SVG image is responsive and the hyperlinks remain functional.

[Hypermedia example](https://smeghead.github.io/php-class-diagram/hyper-media.html)

Dogfooding
----------

[](#dogfooding)

### php-class-diagram class diagram (linked SVG file)

[](#php-class-diagram-class-diagram-linked-svg-file)

[![](docs/images/dogfood.svg)](docs/images/dogfood.svg)

### php-class-diagram class diagram without fields and methods

[](#php-class-diagram-class-diagram-without-fields-and-methods)

If your analysis primarily focuses on the relationships between classes, a simpler notation may be more appropriate.

[![dogfood class diagram image.](docs/images/dogfood-model.png)](docs/images/dogfood-model.png)

### php-class-diagram package related diagram

[](#php-class-diagram-package-related-diagram)

Visualizing package relationships can uncover critical design issues.

[![dogfood package related diagram image.](docs/images/dogfood-package.png)](docs/images/dogfood-package.png)

Install
-------

[](#install)

### From DockerHub

[](#from-dockerhub)

You can utilize a Docker image that includes php-class-diagram and PlantUML from the following URL.

-

### From Composer

[](#from-composer)

```
$ mkdir example
$ cd example
$ composer init
$ composer require --dev smeghead/php-class-diagram
```

Now, you can execute `./vendor/bin/php-class-diagram`. For instance, try to display help message.

```
$ vendor/bin/php-class-diagram --help
usage: php-class-diagram [OPTIONS]

A CLI tool that parses the PHP source directory and generates PlantUML class diagram scripts as output.

OPTIONS
  -h, --help                       show this help page.
  -v, --version                    show version.
      --class-diagram              output class diagram script. (default)
      --package-diagram            output package diagram script.
      --division-diagram           output division diagram script.
      --jig-diagram                output class diagram and package diagram script.
      --enable-class-properties    describe properties in class diagram. (default)
      --disable-class-properties   not describe properties in class diagram.
      --enable-class-methods       describe methods in class diagram. (default)
      --disable-class-methods      not describe methods in class diagram.
      --enable-class-name-summary  describe classname with Class summary of document comment. (default)
      --disable-class-name-summary describe classname without Class summary of document comment.
      --hide-private               hide private properties and methods.
      --hide-private-properties    hide private properties.
      --hide-private-methods       hide private methods.
      --svg-topurl='url'           Specifies the top URL when displaying the class as a link when outputting in SVG format.
      --header='header string'     additional header string. You can specify multiple header values.
      --include='wildcard'         include target file pattern. (default: `*.php`) You can specify multiple include patterns.
      --exclude='wildcard'         exclude target file pattern. You can specify multiple exclude patterns.
      --rel-target-from='classes'  comma separated list of classes to filter dependencies from
      --rel-target-to='classes'    comma separated list of classes to filter dependencies to
      --rel-target='classes'       comma separated list of classes to filter dependencies from or to. this option overrides
                                   --rel-target-from and --rel-target-to if set.
      --rel-target-depth=integer   max depth of dependencies to show when using --from or --to filters
```

How to execute
--------------

[](#how-to-execute)

### Class Diagram

[](#class-diagram)

If there are three PHP source files in the directory test/fixtures/no-namespace each with a TYPE comment,

- php source files.

```
└─test
    └─fixtures
        └─no-namespace
            └─product
                    Product.php
                    Name.php
                    Price.php

```

- Product.php

```
