Skip to content

Commit f7615ec

Browse files
authored
Merge pull request #41 from smeghead/support-pacakge-name-and-class-name-are-equal
support package name and class name are equal. className make alias.
2 parents b2382fb + 21b0bad commit f7615ec

File tree

11 files changed

+49
-36
lines changed

11 files changed

+49
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
### Bug fix
4+
5+
* Fixed. #40 If the namespace name and class name are the same, the class will not be displayed
6+
37
## v0.3.1 (2023-02-24)
48

59
### Features

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,21 @@ To execute `php-class-diagram` will print PlantUML script.
124124
$ vendor/bin/php-class-diagram test/fixtures/no-namespace
125125
@startuml class-diagram
126126
package product as product {
127-
class product.Price {
127+
class "Price" as product_Price {
128128
-price : int
129129
}
130-
class product.Name {
130+
class "Name" as product_Name {
131131
-name : string
132132
}
133-
class product.Product {
133+
class "Product" as product_Product {
134134
-name : Name
135135
-price : Price
136136
+method1(param1)
137137
}
138138
}
139-
product.Product ..> product.Name
140-
product.Product ..> product.Price
139+
product_Product ..> product_Name
140+
product_Product ..> product_Price
141+
product_Product ..> product_Product
141142
@enduml
142143
```
143144

dogfood-model.png

60 Bytes
Loading

dogfood.png

1.17 KB
Loading

output.png

1.13 KB
Loading

src/DiagramElement/ArrowDependency.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ArrowDependency extends Arrow {
88

99
public function toString(PhpClass $toClass): string {
1010
if (strpos($this->getTo()->getName(), '[]') === false) {
11-
return sprintf(' %s %s %s', $this->getFrom()->getLogicalName(), $this->figure, $toClass->getLogicalName());
11+
return sprintf(' %s %s %s', $this->getFrom()->getClassNameAlias(), $this->figure, $toClass->getClassNameAlias());
1212
}
13-
return sprintf(' %s "1" %s "*" %s', $this->getFrom()->getLogicalName(), $this->figure, str_replace('[]', '', $toClass->getLogicalName()));
13+
return sprintf(' %s "1" %s "*" %s', $this->getFrom()->getClassNameAlias(), $this->figure, str_replace('[]', '', $toClass->getClassNameAlias()));
1414
}
1515
}

src/DiagramElement/ArrowInheritance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class ArrowInheritance extends Arrow {
77
protected string $figure = '<|--';
88

99
public function toString(PhpClass $toClass): string {
10-
return sprintf(' %s %s %s', $toClass->getLogicalName(), $this->figure, $this->getFrom()->getLogicalName());
10+
return sprintf(' %s %s %s', $toClass->getClassNameAlias(), $this->figure, $this->getFrom()->getClassNameAlias());
1111
}
1212
}

src/DiagramElement/Entry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function dump($level = 0): array {
3737
// $meta = $this->class->getClassType()->getMeta() === 'Stmt_Interface' ? 'interface' : 'class';
3838
$meta = $this->class->getClassType()->getMetaName();
3939
if ($this->options->classProperties() || $this->options->classMethods()) {
40-
$lines[] = sprintf('%s%s %s {', $indent, $meta, $this->class->getLogicalName());
40+
$lines[] = sprintf('%s%s "%s" as %s {', $indent, $meta, $this->class->getClassType()->getName(), $this->class->getClassNameAlias());
4141
if ($this->options->classProperties()) {
4242
foreach ($this->class->getProperties() as $p) {
4343
$lines[] = sprintf(' %s%s%s : %s', $indent, $this->modifier($p->getAccessModifier()), $p->getName(), $p->getType()->getName());
@@ -53,7 +53,7 @@ public function dump($level = 0): array {
5353
}
5454
$lines[] = sprintf('%s}', $indent);
5555
} else {
56-
$lines[] = sprintf('%s%s %s', $indent, $meta, $this->class->getLogicalName());
56+
$lines[] = sprintf('%s%s "%s" as %s', $indent, $meta, $this->class->getClassType()->getName(), $this->class->getClassNameAlias());
5757
}
5858
return $lines;
5959
}

src/Php/PhpClass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function getLogicalName(): string {
5656
return implode('.', $parts);
5757
}
5858

59+
/**
60+
* return className alias in class-diagram.
61+
* @return string className alias
62+
*/
63+
public function getClassNameAlias(): string {
64+
return str_replace(['.', '[', ']'], '_', $this->getLogicalName());
65+
}
66+
5967
public function getClassType(): PhpType {
6068
$namespace = [];
6169
foreach ($this->full as $stmt) {

test/PackageTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,12 @@ public function testDump(): void {
309309
$expected =<<<EOS
310310
@startuml class-diagram
311311
package product as product {
312-
class product.Product
313-
class product.Price
314-
class product.Name
312+
class "Product" as product_Product
313+
class "Price" as product_Price
314+
class "Name" as product_Name
315315
}
316-
product.Product ..> product.Name
317-
product.Product ..> product.Price
316+
product_Product ..> product_Name
317+
product_Product ..> product_Price
318318
@enduml
319319
EOS;
320320
$this->assertSame($expected, implode(PHP_EOL, $rel->dump()), 'output PlantUML script.');
@@ -334,14 +334,14 @@ public function testDump2(): void {
334334
$expected =<<<EOS
335335
@startuml class-diagram
336336
package product as product {
337-
class product.Product
338-
class product.Price
337+
class "Product" as product_Product
338+
class "Price" as product_Price
339339
package utility as product.utility {
340-
class product.Name
340+
class "Name" as product_Name
341341
}
342342
}
343-
product.Product ..> product.Name
344-
product.Product ..> product.Price
343+
product_Product ..> product_Name
344+
product_Product ..> product_Price
345345
@enduml
346346
EOS;
347347
$this->assertSame($expected, implode(PHP_EOL, $rel->dump()), 'output PlantUML script.');
@@ -360,7 +360,7 @@ public function testDump3(): void {
360360
$expected =<<<EOS
361361
@startuml class-diagram
362362
package product as product {
363-
interface product.Interface_
363+
interface "Interface_" as product_Interface_
364364
}
365365
@enduml
366366
EOS;
@@ -379,7 +379,7 @@ public function testDump4(): void {
379379
$expected =<<<EOS
380380
@startuml class-diagram
381381
package product as product {
382-
interface product.Interface_ {
382+
interface "Interface_" as product_Interface_ {
383383
-name : string
384384
}
385385
}
@@ -399,7 +399,7 @@ public function testDump5(): void {
399399
$expected =<<<EOS
400400
@startuml class-diagram
401401
package product as product {
402-
interface product.Interface_ {
402+
interface "Interface_" as product_Interface_ {
403403
-method1(param1)
404404
}
405405
}
@@ -420,14 +420,14 @@ public function testDump6(): void {
420420
$expected =<<<EOS
421421
@startuml class-diagram
422422
package product as product {
423-
interface product.Interface_ {
423+
interface "Interface_" as product_Interface_ {
424424
-method1(param1)
425425
}
426-
class product.Implement_ {
426+
class "Implement_" as product_Implement_ {
427427
-method1(param1)
428428
}
429429
}
430-
product.Interface_ <|-- product.Implement_
430+
product_Interface_ <|-- product_Implement_
431431
@enduml
432432
EOS;
433433
$this->assertSame($expected, implode(PHP_EOL, $rel->dump()), 'output PlantUML script.');
@@ -447,11 +447,11 @@ public function testDump7(): void {
447447
$expected =<<<EOS
448448
@startuml class-diagram
449449
package product as product {
450-
class product.Product
451-
class product.Price
452-
class product.Name
450+
class "Product" as product_Product
451+
class "Price" as product_Price
452+
class "Name" as product_Name
453453
}
454-
product.Product ..> product.Name
454+
product_Product ..> product_Name
455455
@enduml
456456
EOS;
457457
$this->assertSame($expected, implode(PHP_EOL, $rel->dump()), 'output PlantUML script.');

0 commit comments

Comments
 (0)