Skip to content

Commit

Permalink
Merge pull request #15 from MontealegreLuis/object_from_docblock
Browse files Browse the repository at this point in the history
`object` from DocBlock and FQN as key in Digraph
  • Loading branch information
MontealegreLuis authored Sep 2, 2021
2 parents 5b9efcc + b654a36 commit 68212e9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/Graphviz/ObjectHashIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
use PhUml\Code\WithName;

/**
* Both `ClassDefinition` and `InterfaceDefinition` objects identifiers are generated using the
* function `spl_object_hash`
* Both `ClassDefinition` and `InterfaceDefinition` objects identifiers are generated using their Fully Qualified Name
*/
trait ObjectHashIdentifier
{
use WithName;

public function identifier(): string
{
return (string) $this->name();
return $this->name->fullName();
}
}
13 changes: 12 additions & 1 deletion src/Parser/Code/Builders/TagTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,18 @@ private function resolveType(?Type $type): ?TagType
$type === null => null,
$type instanceof Nullable => TagType::nullable((string) $type->getActualType()),
$type instanceof Compound => TagType::compound(array_map('strval', $type->getIterator()->getArrayCopy())),
default => TagType::named((string) ($type instanceof Object_ ? $type->getFqsen() : $type))
default => $this->fromType($type)
};
}

private function fromType(Type $type): TagType
{
if (! $type instanceof Object_) {
return TagType::named((string) $type);
}
if ($type->getFqsen() === null) {
return TagType::named((string) $type);
}
return TagType::named((string) $type->getFqsen());
}
}
11 changes: 8 additions & 3 deletions tests/src/Fakes/WithDotLanguageAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ trait WithDotLanguageAssertions
{
public function assertNode(Definition $definition, string $dotLanguage): void
{
$identifier = str_replace('\\', '\\\\', $definition->identifier());
$this->assertMatchesRegularExpression(
"/\"{$definition->identifier()}\" \\[label=<(?:.)+{$definition->name()}(?:.)+> shape=plaintext color=\"#[0-9a-f]{6}\"\\]/",
"/\"{$identifier}\" \\[label=<(?:.)+{$definition->name()}(?:.)+> shape=plaintext color=\"#[0-9a-f]{6}\"\\]/",
$dotLanguage,
"Definition {$definition->name()} with identifier {$definition->identifier()} cannot be found"
);
Expand All @@ -28,8 +29,10 @@ public function assertInheritance(
Definition $parent,
string $dotLanguage
): void {
$parentIdentifier = str_replace('\\', '\\\\', $parent->identifier());
$identifier = str_replace('\\', '\\\\', $definition->identifier());
$this->assertMatchesRegularExpression(
"/\"{$parent->identifier()}\" -> \"{$definition->identifier()}\" \\[dir=back arrowtail=empty style=solid color=\"#[0-9a-f]{6}\"\\]/",
"/\"{$parentIdentifier}\" -> \"{$identifier}\" \\[dir=back arrowtail=empty style=solid color=\"#[0-9a-f]{6}\"\\]/",
$dotLanguage,
"{$definition->name()} identified by {$definition->identifier()} does not inherits {$parent->name()} identified by {$parent->identifier()}"
);
Expand All @@ -40,8 +43,10 @@ public function assertImplementation(
InterfaceDefinition $interface,
string $dotLanguage
): void {
$interfaceIdentifier = str_replace('\\', '\\\\', $interface->identifier());
$identifier = str_replace('\\', '\\\\', $class->identifier());
$this->assertMatchesRegularExpression(
"/\"{$interface->identifier()}\" -> \"{$class->identifier()}\" \\[dir=back arrowtail=empty style=dashed color=\"#[0-9a-f]{6}\"\\]/",
"/\"{$interfaceIdentifier}\" -> \"{$identifier}\" \\[dir=back arrowtail=empty style=dashed color=\"#[0-9a-f]{6}\"\\]/",
$dotLanguage,
"{$class->name()} does not implements {$interface->name()}"
);
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Generators/GenerateDotFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function it_creates_the_dot_file_of_a_directory()

$digraph = $this->processor->process($codebase);

$this->assertNode(A::classNamed('plBase'), $digraph->value());
$this->assertNode(A::classNamed('plPhuml'), $digraph->value());
$this->assertNode(A::classNamed('phuml\plBase'), $digraph->value());
$this->assertNode(A::classNamed('phuml\plPhuml'), $digraph->value());
}

/** @test */
Expand All @@ -42,14 +42,14 @@ function it_creates_the_dot_file_of_a_directory_using_a_recursive_finder()

$digraph = $this->processor->process($codebase);

$base = A::classNamed('plBase');
$base = A::classNamed('phuml\plBase');
$tokenParser = A::classNamed('plStructureTokenparserGenerator');
$attribute = A::classNamed('plPhpAttribute');
$class = A::classNamed('plPhpClass');
$function = A::classNamed('plPhpFunction');
$parameter = A::classNamed('plPhpFunctionParameter');
$interface = A::classNamed('plPhpInterface');
$uml = A::classNamed('plPhuml');
$uml = A::classNamed('phuml\plPhuml');
$dotProcessor = A::classNamed('plDotProcessor');
$graphvizProcessor = A::classNamed('plGraphvizProcessor');
$styleName = A::classNamed('plStyleName');
Expand All @@ -60,7 +60,7 @@ function it_creates_the_dot_file_of_a_directory_using_a_recursive_finder()
$statisticsProcessor = A::classNamed('plStatisticsProcessor');
$structureGenerator = A::classNamed('plStructureGenerator');
$externalCommand = A::classNamed('plExternalCommandProcessor');
$processor = A::classNamed('plProcessor');
$processor = A::classNamed('phuml\interfaces\plProcessor');
$style = A::classNamed('plGraphvizProcessorStyle');
$this->assertNode($base, $digraph->value());
$this->assertNode($structureGenerator, $digraph->value());
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/Parser/Code/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@

final class TypeResolverTest extends TestCase
{
/** @test */
function it_resolves_built_in_types()
{
$useStatements = new UseStatements([]);

$objectType = $this->resolver->resolveForAttribute('/** @var object */', $useStatements);
$mixedType = $this->resolver->resolveForReturn('/** @return mixed */', $useStatements);
$stringType = $this->resolver->resolveForParameter('/** @param string[] $test */', '$test', $useStatements);
$boolType = $this->resolver->resolveForAttribute('/** @var bool */', $useStatements);

$this->assertEquals('object', (string) $objectType);
$this->assertEquals('mixed', (string) $mixedType);
$this->assertEquals('string[]', (string) $stringType);
$this->assertEquals('bool', (string) $boolType);
}

/** @test */
function it_resolves_to_absent_type_if_doc_block_is_invalid()
{
Expand Down

0 comments on commit 68212e9

Please sign in to comment.