Skip to content

Commit

Permalink
feat: Highlight the target class when output with rel-target
Browse files Browse the repository at this point in the history
  • Loading branch information
smeghead committed Dec 16, 2024
1 parent b5521c8 commit 5946d0c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 36 deletions.
42 changes: 18 additions & 24 deletions src/Config/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,36 @@ public function hidePrivateMethods(): bool
}

/**
* @return array<string>
* @return list<string>
*/
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<string>
*/
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<string>
* @return list<string>
*/
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);
}
Expand Down
18 changes: 15 additions & 3 deletions src/DiagramElement/ClassIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<b>%s</b>", $classSummary)),
$this->entry->getClass()->getClassNameAlias(),
$this->getLinkExpression($meta)
$this->getLinkExpression($meta),
$this->getRelTargetStyle()
);
}

Expand All @@ -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' : '';
}
}
12 changes: 3 additions & 9 deletions src/DiagramElement/RelationsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use InvalidArgumentException;
use Smeghead\PhpClassDiagram\Config\Options;
use Smeghead\PhpClassDiagram\Enums\DependenciesDirection;
use function preg_match;

class RelationsFilter {
Expand All @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions test/DiagramElement/ClassIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ public function testEnum(): void
$this->assertSame('enum "Suit\n<b>スート</b>" 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);
Expand Down
47 changes: 47 additions & 0 deletions test/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 5946d0c

Please sign in to comment.