iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆

[PHP] I created a tool to automatically generate Mermaid.js class diagrams from code

に公開

Overview

I've created a tool that automatically generates Mermaid.js class diagrams from PHP code.
https://github.com/tasuku43/php-mermaid-class-diagram

I've long been a fan of PlantUML, but lately, with support increasing across various services, I think many people might be considering "converting from PlantUML to Mermaid."
I'm happy that Mermaid can now be used on article-sharing sites like Qiita and Zenn, and above all, the fact that it's now supported on GitHub means it can be used for communication in PRs, which is fantastic.

Currently, it supports rendering relationships such as Inheritance, Realization, and Composition, while omitting internal class details (properties, methods, etc.).
Personally, my motivation was to "quickly visualize high-level class relationships," so I'm not planning on expanding features at the moment. However, if there's demand and issues are raised, I'll consider addressing them.

Installation

Since it's published on Packagist, you can install it with the following command:

composer require --dev tasuku43/mermaid-class-diagram

Usage

Here is an example of the command execution results when used in a simple project like the one below.

$ tree
.
├── composer.json
├── composer.lock
├── src
│   ├── SomeAbstractClass.php
│   ├── SomeClassA.php
│   ├── SomeClassB.php
│   ├── SomeClassC.php
│   └── SomeInterface.php
└── vendor
class SomeClassA extends SomeAbstractClass
{
    private SomeClassB $someClassB;

    public function __construct(private SomeClassC $someClassC)
    {
    }
}

class SomeClassB
{
}

class SomeClassC
{
}

abstract class SomeAbstractClass implements SomeInterface
{
}

interface SomeInterface
{
}

Executing by Specifying a Directory

All files under the directory will be targeted.

$ vendor/bin/mermaid-class-diagram generate --path src
classDiagram
    class SomeClassC {
    }
    class SomeClassB {
    }
    class SomeAbstractClass {
        <<abstruct>>
    }
    class SomeClassA {
    }
    class SomeInterface {
        <<interface>>
    }

    SomeAbstractClass ..|> SomeInterface: realization
    SomeClassA --|> SomeAbstractClass: inheritance
    SomeClassA *-- SomeClassB: composition
    SomeClassA *-- SomeClassC: composition

Executing by Specifying a Specific File

It is also possible to specify a single file. In this case, there will be less information about inherited classes and such. (Though, in the current implementation, it only really distinguishes between an Abstract class and an Interface, so there might not be much of a difference.)

$ vendor/bin/mermaid-class-diagram generate --path src/SomeClassA.php
classDiagram
    class SomeClassA {
    }

    SomeClassA --|> SomeAbstractClass: inheritance
    SomeClassA *-- SomeClassB: composition
    SomeClassA *-- SomeClassC: composition

Discussion