Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 1156a75

Browse files
committed
feat: Update architecture documentation to include Trait_ support and rendering modes
1 parent 05f5f45 commit 1156a75

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

ARCHITECTURE.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,32 @@
1212
- Key classes:
1313
- `ClassDiagram`: Aggregates nodes and relationships; renders Mermaid text.
1414
- `ClassDiagramBuilder`: Facade to parse and assemble nodes/relationships.
15-
- `Node/*`: Type representations (`Class_`, `AbstractClass_`, `Interface_`, `Enum_`) and the common abstract `Node`, plus the `Nodes` collection.
16-
- `Node/Relationship/*`: Relationship representations (`Inheritance`, `Realization`, `Composition`, `Dependency`).
15+
- `Node/*`: Type representations (`Class_`, `AbstractClass_`, `Interface_`, `Enum_`, `Trait_`) with common abstract `Node`, and the `Nodes` collection.
16+
- `Node/Relationship/*`: Relationship representations (`Inheritance`, `Realization`, `Composition`, `Dependency`, `TraitUsage`) and the `Relationships` collection.
1717
- `Node/Connector/*`: Extract relationship information from the AST and connect `Node`s on `Nodes`.
1818
- `Node/NodeParser`: Uses Finder to collect files, PHP-Parser to build AST, and Connectors to extract relationships.
19+
- `TraitRenderMode`: Rendering strategy for traits (WithTraits or Flatten).
20+
- `ClassDiagramDumper`: Debug utility to dump YAML-like structure of nodes/relationships (via reflection).
1921
- `Console/Command/GenerateCommand.php`: Symfony Console command (`generate --path`).
2022
- `mermaid-class-diagram`: Executable binary that boots the Console app.
2123
- `tests/`: PHPUnit tests and fixtures (`tests/data/Project/*`).
2224

2325
## Domain Model
2426
### Nodes (types)
25-
- `Node` (abstract)
27+
- `Node` (abstract)
2628
- Minimal representation of a type with `$name`.
2729
- Holds source collections for relationships:
2830
- `$extends`, `$implements`, `$properties` (composition), `$depends` (dependency)
2931
- `relationships()`: Builds an array of `Relationship` objects from the above collections.
3032
- De-duplicates and filters: removes conflicts (e.g., dependency also present as composition/inheritance/realization) and self references.
3133
- `sortNodes(array &$nodes)`: Sorts by node name (ascending).
3234
- Concrete types
33-
- `Class_`, `AbstractClass_`, `Interface_`, `Enum_`
34-
- `render()`: Outputs Mermaid `class` syntax; stereotypes `<<abstract>>`, `<<interface>>`, `<<enum>>` when applicable.
35-
- `Nodes`
36-
- Dictionary-like collection keyed by node name.
37-
- Provides `add()`, `findByName()`, `getAllNodes()`.
35+
- `Class_`, `AbstractClass_`, `Interface_`, `Enum_`, `Trait_`
36+
- `render()`: Outputs Mermaid `class` syntax; stereotypes `<<abstract>>`, `<<interface>>`, `<<enum>>`, `<<trait>>` when applicable.
37+
- `Nodes`
38+
- Dictionary-like collection keyed by node name.
39+
- Provides `add()`, `findByName()`, `getAll()` and `sort()`.
40+
- `optimize(RenderOptions $options)`: view-time pruning (e.g., hide trait nodes in Flatten mode). Internally uses a private filter.
3841

3942
### Relationships
4043
- `Relationship` (abstract): Holds `from` and `to`; `render()` outputs Mermaid edges.
@@ -53,7 +56,7 @@
5356
- Input: `--path` may be a directory (glob `*.php`) or a single file (`Symfony\Component\Finder\Finder`).
5457
- Preparation: Build AST with `PhpParser`; run `NameResolver` and `ParentConnectingVisitor`.
5558
- Class-like detection:
56-
- Gather `Stmt\ClassLike` (Class/Interface/Enum) and also `Stmt\Trait_` nodes, but only Class/Interface/Enum become `Node`s.
59+
- Gather `Stmt\ClassLike` (Class/Interface/Enum) and also `Stmt\Trait_` nodes; Class, Interface, Enum, and Trait all become `Node`s.
5760
- If no valid class-like found in a file, skip via `CannnotParseToClassLikeException`.
5861
- Two-phase processing:
5962
- Phase 1: Create `Node` (`Class_`/`AbstractClass_`/`Interface_`/`Enum_`) for each valid class-like and add to `Nodes`.
@@ -72,10 +75,13 @@
7275
- Method return types of `Name` are dependencies.
7376
- `new` expressions (`Expr\New_`) add dependencies.
7477
- Filters out `self` and de-duplicates.
78+
- `TraitUsageConnector`
79+
- Records `use TraitName;` for class-like nodes (classes and traits).
80+
- Enables render-time merging/flattening of trait-derived associations into the using node.
7581

7682
## Rendering
7783
- `ClassDiagram::render()`
78-
- Sorts nodes by name; sorts relationships by `from to`.
84+
- Applies `Nodes::optimize($options)->sort()` and `Relationships::optimize($options)->sort()`.
7985
- Emits nodes first, then a blank line, then relationships.
8086
- Header line `classDiagram`; each subsequent line is indented by 4 spaces.
8187
- Examples
@@ -88,6 +94,30 @@
8894
- `includeCompositions`: show/hide `Composition` edges (properties/constructor promotion/FQCN properties)
8995
- `includeInheritances`: show/hide `Inheritance` edges (`extends`)
9096
- `includeRealizations`: show/hide `Realization` edges (`implements`)
97+
- `traitRenderMode` (`TraitRenderMode`): trait rendering policy (default Flatten).
98+
99+
#### Trait rendering modes
100+
- `WithTraits`:
101+
- Show trait nodes and `use` edges.
102+
- Keep trait-origin composition/dependency edges attached to the trait.
103+
- Suppress duplicate class-level composition/dependency when already provided by a used trait.
104+
- `Flatten`:
105+
- Hide trait nodes and `use` edges.
106+
- Reassign trait-origin composition/dependency edges to the using classes.
107+
- Transitive flattening: handles trait→trait→class chains (reassign to final class users), with deduplication.
108+
- Apply `include*` filters after optimization.
109+
110+
### Relationships optimization
111+
- `Relationships::optimize(RenderOptions $options)` performs view-time transformations before rendering:
112+
- Delegates to private implementations per mode:
113+
- `optimizeWithTraitsInternal()`: keep traits/`use`, suppress class-level duplicates for comp/dep.
114+
- `optimizeFlattenInternal()`: hide traits/`use`, reassign trait-origin edges to class users (including transitive chains), deduplicate.
115+
- Applies include flags via private `filterByOptions()`.
116+
117+
### Debug dumper
118+
- `ClassDiagramDumper::toYaml(RenderOptions $options = null): string`
119+
- Uses reflection to access the diagram’s internals and emits a YAML-like structure for debugging.
120+
- Mirrors `render()`’s optimize/sort policy so the dump matches what would be rendered under the same options.
91121

92122
## CLI
93123
- Entry: `mermaid-class-diagram`
@@ -104,7 +134,7 @@
104134
- `symfony/finder` (^7.0): File discovery.
105135
- `symfony/console` (^7.0): CLI framework.
106136
- Dev
107-
- `phpunit/phpunit` (^9.5): Unit tests.
137+
- `phpunit/phpunit`: Unit tests.
108138

109139
## Notes and Trade-offs
110140
- Type resolution simplifications
@@ -114,7 +144,9 @@
114144
- Types referenced but not defined in the scan scope are synthesized as minimal `Node`s so relationships still render.
115145
- This may produce nodes in the output with empty bodies.
116146
- Traits
117-
- Traits are detected in AST collection but not rendered as nodes; only Class/Interface/Enum are supported.
147+
- Participates fully in AST analysis and can be rendered as nodes depending on `TraitRenderMode`.
148+
- WithTraits: show trait nodes and `use` edges, suppress duplicate class-level comp/dep provided by traits.
149+
- Flatten: hide trait nodes and `use` edges; reassign trait-origin comp/dep to using classes, including transitive trait chains; deduplicate.
118150
- Safety
119151
- Never executes target PHP; analysis is static and AST-based.
120152
- CLI does not currently validate `--path` beyond being required.
@@ -125,6 +157,7 @@
125157
- Compare `ClassDiagram`/`ClassDiagramBuilder` output against precise expected strings.
126158
- Fixtures
127159
- `tests/data/Project/*` provides a small pseudo-app to cover common patterns.
160+
- `tests/data/TraitChain/*` covers trait→trait chains and flattening behavior.
128161

129162
## Extensibility
130163
- New relationships

0 commit comments

Comments
 (0)