Skip to content

Commit

Permalink
Merge pull request #11 from MontealegreLuis/relative_names
Browse files Browse the repository at this point in the history
Support for relative names in DocBlocks
  • Loading branch information
MontealegreLuis authored Aug 26, 2021
2 parents 674e237 + 152faa6 commit 50290c5
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ jobs:
chmod +x box.phar
./box.phar compile -vv
gpg -u [email protected] --detach-sign --output phuml.phar.asc phuml.phar
ls -alh phuml*
- name: "Upload binaries to distribute phUML via PHIVE"
uses: "svenstaro/upload-release-action@v2"
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: phuml.phar*
tag: ${{ github.ref }}
file_glob: true
3 changes: 1 addition & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ build:

tools:
external_code_coverage:
timeout: 600
runs: 3
timeout: 1200
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
SHELL = /bin/bash

ARGS=""
INFECTION_BADGE_API_KEY=""

.PHONY: help
help: ## Show help
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![CI workflow](https://github.com/montealegreluis/phuml/actions/workflows/ci.yml/badge.svg)](https://github.com/montealegreluis/phuml/actions/workflows/ci.yml)
[![Scrutinizer Code Quality][scrutinizer-badge]][scrutinizer]
[![Code Coverage][coverage-badge]][coverage]
[![Infection MSI](https://badge.stryker-mutator.io/github.com/montealegreluis/phuml/master)](https://dashboard.stryker-mutator.io/reports/github.com/montealegreluis/phuml/master)
[![Infection MSI](https://badge.stryker-mutator.io/github.com/MontealegreLuis/phuml/master)](https://dashboard.stryker-mutator.io/reports/github.com/MontealegreLuis/phuml/master)
[![Latest Stable Version][stable-badge]][packagist]
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.0-8892BF.svg?style=flat-square)](https://php.net/)

Expand Down
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
currentMenu: index
currentMenu: installation
---

# Installation
Expand Down
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![CI workflow](https://github.com/montealegreluis/phuml/actions/workflows/ci.yml/badge.svg)
[![Scrutinizer Code Quality][scrutinizer-badge]][scrutinizer]
[![Code Coverage][coverage-badge]][coverage]
[![Infection MSI](https://badge.stryker-mutator.io/github.com/montealegreluis/phuml/master)](https://dashboard.stryker-mutator.io/reports/github.com/montealegreluis/phuml/master)
[![Infection MSI](https://badge.stryker-mutator.io/github.com/MontealegreLuis/phuml/master)](https://dashboard.stryker-mutator.io/reports/github.com/MontealegreLuis/phuml/master)
[![Latest Stable Version][stable-badge]][packagist]
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.0-8892BF.svg?style=flat-square)](https://php.net/)

Expand Down
11 changes: 11 additions & 0 deletions src/Code/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function __construct(string $name)
$this->parts = explode('\\', trim($name));
}

public function first(): string
{
return $this->parts[0];
}

public function fullName(): string
{
return implode('\\', $this->parts);
Expand All @@ -40,4 +45,10 @@ public function __toString(): string
{
return $this->parts[count($this->parts) - 1];
}

public function packageName(): string
{
$package = array_slice($this->parts, 0, -1);
return implode('\\', $package);
}
}
12 changes: 11 additions & 1 deletion src/Code/UseStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ public function endsWith(Name $name): bool
return str_ends_with(haystack: (string) $this->name, needle: $name->removeArraySuffix());
}

public function includes(Name $name): bool
{
return str_ends_with(haystack: (string) $this->name, needle: $name->first());
}

public function isAliasedAs(Name $name): bool
{
return $this->alias !== null && (string) $this->alias === $name->removeArraySuffix();
}

public function fullyQualifiedName(Name $name): string
{
return $name->isArray() ? $this->name->fullName() . '[]' : $this->name->fullName();
return $name->isArray() ? "{$this->name->fullName()}[]" : $this->name->fullName();
}

public function merge(Name $name): string
{
return "{$this->name->packageName()}\\{$name->fullName()}";
}
}
3 changes: 3 additions & 0 deletions src/Code/UseStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function fullyQualifiedNameFor(Name $name): string
if ($useStatement->endsWith($name)) {
return $useStatement->fullyQualifiedName($name);
}
if ($useStatement->includes($name)) {
return $useStatement->merge($name);
}
if ($useStatement->isAliasedAs($name)) {
return $useStatement->fullyQualifiedName($name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/Code/Builders/TagType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function named(string $type): TagType
}

/** @param string[] $types */
public function __construct(private array $types, private bool $isNullable = false)
private function __construct(private array $types, private bool $isNullable = false)
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Code/Builders/TagTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Nullable;
use phpDocumentor\Reflection\Types\Object_;

final class TagTypeFactory
{
Expand Down Expand Up @@ -88,7 +89,7 @@ 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)
default => TagType::named((string) ($type instanceof Object_ ? $type->getFqsen() : $type))
};
}
}
39 changes: 39 additions & 0 deletions tests/unit/Parser/Code/Builders/TagTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);
/**
* PHP version 8.0
*
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*/

namespace PhUml\Parser\Code\Builders;

use PHPUnit\Framework\TestCase;
use PhUml\Code\Name;
use PhUml\Code\UseStatement;
use PhUml\Code\UseStatements;
use PhUml\Code\Variables\TypeDeclaration;

final class TagTypeTest extends TestCase
{
/** @test */
function it_resolves_fully_qualified_names_from_relative_names()
{
$useStatements = new UseStatements([new UseStatement(new Name('PhpParser\Node'), null)]);
$tagType = TagType::named('Node\Param[]');

$type = $tagType->resolve($useStatements);

$this->assertEquals(TypeDeclaration::from('PhpParser\Node\Param[]'), $type);
}

/** @test */
function it_resolves_fully_qualified_names_from_relative_names_with_multiple_prefix()
{
$useStatements = new UseStatements([new UseStatement(new Name('PhpParser\Node'), null)]);
$tagType = TagType::named('Node\Another\Param[]');

$type = $tagType->resolve($useStatements);

$this->assertEquals(TypeDeclaration::from('PhpParser\Node\Another\Param[]'), $type);
}
}

0 comments on commit 50290c5

Please sign in to comment.