smeghead/php-class-diagram

一个命令行工具,它解析PHP源目录并输出PlantUML脚本。

v1.4.2 2024-09-23 14:07 UTC

README

一个命令行工具,它解析PHP源目录并生成PlantUML类图脚本作为输出。

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

功能

  • ♻️ 从源代码生成类图有助于持续的设计改进。
  • 🔖 它生成专注于命名空间和关系的表达性类图。
  • 🌐 图表是交互式超媒体工具,允许您点击类框直接导航到源代码,在视觉和交互方面增强理解。
  • 🔧 这个简单的命令行工具易于使用。
  • 💡 此外,它还可以生成一个包关系图,以可视化对外部命名空间的依赖关系。

什么是PlantUML

PlantUML - 维基百科 PlantUML是一个开源工具,允许用户使用纯文本语言创建图表。除了各种UML图表外,PlantUML还支持各种其他软件开发相关格式(如Archimate、块图、BPMN、C4、计算机网络图、ERD、甘特图、思维导图和WBD),以及JSON和YAML文件的可视化。

超媒体特性

此工具的一个突出特点是能够生成带有可点击链接的类图,这些链接直接导航到这些类的源代码。这把图表变成了交互式超媒体工具,在视觉和交互方面增强了源代码的理解。

要启用此功能,以SVG格式生成图表,并使用--svg-topurl选项指定链接的基本URL

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

要嵌入SVG图表并保留可点击链接,请使用embedobject标签而不是img标签。以下是一个示例

<html lang="en">
<head>
    <title>PHP Class Diagram</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        embed {
            max-width: 100%;
            max-height: 200%;
            width: auto;
            height: auto;
        }
    </style>
</head>
<body>
    <embed type="image/svg+xml" src="php-class-diagram.svg"/>
</body>
</html>

这确保了SVG图像是响应式的,并且超链接保持功能。

超媒体示例

自用

php-class-diagram类图(链接SVG文件)

没有字段和方法的php-class-diagram类图

如果您的分析主要关注类之间的关系,则可能更适合使用简单的符号。

dogfood class diagram image.

php-class-diagram包相关图

可视化包关系可以发现关键的设计问题。

dogfood package related diagram image.

安装

从DockerHub

您可以从以下URL使用包含php-class-diagram和PlantUML的Docker镜像。

从Composer

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

现在,您可以执行./vendor/bin/php-class-diagram。例如,尝试显示帮助信息。

$ 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 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                 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.

如何执行

类图

如果目录test/fixtures/no-namespace中有三个PHP源文件,每个文件都有一个TYPE注释,

  • PHP源文件。
└─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;
}

执行php-class-diagram将输出PlantUML脚本。

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

使用PlantUML将PlantUML脚本转换为图像。

PlantUML output image.

选项header

您可以为PlantUML头部指定字符串。

$ vendor/bin/php-class-diagram \
    --header='title "This is the class diagram"' \
    path/to/src

选项include

您可以为处理指定目标文件的模式。

$ vendor/bin/php-class-diagram \
    --include='*.php' \
    --include='*.php4' \
    path/to/src

选项exclude

您可以为排除处理的文件指定模式。

$ vendor/bin/php-class-diagram \
    --exclude='test' \
    --exclude='*Exception.php' \
    path/to/src

包图

您可以使用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

相互依赖的包是不理想的。如果工具检测到此类依赖关系,则将发出带有粗红色线的警告。

PlantUML output image.

PlantUML output image.

分区图

如果您正在使用PHP 8.1中引入的枚举(Enum)功能,您可以生成分部图。将程序中使用的分部可视化对于研究和设计都是有帮助的。

$ bin/php-class-diagram --division-diagram test/fixtures/enum/ 
@startuml division-diagram
  card Suit #ccffcc [
    Suit
    <b>スート</b>
    ====
    Hearts
    <b>ハート</b>
    ----
    Diamonds
    <b>ダイヤ</b>
    ----
    Clubs
    <b>クローバー</b>
    ----
    Spades
    <b>スペード</b>
  ]
  package Sub as Sub {
    card Status #ffcccc [
      Status
      <b>ゲームのステータス</b>
      ====
      Player
      <b>プレイヤーのターン</b>
      ----
      Computer
      <b>コンピュータのターン</b>
      ----
      GameSet
      <b>ゲーム終了</b>
    ]
    card MyExceptionCase #ccccff [
      MyExceptionCase
      ====
      InvalidMethod
      ----
      InvalidProperty
      ----
      Timeout
    ]
    card Size #ccffff [
      Size
      ====
      Small
      ----
      Medium
      ----
      Large
    ]
  }
@enduml

PlantUML output image.

GitHub Actions支持

以下仓库提供在GitHub Actions工作流程中使用php-class-diagram生成类图的actions。

smeghead/php-class-diagram-gh-action

开发

打开shell

docker compose build
docker compose run --rm php_cli bash

安装依赖项

composer install

执行测试

composer test

贡献

欢迎提交问题和Pull Requests!