Skip to content

Commit 215f667

Browse files
committed
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.
1 parent 992d1ae commit 215f667

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## v0.2.1 (2023-02-16)
4+
5+
### Bug fix
6+
7+
* Fixed an exception that occurred when a UnionType was specified in DocString.
8+
* Fixed an issue where DocString did not correctly compare types when describing an array of classes.
9+
310
## v0.2.0 (2023-02-16)
411

512
### Features

src/Main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Smeghead\PhpClassDiagram\Php\PhpReader;
1111

1212
class Main {
13-
const VERSION = 'v0.2.0';
13+
const VERSION = 'v0.2.1';
1414

1515
public function __construct(string $directory, Options $options) {
1616
$finder = new Finder();

src/Php/PhpTypeExpression.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ public static function buildByPhpType(PhpType $type): self {
131131
}
132132

133133
/**
134-
* @param Property|Identifier|NullableType|Name $type 型を表すAST
134+
* @param Property|Identifier|NullableType|Name|UnionType|null $type 型を表すAST (UnionTypeが指定されて呼び出される時は、typeStringで判断する時なので型判定には使われない)
135135
* @param string[] $currentNamespace 名前空間配列
136136
* @param ?string $typeString コメントの型表記
137137
*/
138-
private function parseType(Property|Identifier|NullableType|Name|null $type, array $currentNamespace, ?string $typeString = '') {
138+
private function parseType(Property|Identifier|NullableType|Name|UnionType|null $type, array $currentNamespace, ?string $typeString = '') {
139139
$parts = [];
140140
if (!empty($typeString)) {
141141
$primitiveTypes = [
@@ -160,10 +160,13 @@ private function parseType(Property|Identifier|NullableType|Name|null $type, arr
160160
$targets = array_values(array_filter($this->uses, function(PhpType $t) use($typeString) {
161161
$xParts = explode('\\', $typeString);
162162
$name = end($xParts);
163-
return $name === $t->getName();
163+
// docString で配列が指定されていた場合は、[] を除外して比較する。
164+
return preg_replace('/\[\]$/', '', $name) === $t->getName();
164165
}));
165166
if (count($targets) > 0) {
166-
$parts = array_merge($targets[0]->getNamespace(), [$targets[0]->getName()]);
167+
$parts = array_merge(
168+
$targets[0]->getNamespace(),
169+
[sprintf('%s%s', $targets[0]->getName(), preg_match('/\[\]$/', $typeString) ? '[]' : '')]);
167170
} else {
168171
$parts = array_merge($currentNamespace, explode('\\', $typeString));
169172
}

test/PhpTypeExpressionTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,5 +305,67 @@ public function testMethodParameterTag(): void {
305305
$this->assertSame('Tag', $types[0]->getName(), 'name');
306306
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
307307
}
308+
public function testMethodReturnUnion(): void {
309+
// /** @params string|int $param1 */
310+
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
311+
$filename = sprintf('%s/php8/product/Product.php', $this->fixtureDir);
312+
try {
313+
$ast = $parser->parse(file_get_contents($filename));
314+
} catch (Error $error) {
315+
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
316+
}
317+
$method = $ast[0]->stmts[2]->stmts[14];
318+
// var_dump($method);die();
319+
$expression = PhpTypeExpression::buildByMethodReturn($method, ['hoge', 'fuga', 'product'], []);
320+
$types = $expression->getTypes();
321+
322+
$this->assertSame('method5', $method->name->name, 'method name');
323+
$this->assertSame([], $types[0]->getNamespace(), 'namespace');
324+
$this->assertSame('int', $types[0]->getName(), 'name');
325+
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
326+
$this->assertSame('string', $types[1]->getName(), 'name');
327+
$this->assertSame(false, $types[1]->getNullable(), 'nullable');
328+
}
329+
public function testMethodReturnUnionDoc(): void {
330+
// /** @params string|int $param1 */
331+
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
332+
$filename = sprintf('%s/php8/product/Product.php', $this->fixtureDir);
333+
try {
334+
$ast = $parser->parse(file_get_contents($filename));
335+
} catch (Error $error) {
336+
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
337+
}
338+
$method = $ast[0]->stmts[2]->stmts[15];
339+
// var_dump($method);die();
340+
$expression = PhpTypeExpression::buildByMethodReturn($method, ['hoge', 'fuga', 'product'], []);
341+
$types = $expression->getTypes();
342+
343+
$this->assertSame('method6', $method->name->name, 'method name');
344+
$this->assertSame([], $types[0]->getNamespace(), 'namespace');
345+
$this->assertSame('int', $types[0]->getName(), 'name');
346+
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
347+
$this->assertSame('string', $types[1]->getName(), 'name');
348+
$this->assertSame(false, $types[1]->getNullable(), 'nullable');
349+
}
350+
public function testMethodReturnObjectArray(): void {
351+
// /** @params string|int $param1 */
352+
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
353+
$filename = sprintf('%s/php8/product/Product.php', $this->fixtureDir);
354+
try {
355+
$ast = $parser->parse(file_get_contents($filename));
356+
} catch (Error $error) {
357+
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
358+
}
359+
$method = $ast[0]->stmts[2]->stmts[16];
360+
$uses = [new PhpType(['hoge', 'fuga', 'product', 'tag'], '', 'Tag')];
361+
// var_dump($method);die();
362+
$expression = PhpTypeExpression::buildByMethodReturn($method, ['hoge', 'fuga', 'product'], $uses);
363+
$types = $expression->getTypes();
364+
365+
$this->assertSame('method7', $method->name->name, 'method name');
366+
$this->assertSame(['hoge', 'fuga', 'product', 'tag'], $types[0]->getNamespace(), 'namespace');
367+
$this->assertSame('Tag[]', $types[0]->getName(), 'name');
368+
$this->assertSame(false, $types[0]->getNullable(), 'nullable');
369+
}
308370

309371
}

test/fixtures/php8/product/Product.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,20 @@ public function method3(): array {
4343
public function method4(Tag $tag): array {
4444
return [];
4545
}
46+
47+
public function method5(Tag $tag): int|string {
48+
return 0;
49+
}
50+
/**
51+
* @return int|string return value.
52+
*/
53+
public function method6(Tag $tag): int|string {
54+
return 0;
55+
}
56+
/**
57+
* @return Tag[] tags
58+
*/
59+
public function method7(Tag $tag): array {
60+
return 0;
61+
}
4662
}

0 commit comments

Comments
 (0)