Skip to content

Commit b5521c8

Browse files
committed
refactor: extract Class identifier logic.
1 parent 2d6a775 commit b5521c8

File tree

3 files changed

+113
-28
lines changed

3 files changed

+113
-28
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smeghead\PhpClassDiagram\DiagramElement;
6+
7+
use Smeghead\PhpClassDiagram\Config\Options;
8+
9+
final class ClassIdentifier {
10+
public function __construct(private Options $options, private string $directory, private Entry $entry)
11+
{
12+
}
13+
14+
public function getIdentifier(): string {
15+
$meta = $this->entry->getClass()->getClassType()->getMetaName();
16+
$classSummary = ($this->options->classNameSummary()
17+
? $this->entry->getClass()->getDescription()
18+
: '');
19+
return sprintf(
20+
'%s "%s" as %s%s',
21+
$meta,
22+
$this->entry->getClass()->getClassType()->getName() . (empty($classSummary) ? '' : sprintf("\\n<b>%s</b>", $classSummary)),
23+
$this->entry->getClass()->getClassNameAlias(),
24+
$this->getLinkExpression($meta)
25+
);
26+
}
27+
28+
private function getLinkExpression(string $meta): string
29+
{
30+
if (empty($this->options->svgTopurl())) {
31+
return '';
32+
}
33+
$path = sprintf(
34+
'/%s%s.php',
35+
empty($this->directory) ? '' : sprintf('%s/', $this->directory),
36+
$this->entry->getClass()->getClassType()->getName());
37+
return sprintf(
38+
' [[%s %s %s]]',
39+
$path,
40+
$this->entry->getClass()->getClassType()->getName(),
41+
ucfirst($meta)
42+
);
43+
}
44+
}

src/DiagramElement/Entry.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,9 @@ public function dump(int $level = 0): array
4848
{
4949
$indent = str_repeat(' ', $level);
5050
$lines = [];
51-
$meta = $this->class->getClassType()->getMetaName();
52-
$classSummary = ($this->options->classNameSummary()
53-
? $this->class->getDescription()
54-
: '');
55-
$classIdentifier = sprintf(
56-
'%s "%s" as %s%s',
57-
$meta,
58-
$this->class->getClassType()->getName() . (empty($classSummary) ? '' : sprintf("\\n<b>%s</b>", $classSummary)),
59-
$this->class->getClassNameAlias(),
60-
$this->getLinkExpression($meta)
61-
);
51+
$identifier = new ClassIdentifier($this->options, $this->directory, $this);
52+
$classIdentifier = $identifier->getIdentifier();
53+
6254
if ($this->options->classProperties() || $this->options->classMethods()) {
6355
$lines[] = sprintf('%s%s {', $indent, $classIdentifier);
6456
if ($this->options->classProperties()) {
@@ -192,21 +184,4 @@ public function getUsingArrows(): array
192184
}
193185
return $arrows;
194186
}
195-
196-
private function getLinkExpression(string $meta): string
197-
{
198-
if (empty($this->options->svgTopurl())) {
199-
return '';
200-
}
201-
$path = sprintf(
202-
'/%s%s.php',
203-
empty($this->directory) ? '' : sprintf('%s/', $this->directory),
204-
$this->class->getClassType()->getName());
205-
return sprintf(
206-
' [[%s %s %s]]',
207-
$path,
208-
$this->class->getClassType()->getName(),
209-
ucfirst($meta)
210-
);
211-
}
212187
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Smeghead\PhpClassDiagram\Config\Options;
7+
use Smeghead\PhpClassDiagram\DiagramElement\ClassIdentifier;
8+
use Smeghead\PhpClassDiagram\DiagramElement\Entry;
9+
use Smeghead\PhpClassDiagram\Php\PhpReader;
10+
11+
final class ClassIdentifierTest extends TestCase
12+
{
13+
private string $fixtureDir;
14+
15+
public function setUp(): void
16+
{
17+
$this->fixtureDir = realpath(sprintf('%s/../fixtures', __DIR__));
18+
}
19+
20+
public function testClass(): void
21+
{
22+
$directory = sprintf('%s/namespace', $this->fixtureDir);
23+
24+
$options = new Options([]);
25+
$entry = $this->getTargetEntry('product/Product.php', $directory, $options);
26+
27+
$sut = new ClassIdentifier($options, 'product', $entry);
28+
29+
$this->assertSame('class "Product" as product_Product', $sut->getIdentifier());
30+
}
31+
32+
public function testInterface(): void
33+
{
34+
$directory = sprintf('%s/interface', $this->fixtureDir);
35+
36+
$options = new Options([]);
37+
$entry = $this->getTargetEntry('product/Interface_.php', $directory, $options);
38+
39+
$sut = new ClassIdentifier($options, 'product', $entry);
40+
41+
$this->assertSame('interface "Interface_" as product_Interface_', $sut->getIdentifier());
42+
}
43+
44+
public function testEnum(): void
45+
{
46+
$directory = sprintf('%s/enum', $this->fixtureDir);
47+
48+
$options = new Options([]);
49+
$entry = $this->getTargetEntry('TestEnum.php', $directory, $options);
50+
51+
$sut = new ClassIdentifier($options, '', $entry);
52+
53+
$this->assertSame('enum "Suit\n<b>スート</b>" as Suit', $sut->getIdentifier());
54+
}
55+
56+
private function getTargetEntry(string $path, string $directory, Options $options): Entry
57+
{
58+
$filename = sprintf('%s/%s', $directory, $path);
59+
$classes = PhpReader::parseFile($directory, $filename, $options);
60+
if (count($classes) === 0) {
61+
throw new \Exception('class not found.');
62+
}
63+
return new Entry(dirname($path), $classes[0]->getInfo(), $options);
64+
}
65+
66+
}

0 commit comments

Comments
 (0)