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

Commit 0cb87c1

Browse files
committed
feat: Refactor sorting and filtering methods in Relationships and Nodes classes for improved functionality
1 parent a4f3552 commit 0cb87c1

File tree

11 files changed

+86
-34
lines changed

11 files changed

+86
-34
lines changed

src/ClassDiagramRenderer/ClassDiagram.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public function addRelationships(Relationship ...$relationships): self
4242

4343
public function render(RenderOptions $options = null): string
4444
{
45-
$this->nodes->sort();
46-
$nodes = $this->nodes->getAllNodes();
47-
$this->relationships->sort();
45+
$nodes = $this->nodes->filter($options)->sort()->getAll();
46+
$relationships = $this->relationships->filter($options)->sort()->getAll();
4847

4948
$output = "classDiagram\n";
5049

@@ -54,7 +53,7 @@ public function render(RenderOptions $options = null): string
5453

5554
$output .= "\n";
5655

57-
foreach ($this->relationships->filter($options)->getAll() as $relationship) {
56+
foreach ($relationships as $relationship) {
5857
$output .= " " . $relationship->render() . "\n";
5958
}
6059

src/ClassDiagramRenderer/ClassDiagramBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function build(string $path): ClassDiagram
2222
{
2323
$classDigagram = new ClassDiagram();
2424

25-
foreach ($this->nodeParser->parse($path)->getAllNodes() as $node) {
25+
foreach ($this->nodeParser->parse($path)->getAll() as $node) {
2626
$classDigagram->addNode($node)->addRelationships(...$node->relationships());
2727
}
2828

src/ClassDiagramRenderer/Node/Node.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ public function nodeName(): string
6969
*/
7070
public function relationships(): array
7171
{
72-
$extends = $this->extends->getAllNodes();
73-
$implements = $this->implements->getAllNodes();
74-
$ownProperties = $this->properties->getAllNodes();
75-
$ownDepends = $this->depends->getAllNodes();
72+
$extends = $this->extends->getAll();
73+
$implements = $this->implements->getAll();
74+
$ownProperties = $this->properties->getAll();
75+
$ownDepends = $this->depends->getAll();
7676

7777
// Collect trait-derived relations (no mutation of own collections)
7878
$traitCompositions = [];
7979
$traitDependencies = [];
8080
$visitedTraits = [];
81-
foreach ($this->traits->getAllNodes() as $traitNode) {
81+
foreach ($this->traits->getAll() as $traitNode) {
8282
$this->collectTraitRelations($traitNode, $visitedTraits, $traitCompositions, $traitDependencies);
8383
}
8484

@@ -119,15 +119,15 @@ private function collectTraitRelations(Node $traitNode, array &$visited, array &
119119
$visited[$traitName] = true;
120120

121121
// Direct compositions and dependencies declared in the trait
122-
foreach ($traitNode->properties->getAllNodes() as $name => $node) {
122+
foreach ($traitNode->properties->getAll() as $name => $node) {
123123
$compositionsOut[$name] = $node;
124124
}
125-
foreach ($traitNode->depends->getAllNodes() as $name => $node) {
125+
foreach ($traitNode->depends->getAll() as $name => $node) {
126126
$dependenciesOut[$name] = $node;
127127
}
128128

129129
// Nested trait uses
130-
foreach ($traitNode->traits->getAllNodes() as $nestedTrait) {
130+
foreach ($traitNode->traits->getAll() as $nestedTrait) {
131131
$this->collectTraitRelations($nestedTrait, $visited, $compositionsOut, $dependenciesOut);
132132
}
133133
}

src/ClassDiagramRenderer/Node/Nodes.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare(strict_types=1);
33

44
namespace Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node;
5+
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\RenderOptions;
56

67
class Nodes
78
{
@@ -31,16 +32,31 @@ public function findByName(string $nodeName): ?Node
3132
return $this->nodes[$nodeName] ?? null;
3233
}
3334

34-
public function sort(): void
35+
public function sort(): self
3536
{
3637
// Keys are node names; sort by key keeps deterministic order
3738
ksort($this->nodes);
39+
40+
return $this;
41+
}
42+
43+
public function filter(RenderOptions $options): self
44+
{
45+
$filtered = new self();
46+
foreach ($this->nodes as $node) {
47+
if (!$options->includeTraits && $node instanceof Trait_) {
48+
continue;
49+
}
50+
$filtered->add($node);
51+
}
52+
53+
return $filtered;
3854
}
3955

4056
/**
4157
* @return Node[]
4258
*/
43-
public function getAllNodes(): array
59+
public function getAll(): array
4460
{
4561
return $this->nodes;
4662
}

src/ClassDiagramRenderer/Node/Relationship/Relationships.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node\Relationship;
55

66
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\RenderOptions;
7+
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node\Trait_;
78

89
class Relationships
910
{
@@ -32,18 +33,23 @@ public function getAll(): array
3233
return $this->relationships;
3334
}
3435

35-
public function sort(): void
36+
public function sort(): self
3637
{
3738
usort($this->relationships, function (Relationship $a, Relationship $b) {
3839
$aKey = $a->from->nodeName() . ' ' . $a->to->nodeName();
3940
$bKey = $b->from->nodeName() . ' ' . $b->to->nodeName();
4041
return strcmp($aKey, $bKey);
4142
});
43+
44+
return $this;
4245
}
4346

4447
public function filter(RenderOptions $options): self
4548
{
4649
$filtered = array_filter($this->relationships, function (Relationship $relationship) use ($options) {
50+
if (!$options->includeTraits && ($relationship->from instanceof Trait_ || $relationship->to instanceof Trait_)) {
51+
return false;
52+
}
4753
if ($relationship instanceof Dependency && !$options->includeDependencies) {
4854
return false;
4955
}
@@ -63,4 +69,3 @@ public function filter(RenderOptions $options): self
6369
return new self(array_values($filtered));
6470
}
6571
}
66-

src/ClassDiagramRenderer/RenderOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ public function __construct(
1010
public bool $includeCompositions,
1111
public bool $includeInheritances,
1212
public bool $includeRealizations,
13+
public bool $includeTraits = false,
1314
) {
1415
}
1516

1617
public static function default(): self
1718
{
18-
return new self(true, true, true, true);
19+
return new self(true, true, true, true, false);
1920
}
2021
}

tests/ClassDiagramRenderer/ClassDiagramBuilderTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ class AuditLogger {
4444
}
4545
class AuditTarget {
4646
}
47-
class RepositoryAwareTrait {
48-
<<trait>>
49-
}
5047
class User {
5148
}
5249
class UserController {
@@ -62,10 +59,6 @@ class UserStatus {
6259
<<enum>>
6360
}
6461
65-
RepositoryAwareTrait *-- AuditLogger: composition
66-
RepositoryAwareTrait ..> AuditTarget: dependency
67-
RepositoryAwareTrait ..> User: dependency
68-
RepositoryAwareTrait *-- UserRepositoryInterface: composition
6962
User *-- UserStatus: composition
7063
AbstractController <|-- UserController: inheritance
7164
UserController *-- UserService: composition
@@ -121,9 +114,6 @@ class AuditLogger {
121114
}
122115
class AuditTarget {
123116
}
124-
class RepositoryAwareTrait {
125-
<<trait>>
126-
}
127117
class User {
128118
}
129119
class UserController {
@@ -139,8 +129,6 @@ class UserStatus {
139129
<<enum>>
140130
}
141131
142-
RepositoryAwareTrait *-- AuditLogger: composition
143-
RepositoryAwareTrait *-- UserRepositoryInterface: composition
144132
User *-- UserStatus: composition
145133
AbstractController <|-- UserController: inheritance
146134
UserController *-- UserService: composition

tests/ClassDiagramRenderer/Node/NodesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testSort(): void
2323

2424
// Sort by name
2525
$nodes->sort();
26-
$sorted = array_values($nodes->getAllNodes());
26+
$sorted = array_values($nodes->getAll());
2727

2828
$this->assertSame('A', $sorted[0]->nodeName());
2929
$this->assertSame('B', $sorted[1]->nodeName());
@@ -52,7 +52,7 @@ public function testAddAndGetAllNodes(): void
5252
$nodes->add($classA);
5353
$nodes->add($classB);
5454

55-
$allNodes = $nodes->getAllNodes();
55+
$allNodes = $nodes->getAll();
5656
$this->assertCount(2, $allNodes);
5757
$this->assertArrayHasKey('ClassA', $allNodes);
5858
$this->assertArrayHasKey('ClassB', $allNodes);
@@ -68,7 +68,7 @@ public function testEmpty(): void
6868
$nodes = Nodes::empty();
6969

7070
$this->assertInstanceOf(Nodes::class, $nodes);
71-
$this->assertCount(0, $nodes->getAllNodes());
71+
$this->assertCount(0, $nodes->getAll());
7272
}
7373

7474
/**
@@ -102,7 +102,7 @@ public function testAddSameNameTwice(): void
102102
$nodes->add($classA1);
103103
$nodes->add($classA2); // Should replace the first one
104104

105-
$allNodes = $nodes->getAllNodes();
105+
$allNodes = $nodes->getAll();
106106
$this->assertCount(1, $allNodes);
107107
$this->assertSame($classA2, $allNodes['ClassA']); // Should be the second instance
108108
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TestProject\Service;
5+
6+
class AuditLogger
7+
{
8+
}
9+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TestProject\Service;
5+
6+
class AuditTarget
7+
{
8+
}
9+

0 commit comments

Comments
 (0)