Skip to content

Commit

Permalink
v0.2.1 (2023-02-16)
Browse files Browse the repository at this point in the history
Bug fix

 * Fixed an exception that occurred when a UnionType was specified in DocString.
 * Fixed an issue where DocString did not correctly compare types when describing an array of classes.
  • Loading branch information
smeghead committed Feb 16, 2023
1 parent 992d1ae commit 215f667
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## v0.2.1 (2023-02-16)

### Bug fix

* Fixed an exception that occurred when a UnionType was specified in DocString.
* Fixed an issue where DocString did not correctly compare types when describing an array of classes.

## v0.2.0 (2023-02-16)

### Features
Expand Down
2 changes: 1 addition & 1 deletion src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Smeghead\PhpClassDiagram\Php\PhpReader;

class Main {
const VERSION = 'v0.2.0';
const VERSION = 'v0.2.1';

public function __construct(string $directory, Options $options) {
$finder = new Finder();
Expand Down
11 changes: 7 additions & 4 deletions src/Php/PhpTypeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ public static function buildByPhpType(PhpType $type): self {
}

/**
* @param Property|Identifier|NullableType|Name $type 型を表すAST
* @param Property|Identifier|NullableType|Name|UnionType|null $type 型を表すAST (UnionTypeが指定されて呼び出される時は、typeStringで判断する時なので型判定には使われない)
* @param string[] $currentNamespace 名前空間配列
* @param ?string $typeString コメントの型表記
*/
private function parseType(Property|Identifier|NullableType|Name|null $type, array $currentNamespace, ?string $typeString = '') {
private function parseType(Property|Identifier|NullableType|Name|UnionType|null $type, array $currentNamespace, ?string $typeString = '') {
$parts = [];
if (!empty($typeString)) {
$primitiveTypes = [
Expand All @@ -160,10 +160,13 @@ private function parseType(Property|Identifier|NullableType|Name|null $type, arr
$targets = array_values(array_filter($this->uses, function(PhpType $t) use($typeString) {
$xParts = explode('\\', $typeString);
$name = end($xParts);
return $name === $t->getName();
// docString で配列が指定されていた場合は、[] を除外して比較する。
return preg_replace('/\[\]$/', '', $name) === $t->getName();
}));
if (count($targets) > 0) {
$parts = array_merge($targets[0]->getNamespace(), [$targets[0]->getName()]);
$parts = array_merge(
$targets[0]->getNamespace(),
[sprintf('%s%s', $targets[0]->getName(), preg_match('/\[\]$/', $typeString) ? '[]' : '')]);
} else {
$parts = array_merge($currentNamespace, explode('\\', $typeString));
}
Expand Down
62 changes: 62 additions & 0 deletions test/PhpTypeExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,67 @@ public function testMethodParameterTag(): void {
$this->assertSame('Tag', $types[0]->getName(), 'name');
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
}
public function testMethodReturnUnion(): void {
// /** @params string|int $param1 */
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$filename = sprintf('%s/php8/product/Product.php', $this->fixtureDir);
try {
$ast = $parser->parse(file_get_contents($filename));
} catch (Error $error) {
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
}
$method = $ast[0]->stmts[2]->stmts[14];
// var_dump($method);die();
$expression = PhpTypeExpression::buildByMethodReturn($method, ['hoge', 'fuga', 'product'], []);
$types = $expression->getTypes();

$this->assertSame('method5', $method->name->name, 'method name');
$this->assertSame([], $types[0]->getNamespace(), 'namespace');
$this->assertSame('int', $types[0]->getName(), 'name');
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
$this->assertSame('string', $types[1]->getName(), 'name');
$this->assertSame(false, $types[1]->getNullable(), 'nullable');
}
public function testMethodReturnUnionDoc(): void {
// /** @params string|int $param1 */
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$filename = sprintf('%s/php8/product/Product.php', $this->fixtureDir);
try {
$ast = $parser->parse(file_get_contents($filename));
} catch (Error $error) {
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
}
$method = $ast[0]->stmts[2]->stmts[15];
// var_dump($method);die();
$expression = PhpTypeExpression::buildByMethodReturn($method, ['hoge', 'fuga', 'product'], []);
$types = $expression->getTypes();

$this->assertSame('method6', $method->name->name, 'method name');
$this->assertSame([], $types[0]->getNamespace(), 'namespace');
$this->assertSame('int', $types[0]->getName(), 'name');
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
$this->assertSame('string', $types[1]->getName(), 'name');
$this->assertSame(false, $types[1]->getNullable(), 'nullable');
}
public function testMethodReturnObjectArray(): void {
// /** @params string|int $param1 */
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$filename = sprintf('%s/php8/product/Product.php', $this->fixtureDir);
try {
$ast = $parser->parse(file_get_contents($filename));
} catch (Error $error) {
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
}
$method = $ast[0]->stmts[2]->stmts[16];
$uses = [new PhpType(['hoge', 'fuga', 'product', 'tag'], '', 'Tag')];
// var_dump($method);die();
$expression = PhpTypeExpression::buildByMethodReturn($method, ['hoge', 'fuga', 'product'], $uses);
$types = $expression->getTypes();

$this->assertSame('method7', $method->name->name, 'method name');
$this->assertSame(['hoge', 'fuga', 'product', 'tag'], $types[0]->getNamespace(), 'namespace');
$this->assertSame('Tag[]', $types[0]->getName(), 'name');
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
}

}
16 changes: 16 additions & 0 deletions test/fixtures/php8/product/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,20 @@ public function method3(): array {
public function method4(Tag $tag): array {
return [];
}

public function method5(Tag $tag): int|string {
return 0;
}
/**
* @return int|string return value.
*/
public function method6(Tag $tag): int|string {
return 0;
}
/**
* @return Tag[] tags
*/
public function method7(Tag $tag): array {
return 0;
}
}

0 comments on commit 215f667

Please sign in to comment.