Skip to content

Commit 53f88c4

Browse files
committed
make method return type PhpTypeExcepression.
1 parent 95f78b2 commit 53f88c4

File tree

7 files changed

+17
-66
lines changed

7 files changed

+17
-66
lines changed

src/DiagramElement/Entry.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public function getArrows(): array {
9292
if (count($m->getParams()) > 0) {
9393
continue;
9494
}
95-
$arrows[] = new ArrowDependency($this->class, $m->getType());
95+
foreach ($m->getType()->getTypes() as $t) {
96+
$arrows[] = new ArrowDependency($this->class, $t);
97+
}
9698
}
9799
$extends = $this->class->getExtends();
98100
if ( ! empty($extends)) {

src/Php/PhpClass.php

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -147,54 +147,6 @@ protected function findNamespaceByTypeParts(array $type_parts): array {
147147
return $this->getNamespace();
148148
}
149149

150-
/**
151-
* php-parserの解析結果の情報から、指定のクラスの型情報を取得します。
152-
* クラスのuseの状態から、namespaceを解決したPhpTypeを返却します。
153-
* @param NodeAbstract ClassMethod または Property または Param が渡されることを想定
154-
*/
155-
public function findTypeByTypeParts(NodeAbstract $stmt, string $property, string $docAttribute = ''): PhpType {
156-
$docComment = $stmt->getDocComment();
157-
$doc = '';
158-
if ( ! empty($docComment)) {
159-
$doc = $docComment->getText();
160-
}
161-
$parts = [];
162-
$type = $stmt->{$property};
163-
if ($type instanceOf NullableType) {
164-
$type = $type->type;
165-
}
166-
if ( ! empty($doc) && ! empty($docAttribute)) {
167-
// @{$docAttribute} に定義された型情報を取得する。
168-
if (preg_match(sprintf('/@%s\s+(\S+)(\b|\s).*/', $docAttribute), $doc, $matches)) {
169-
$typeString = $matches[1];
170-
if (mb_substr($typeString, 0, 1) === '\\') {
171-
$parts = explode('\\', mb_substr($typeString, 1));
172-
$typeName = array_pop($parts);
173-
return new PhpType($parts, '', $typeName);
174-
}
175-
176-
$parts = explode('\\', $typeString);
177-
}
178-
} else if ($type instanceOf Identifier) {
179-
$parts[] = $type->name;
180-
} else if ($type instanceOf FullyQualified) {
181-
return new PhpType(
182-
array_slice($type->parts, 0, count($type->parts) - 1),
183-
'',
184-
end($type->parts));
185-
} else if ($type instanceOf Name) {
186-
$parts = $type->parts;
187-
} else if ($type instanceOf UnionType) {
188-
189-
}
190-
$namespace = [];
191-
if (count($parts) > 0) {
192-
$namespace = $this->findNamespaceByTypeParts($parts);
193-
$typeName = array_pop($parts);
194-
}
195-
return new PhpType($namespace, $stmt->getType(), $typeName ?? '');
196-
}
197-
198150
/** @return PhpMethod[] メソッド一覧 */
199151
public function getMethods(): array {
200152
$methods = [];

src/Php/PhpMethod.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class PhpMethod {
1313
protected string $name;
14-
protected PhpType $type;
14+
protected PhpTypeExpression $type;
1515
/** @var PhpMethodParameter[] パラメータ一覧 */
1616
protected array $params;
1717
protected PhpAccessModifier $accessModifier;
@@ -23,7 +23,6 @@ public function __construct(ClassMethod $method, PhpClass $class) {
2323
$docString = $doc->getText();
2424
}
2525
$params = array_map(function($x) use ($class, $docString){
26-
// $type = $class->findTypeByTypeParts($x, 'type');
2726
$type = PhpTypeExpression::buildByMethodParam($x, $class->getNamespace(), $docString, $x->var->name, $class->getUses());
2827
return new PhpMethodParameter($x->var->name, $type);
2928
}, $method->getParams());
@@ -33,15 +32,15 @@ public function __construct(ClassMethod $method, PhpClass $class) {
3332
$this->accessModifier = new PhpAccessModifier($method);
3433
}
3534

36-
private function getTypeFromMethod(ClassMethod $method, PhpClass $class): PhpType {
37-
return $class->findTypeByTypeParts($method, 'returnType', 'return');
35+
private function getTypeFromMethod(ClassMethod $method, PhpClass $class): PhpTypeExpression {
36+
return PhpTypeExpression::buildByMethodReturn($method, $class->getNamespace(), $class->getUses());
3837
}
3938

4039
public function getName(): string {
4140
return $this->name;
4241
}
4342

44-
public function getType(): PhpType {
43+
public function getType(): PhpTypeExpression {
4544
return $this->type;
4645
}
4746

src/Php/PhpProperty.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class PhpProperty {
1212

1313
public function __construct(Property $p, PhpClass $class) {
1414
$this->name = $p->props[0]->name->toString();
15-
// $this->type = $class->findTypeByTypeParts($p, 'type', 'var');
1615
$this->type = PhpTypeExpression::buildByVar($p, $class->getNamespace(), $class->getUses());
1716
$this->accessModifier = new PhpAccessModifier($p);
1817
}

src/Php/PhpTypeExpression.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ private function parseType(Property|Identifier|NullableType|Name|null $type, arr
165165
if (count($targets) > 0) {
166166
$parts = array_merge($targets[0]->getNamespace(), [$targets[0]->getName()]);
167167
} else {
168-
$docString = sprintf('%s\\%s', implode('\\', $currentNamespace), $typeString);
169-
$parts = explode('\\', $docString);
168+
$parts = array_merge($currentNamespace, explode('\\', $typeString));
170169
}
171170
}
172171
}

test/PhpReflectionTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function testDump_with_methods(): void {
131131
$this->assertSame('method1', $data->getMethods()[0]->getName(), 'namespace name.');
132132
$this->assertSame('param1', $data->getMethods()[0]->getParams()[0]->getName(), 'parameter name.');
133133
$this->assertSame('Product', $data->getMethods()[0]->getType()->getName(), 'return type.');
134-
$this->assertSame([], $data->getMethods()[0]->getType()->getNamespace(), 'return type namespace.');
134+
$this->assertSame([], $data->getMethods()[0]->getType()->getTypes()[0]->getNamespace(), 'return type namespace.');
135135
$this->assertSame(true, $data->getMethods()[0]->getAccessModifier()->isPublic(), 'public.');
136136
$this->assertSame(false, $data->getMethods()[0]->getAccessModifier()->isPrivate(), 'private.');
137137
$this->assertSame(false, $data->getMethods()[0]->getAccessModifier()->isStatic(), 'static.');
@@ -148,7 +148,7 @@ public function testDump_with_methods2(): void {
148148
$this->assertSame(['hoge', 'fuga', 'product'], $data->getClassType()->getNamespace(), 'namespace name.');
149149
$this->assertSame('method1', $data->getMethods()[0]->getName(), 'namespace name.');
150150
$this->assertSame('Product', $data->getMethods()[0]->getType()->getName(), 'return type.');
151-
$this->assertSame(['hoge', 'fuga', 'product'], $data->getMethods()[0]->getType()->getNamespace(), 'return type namespace.');
151+
$this->assertSame(['hoge', 'fuga', 'product'], $data->getMethods()[0]->getType()->getTypes()[0]->getNamespace(), 'return type namespace.');
152152
$this->assertSame('param1', $data->getMethods()[0]->getParams()[0]->getName(), 'parameter name.');
153153
$this->assertSame(true, $data->getMethods()[0]->getAccessModifier()->isPublic(), 'public.');
154154
$this->assertSame(false, $data->getMethods()[0]->getAccessModifier()->isPrivate(), 'private.');
@@ -221,8 +221,8 @@ public function testNullable(): void {
221221
$this->assertSame('Product', $data->getClassType()->getName(), 'class type name.');
222222
$this->assertSame(['hoge', 'fuga', 'product'], $data->getClassType()->getNamespace(), 'namespace name.');
223223
$this->assertSame('nullable', $data->getMethods()[0]->getName(), 'namespace name.');
224-
$this->assertSame('Tag', $data->getMethods()[0]->getType()->getName(), 'return type.');
225-
$this->assertSame(['hoge', 'fuga', 'product', 'tags'], $data->getMethods()[0]->getType()->getNamespace(), 'return type namespace.');
224+
$this->assertSame('?Tag', $data->getMethods()[0]->getType()->getName(), 'return type.');
225+
$this->assertSame(['hoge', 'fuga', 'product', 'tags'], $data->getMethods()[0]->getType()->getTypes()[0]->getNamespace(), 'return type namespace.');
226226
$this->assertSame('name', $data->getMethods()[0]->getParams()[0]->getName(), 'parameter name.');
227227
$this->assertSame('?Name', $data->getMethods()[0]->getParams()[0]->getType()->getName(), 'parameter type.');
228228
$this->assertSame(['hoge', 'fuga', 'product'], $data->getMethods()[0]->getParams()[0]->getType()->getTypes()[0]->getNamespace(), 'parameter type namespace.');
@@ -240,8 +240,8 @@ public function testNullableWithoutNamespace(): void {
240240
$this->assertSame('ProductWithoutNamespace', $data->getClassType()->getName(), 'class type name.');
241241
$this->assertSame([], $data->getClassType()->getNamespace(), 'namespace name.');
242242
$this->assertSame('nullable', $data->getMethods()[0]->getName(), 'namespace name.');
243-
$this->assertSame('Name', $data->getMethods()[0]->getType()->getName(), 'return type.');
244-
$this->assertSame(['hoge', 'fuga', 'product'], $data->getMethods()[0]->getType()->getNamespace(), 'return type namespace.');
243+
$this->assertSame('?Name', $data->getMethods()[0]->getType()->getName(), 'return type.');
244+
$this->assertSame(['hoge', 'fuga', 'product'], $data->getMethods()[0]->getType()->getTypes()[0]->getNamespace(), 'return type namespace.');
245245
$this->assertSame('name', $data->getMethods()[0]->getParams()[0]->getName(), 'parameter name.');
246246
$this->assertSame('?Name', $data->getMethods()[0]->getParams()[0]->getType()->getName(), 'parameter type.');
247247
$this->assertSame(['hoge', 'fuga', 'product'], $data->getMethods()[0]->getParams()[0]->getType()->getTypes()[0]->getNamespace(), 'parameter type namespace.');
@@ -259,13 +259,13 @@ public function testFullyQualified(): void {
259259
$this->assertSame([], $data->getExtends()[0]->getNamespace(), 'super class namespace.');
260260
$this->assertSame('getInner', $data->getMethods()[0]->getName(), 'method name.');
261261
$this->assertSame('Exception', $data->getMethods()[0]->getType()->getName(), 'return type.');
262-
$this->assertSame([], $data->getMethods()[0]->getType()->getNamespace(), 'return type namespace.');
262+
$this->assertSame([], $data->getMethods()[0]->getType()->getTypes()[0]->getNamespace(), 'return type namespace.');
263263
$this->assertSame('e', $data->getMethods()[0]->getParams()[0]->getName(), 'parameter name.');
264264
$this->assertSame('Exception', $data->getMethods()[0]->getParams()[0]->getType()->getName(), 'parameter type.');
265265
$this->assertSame([], $data->getMethods()[0]->getParams()[0]->getType()->getTypes()[0]->getNamespace(), 'parameter type namespace.');
266266
$this->assertSame('external', $data->getMethods()[1]->getName(), 'method name.');
267267
$this->assertSame('Exception', $data->getMethods()[1]->getType()->getName(), 'return type.');
268-
$this->assertSame(['external'], $data->getMethods()[1]->getType()->getNamespace(), 'return type namespace.');
268+
$this->assertSame(['external'], $data->getMethods()[1]->getType()->getTypes()[0]->getNamespace(), 'return type namespace.');
269269
}
270270
public function testClassesInAFile(): void {
271271
$options = new Options([]);

test/dummy/PhpMethodDummy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(\stdClass $method) {
2222
return new PhpMethodParameter($x->name, PhpTypeExpression::buildByPhpType(new PhpType([], '', $x->type->name)));
2323
}, $method->params);
2424
$this->name = $method->name;
25-
$this->type = new PhpType($method->type->namespace, 'Stmt_Class', $method->type->name);
25+
$this->type = PhpTypeExpression::buildByPhpType(new PhpType($method->type->namespace, 'Stmt_Class', $method->type->name));
2626
$this->params = $params;
2727
$this->accessModifier = new PhpAccessModifierDummy($method->modifier);
2828
}

0 commit comments

Comments
 (0)