-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Filter relations from and to class #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be18fab
feat: Filter relations from and to class
jose-ba 536b136
refactor: Rename options
jose-ba ec47db9
feat: --rel-target option
jose-ba 13dbde7
fix: Improve class name filtering
jose-ba 0f7ccd0
fix: Fix errors detected by smeghead
jose-ba b0ca023
fix: Fix errors detected by smeghead
jose-ba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Smeghead\PhpClassDiagram\DiagramElement; | ||
|
||
use InvalidArgumentException; | ||
use Smeghead\PhpClassDiagram\Config\Options; | ||
use Smeghead\PhpClassDiagram\Enums\DependenciesDirection; | ||
use function preg_match; | ||
|
||
class RelationsFilter { | ||
|
||
private int $maxDepth; | ||
/** | ||
* @var string[] | ||
*/ | ||
private array $relationExpressions; | ||
private bool $removeUnlinked = false; | ||
|
||
public function __construct(private Options $options) | ||
{ | ||
} | ||
|
||
/** | ||
* @param list<string> $relation_expressions | ||
* @return list<string> | ||
*/ | ||
public function filterRelations(array $relation_expressions): array | ||
{ | ||
$output = []; | ||
$fromClasses = $this->options->fromClass(); | ||
$toClasses = $this->options->toClass(); | ||
|
||
if ([] !== $this->options->targetClass()) { | ||
$fromClasses = $this->options->targetClass(); | ||
$toClasses = $this->options->targetClass(); | ||
} | ||
|
||
$this->maxDepth = $this->options->depth() - 1; | ||
$this->relationExpressions = $relation_expressions; | ||
|
||
if ([] === $fromClasses && [] === $toClasses) { | ||
return $relation_expressions; | ||
} | ||
|
||
if ([] !== $fromClasses) { | ||
$output = array_merge($output, $this->filterClasses($fromClasses, 'out')); | ||
$this->removeUnlinked = true; | ||
} | ||
|
||
if ([] !== $toClasses) { | ||
$output = array_merge($output, $this->filterClasses($toClasses, 'in')); | ||
$this->removeUnlinked = true; | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* @param list<string> $relation_expressions | ||
* @return list<string> | ||
*/ | ||
public function addRemoveUnlinkedDirective(array $relation_expressions): array | ||
{ | ||
if ($this->removeUnlinked) { | ||
$relation_expressions[] = ' remove @unlinked'; | ||
} | ||
return $relation_expressions; | ||
} | ||
|
||
/** | ||
* @param array<string> $filteredClasses | ||
* @return array<string> | ||
*/ | ||
public function filterClasses(array $filteredClasses, string $direction): array | ||
{ | ||
$currentDepth = 0; | ||
/** @var array<string> $matches */ | ||
$matches = []; | ||
do { | ||
$oldMatches = $matches; | ||
foreach ($matches as $match) { | ||
$parts = explode(' ', trim($match)); | ||
$filteredClasses[] = $direction === 'out' ? | ||
end($parts) : | ||
array_shift($parts) | ||
; | ||
} | ||
$matches = array_filter($this->relationExpressions, function ($line) use ($filteredClasses, $direction) { | ||
$line = str_replace(['"1" ', '"*" '], '', $line); | ||
$line = trim($line); | ||
foreach ($filteredClasses as $filteredClass) { | ||
if (1 === preg_match($this->getFilteringRegex($filteredClass, $direction), $line)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
$matches = array_unique($matches); | ||
$filteredClasses = array_unique($filteredClasses); | ||
} while (++$currentDepth <= $this->maxDepth && count(array_diff($matches, $oldMatches)) > 0); | ||
|
||
return $matches; | ||
} | ||
|
||
function getFilteringRegex(string $filteredClass, string $direction): string | ||
{ | ||
$filteredClass = str_replace('*', '.*?', $filteredClass); | ||
|
||
if (!in_array($direction, ['out', 'in'])) { | ||
throw new InvalidArgumentException("Invalid direction '$direction'"); | ||
} | ||
|
||
return match ($direction) { | ||
'in' => "/.*?> ({$filteredClass}$|[\w]+_{$filteredClass}$)/", | ||
'out' => "/^({$filteredClass}|^[\w]+_{$filteredClass}) .*?>.*?/", | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
use Smeghead\PhpClassDiagram\Config\Options; | ||
use Smeghead\PhpClassDiagram\DiagramElement\RelationsFilter; | ||
|
||
final class RelationsFilterTest extends TestCase | ||
{ | ||
/** | ||
* @var array|string[] | ||
*/ | ||
private array $fixture; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->fixture = [ | ||
' Entry "1" ..> "*" Arrow', | ||
' Entry "1" ..> "*" Arrow', | ||
' Package "1" ..> "*" Package', | ||
' Package "1" ..> "*" Entry', | ||
' Package ..> Entry', | ||
' Package "1" ..> "*" Arrow', | ||
' Package "1" ..> "*" Entry', | ||
' Package ..> Package', | ||
' PackageRelations ..> Package', | ||
' PackageRelations ..> Package', | ||
' Relation ..> Package', | ||
' Relation ..> RelationsFilter', | ||
' Relation "1" ..> "*" Entry', | ||
' Relation ..> Package', | ||
' Arrow <|-- ArrowDependency', | ||
' Arrow <|-- ArrowInheritance', | ||
' ExternalPackage_PackageHierarchy ..> ExternalPackage_PackageNode', | ||
' ExternalPackage_PackageNode "1" ..> "*" ExternalPackage_PackageNode', | ||
' ExternalPackage_PackageNode "1" ..> "*" ExternalPackage_PackageNode', | ||
' ExternalPackage_PackageHierarchy ..> ExternalPackage_PackageNode', | ||
' ExternalPackage_PackageNode ..> ExternalPackage_PackageNode', | ||
' Entry ..> Division_DivisionColor', | ||
' Entry ..> ArrowDependency', | ||
' Entry ..> ArrowDependency', | ||
' Entry ..> ArrowDependency', | ||
' Entry ..> ArrowInheritance', | ||
' Entry ..> ArrowDependency', | ||
' Package ..> Entry', | ||
' Package ..> Package', | ||
' Package ..> Package', | ||
' Package ..> Package', | ||
' PackageRelations ..> Package', | ||
' PackageRelations ..> ExternalPackage_PackageHierarchy', | ||
' PackageRelations ..> PackageArrow', | ||
' PackageRelations ..> PackageArrow', | ||
' Relation ..> RelationsFilter', | ||
' Relation ..> Package', | ||
' Relation ..> Package', | ||
' Relation ..> Arrow', | ||
' Relation ..> PackageRelations', | ||
]; | ||
} | ||
|
||
public function testFiltersInboundRelations(): void | ||
{ | ||
$relationsFilter = new RelationsFilter(new Options([ | ||
'rel-target-to' => 'PackageNode' | ||
])); | ||
|
||
$result = $relationsFilter->filterRelations($this->fixture); | ||
|
||
$this->assertSame(" ExternalPackage_PackageHierarchy ..> ExternalPackage_PackageNode", $result[0]); | ||
$this->assertSame(" ExternalPackage_PackageNode \"1\" ..> \"*\" ExternalPackage_PackageNode", $result[1]); | ||
$this->assertSame(" ExternalPackage_PackageNode ..> ExternalPackage_PackageNode", $result[2]); | ||
$this->assertSame(" PackageRelations ..> ExternalPackage_PackageHierarchy", $result[3]); | ||
$this->assertSame(" Relation ..> PackageRelations", $result[4]); | ||
} | ||
|
||
public function testFiltersTargetRelations(): void | ||
{ | ||
$relationsFilter = new RelationsFilter(new Options([ | ||
'rel-target' => 'Entry' | ||
])); | ||
|
||
$result = $relationsFilter->filterRelations($this->fixture); | ||
|
||
$this->assertSame(" Entry \"1\" ..> \"*\" Arrow", $result[0]); | ||
$this->assertSame(" Entry ..> Division_DivisionColor", $result[1]); | ||
$this->assertSame(" Entry ..> ArrowDependency", $result[2]); | ||
$this->assertSame(" Entry ..> ArrowInheritance", $result[3]); | ||
$this->assertSame(" Package \"1\" ..> \"*\" Package", $result[4]); | ||
$this->assertSame(" Package \"1\" ..> \"*\" Entry", $result[5]); | ||
$this->assertSame(" Package ..> Entry", $result[6]); | ||
$this->assertSame(" Package ..> Package", $result[7]); | ||
$this->assertSame(" PackageRelations ..> Package", $result[8]); | ||
$this->assertSame(" Relation ..> Package", $result[9]); | ||
$this->assertSame(" Relation \"1\" ..> \"*\" Entry", $result[10]); | ||
$this->assertSame(" Relation ..> PackageRelations", $result[11]); | ||
} | ||
|
||
public function testFiltersInboundRelationsWithDepth(): void | ||
{ | ||
$relationsFilter = new RelationsFilter(new Options([ | ||
'rel-target-to' => 'PackageNode', | ||
'rel-target-depth' => 1 | ||
])); | ||
|
||
$result = $relationsFilter->filterRelations($this->fixture); | ||
|
||
$this->assertSame(" ExternalPackage_PackageHierarchy ..> ExternalPackage_PackageNode", $result[0]); | ||
$this->assertSame(" ExternalPackage_PackageNode \"1\" ..> \"*\" ExternalPackage_PackageNode", $result[1]); | ||
$this->assertSame(" ExternalPackage_PackageNode ..> ExternalPackage_PackageNode", $result[2]); | ||
$this->assertCount(3, $result); | ||
} | ||
|
||
public function testFiltersOutboundRelations(): void | ||
{ | ||
$relationsFilter = new RelationsFilter(new Options([ | ||
'rel-target-from' => 'Package' | ||
])); | ||
|
||
$result = $relationsFilter->filterRelations($this->fixture); | ||
|
||
$this->assertSame(" Entry \"1\" ..> \"*\" Arrow", $result[0]); | ||
$this->assertSame(" Package \"1\" ..> \"*\" Package", $result[1]); | ||
$this->assertSame(" Package \"1\" ..> \"*\" Entry", $result[2]); | ||
$this->assertSame(" Package ..> Entry", $result[3]); | ||
$this->assertSame(" Package \"1\" ..> \"*\" Arrow", $result[4]); | ||
$this->assertSame(" Package ..> Package", $result[5]); | ||
$this->assertSame(" Entry ..> Division_DivisionColor", $result[6]); | ||
$this->assertSame(" Entry ..> ArrowDependency", $result[7]); | ||
$this->assertSame(" Entry ..> ArrowInheritance", $result[8]); | ||
} | ||
|
||
public function testFiltersOutboundRelationsWithDepth(): void | ||
{ | ||
$relationsFilter = new RelationsFilter(new Options([ | ||
'rel-target-from' => 'Package', | ||
'rel-target-depth' => 1 | ||
])); | ||
|
||
$result = $relationsFilter->filterRelations($this->fixture); | ||
|
||
$this->assertSame(" Package \"1\" ..> \"*\" Package", $result[0]); | ||
$this->assertSame(" Package \"1\" ..> \"*\" Entry", $result[1]); | ||
$this->assertSame(" Package ..> Entry", $result[2]); | ||
$this->assertSame(" Package \"1\" ..> \"*\" Arrow", $result[3]); | ||
$this->assertSame(" Package ..> Package", $result[4]); | ||
} | ||
|
||
public function testGeneratesRemoveUnlinkedDirective(): void | ||
{ | ||
$relationsFilter = new RelationsFilter(new Options([ | ||
'rel-target-from' => 'Package' | ||
])); | ||
|
||
$relationsFilter->filterRelations($this->fixture); | ||
$result = $relationsFilter->addRemoveUnlinkedDirective([]); | ||
|
||
$this->assertSame(" remove @unlinked", $result[0]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.