smeghead/php-class-diagram

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

v0.0.8 2022-11-21 12:41 UTC

This package is auto-updated.

Last update: 2022-11-24 08:25:37 UTC


README

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

Testing Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Features

  • Generating class diagrams from source code helps improve continuous design.
  • Generates expressive class diagrams with an emphasis on namespaces and relationships.
  • A simple CLI tool that is easy to handle.
  • It is also possible to output a package relationship diagram that visualizes the dependency on the external namespace.

php-class-diagram class diagram

dogfood class diagram image.

php-class-diagram package related diagram

dogfood package related diagram image.

Install

From DockerHub

You can use docker image includes php-class-diagram and plantuml, from below URL.

From Composer

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

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] <target php source directory>

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

OPTIONS
  -h, --help                     show this help page.
      --class-diagram            output class diagram script. (default)
      --package-diagram          output package 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.
      --php5                     parse php source file as php5.
      --php7                     parse php source file as php7.
      --php8                     parse php source file as php8. (not suppoted)
      --header='header string'   additional header string. You can specify multiple header values.

How to execute

Class Diagram

When three php source files that TYPE commented exist in test/fixtures/no-namespace,

  • php source files.
└─test
    └─fixtures
        └─no-namespace
            └─product
                    Product.php
                    Name.php
                    Price.php
  • Product.php
<?php
class Product {
    /** @var Name   product name. */
    private $name;
    /** @var Price  price of product. */
    private $price;
}
  • Name.php
<?php
class Name {
    /** @var string  name. */
    private $name;
}
  • Price.php
<?php
class Price {
    /** @var int  price. */
    private int $price;
}

To execute php-class-diagram will print PlantUML script.

$ vendor/bin/php-class-diagram test/fixtures/no-namespace
@startuml class-diagram
  package product as product <<Rectangle>> {
    class product.Price {
      -price : int
    }
    class product.Name {
      -name : string
    }
    class product.Product {
      -name : Name
      -price : Price
      +method1(param1)
    }
  }
  product.Product ..> product.Name
  product.Product ..> product.Price
@enduml

Use PlnatUML to convert the PlantUML script to an image.

PlantUML output image.

Package Diagram

You can visualize package dependencies by creating a package relationship diagram with php-class-diagram.

$ vendor/bin/php-class-diagram --package-diagram test/fixtures/dependency-loops
@startuml package-related-diagram
  package hoge.fuga as ROOT {
    package product as product {
      package attribute as product.attribute {
      }
      package config as product.config {
      }
    }
  }
  product --> product.attribute
  product <-[#red,plain,thickness=4]-> product.config
@enduml

Packages that depend on each other are not desirable. If it finds packages that depend on each other, it will warn you with a thick red line.

PlantUML output image.

PlantUML output image.

Development

Open shell

docker-compose build
docker-compose run --rm php_cli bash

install dependencies

composer install

execute tests

composer test