From 5946d0ca00cc7e545d72db0baafb33e225b17ec0 Mon Sep 17 00:00:00 2001 From: smeghead Date: Tue, 17 Dec 2024 00:34:48 +0900 Subject: [PATCH] feat: Highlight the target class when output with rel-target --- src/Config/Options.php | 42 ++++++++---------- src/DiagramElement/ClassIdentifier.php | 18 ++++++-- src/DiagramElement/RelationsFilter.php | 12 ++---- test/DiagramElement/ClassIdentifierTest.php | 24 +++++++++++ test/OptionsTest.php | 47 +++++++++++++++++++++ 5 files changed, 107 insertions(+), 36 deletions(-) diff --git a/src/Config/Options.php b/src/Config/Options.php index 1b8977a..4775714 100644 --- a/src/Config/Options.php +++ b/src/Config/Options.php @@ -168,42 +168,36 @@ public function hidePrivateMethods(): bool } /** - * @return array + * @return list */ - public function fromClass(): array + public function relTargetsFrom(): array { - if (!isset($this->opt['rel-target-from'])) { - return []; + $targets = []; + if (isset($this->opt['rel-target-from'])) { + $targets = [...$targets, ...explode(',', $this->opt['rel-target-from'])]; } - - return explode(',', $this->opt['rel-target-from']); - } - - /** - * @return array - */ - public function toClass(): array - { - if (!isset($this->opt['rel-target-to'])) { - return []; + if (isset($this->opt['rel-target'])) { + $targets = [...$targets, ...explode(',', $this->opt['rel-target'])]; } - - return explode(',', $this->opt['rel-target-to']); + return array_values($targets); } /** - * @return array + * @return list */ - public function targetClass(): array + public function relTargetsTo(): array { - if (!isset($this->opt['rel-target'])) { - return []; + $targets = []; + if (isset($this->opt['rel-target-to'])) { + $targets = [...$targets, ...explode(',', $this->opt['rel-target-to'])]; } - - return explode(',', $this->opt['rel-target']); + if (isset($this->opt['rel-target'])) { + $targets = [...$targets, ...explode(',', $this->opt['rel-target'])]; + } + return array_values($targets); } - public function depth(): int + public function relTargetsDepth(): int { return (int) ($this->opt['rel-target-depth'] ?? PHP_INT_MAX); } diff --git a/src/DiagramElement/ClassIdentifier.php b/src/DiagramElement/ClassIdentifier.php index 6d64e8f..553d0ac 100644 --- a/src/DiagramElement/ClassIdentifier.php +++ b/src/DiagramElement/ClassIdentifier.php @@ -11,17 +11,19 @@ public function __construct(private Options $options, private string $directory, { } - public function getIdentifier(): string { + public function getIdentifier(): string + { $meta = $this->entry->getClass()->getClassType()->getMetaName(); $classSummary = ($this->options->classNameSummary() ? $this->entry->getClass()->getDescription() : ''); return sprintf( - '%s "%s" as %s%s', + '%s "%s" as %s%s%s', $meta, $this->entry->getClass()->getClassType()->getName() . (empty($classSummary) ? '' : sprintf("\\n%s", $classSummary)), $this->entry->getClass()->getClassNameAlias(), - $this->getLinkExpression($meta) + $this->getLinkExpression($meta), + $this->getRelTargetStyle() ); } @@ -41,4 +43,14 @@ private function getLinkExpression(string $meta): string ucfirst($meta) ); } + + private function getRelTargetStyle(): string + { + $targets = [ + ...$this->options->relTargetsFrom(), + ...$this->options->relTargetsTo(), + ]; + return array_search($this->entry->getClass()->getClassType()->getName(), $targets) !== false + ? ' #FFF0F5;line:740125;text:740125' : ''; + } } \ No newline at end of file diff --git a/src/DiagramElement/RelationsFilter.php b/src/DiagramElement/RelationsFilter.php index b069fb1..fca66c0 100644 --- a/src/DiagramElement/RelationsFilter.php +++ b/src/DiagramElement/RelationsFilter.php @@ -4,7 +4,6 @@ use InvalidArgumentException; use Smeghead\PhpClassDiagram\Config\Options; -use Smeghead\PhpClassDiagram\Enums\DependenciesDirection; use function preg_match; class RelationsFilter { @@ -27,15 +26,10 @@ public function __construct(private Options $options) public function filterRelations(array $relation_expressions): array { $output = []; - $fromClasses = $this->options->fromClass(); - $toClasses = $this->options->toClass(); + $fromClasses = $this->options->relTargetsFrom(); + $toClasses = $this->options->relTargetsTo(); - if ([] !== $this->options->targetClass()) { - $fromClasses = $this->options->targetClass(); - $toClasses = $this->options->targetClass(); - } - - $this->maxDepth = $this->options->depth() - 1; + $this->maxDepth = $this->options->relTargetsDepth() - 1; $this->relationExpressions = $relation_expressions; if ([] === $fromClasses && [] === $toClasses) { diff --git a/test/DiagramElement/ClassIdentifierTest.php b/test/DiagramElement/ClassIdentifierTest.php index e73abc5..968d46a 100644 --- a/test/DiagramElement/ClassIdentifierTest.php +++ b/test/DiagramElement/ClassIdentifierTest.php @@ -53,6 +53,30 @@ public function testEnum(): void $this->assertSame('enum "Suit\nスート" as Suit', $sut->getIdentifier()); } + public function testClassRelTarget(): void + { + $directory = sprintf('%s/namespace', $this->fixtureDir); + + $options = new Options(['rel-target' => 'Product,Name']); + $entry = $this->getTargetEntry('product/Product.php', $directory, $options); + + $sut = new ClassIdentifier($options, 'product', $entry); + + $this->assertSame('class "Product" as product_Product #FFF0F5;line:740125;text:740125', $sut->getIdentifier()); + } + + public function testClassRelTargetNotMatch(): void + { + $directory = sprintf('%s/namespace', $this->fixtureDir); + + $options = new Options(['rel-target' => 'ProductXX,Name']); + $entry = $this->getTargetEntry('product/Product.php', $directory, $options); + + $sut = new ClassIdentifier($options, 'product', $entry); + + $this->assertSame('class "Product" as product_Product', $sut->getIdentifier()); + } + private function getTargetEntry(string $path, string $directory, Options $options): Entry { $filename = sprintf('%s/%s', $directory, $path); diff --git a/test/OptionsTest.php b/test/OptionsTest.php index f35cae3..36c13b3 100644 --- a/test/OptionsTest.php +++ b/test/OptionsTest.php @@ -292,4 +292,51 @@ public function testHidePrivateMethodsTrue(): void $this->assertFalse($options->hidePrivateProperties(), 'hide-private-property is false.'); $this->assertTrue($options->hidePrivateMethods(), 'hide-private-methods is true.'); } + + public function testRelTarget(): void + { + $opt = [ + 'rel-target' => 'Product,Name', + ]; + + $options = new Options($opt); + + $this->assertSame(['Product', 'Name'], $options->relTargetsFrom()); + $this->assertSame(['Product', 'Name'], $options->relTargetsTo()); + } + + public function testRelTargetFrom(): void + { + $opt = [ + 'rel-target-from' => 'Product,Name', + ]; + + $options = new Options($opt); + + $this->assertSame(['Product', 'Name'], $options->relTargetsFrom()); + $this->assertSame([], $options->relTargetsTo()); + } + + public function testRelTargetTo(): void + { + $opt = [ + 'rel-target-to' => 'Product,Name', + ]; + + $options = new Options($opt); + + $this->assertSame([], $options->relTargetsFrom()); + $this->assertSame(['Product', 'Name'], $options->relTargetsTo()); + } + + public function testRelTargetDepth(): void + { + $opt = [ + 'rel-target-depth' => '1', + ]; + + $options = new Options($opt); + + $this->assertSame(1, $options->relTargetsDepth()); + } }