Skip to content

Commit b7d7a3e

Browse files
committed
Since name resolution is done with NameResolver, unnecessary processing has been deleted.
1 parent 8bbd422 commit b7d7a3e

40 files changed

+26
-44
lines changed

dogfood-model.png

4.42 KB
Loading

dogfood-package.png

8.86 KB
Loading

dogfood.png

13.5 KB
Loading

src/Php/Finder/FindConstructerProperties.php renamed to src/Php/Finders/FindConstructerProperties.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Smeghead\PhpClassDiagram\Php\Finder;
5+
namespace Smeghead\PhpClassDiagram\Php\Finders;
66

77
use PhpParser\Node;
88
use PhpParser\Node\Param;

src/Php/PhpClass.php

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44

55
namespace Smeghead\PhpClassDiagram\Php;
66

7-
use PhpParser\NodeFinder;
8-
use PhpParser\Node;
9-
use PhpParser\Node\{
10-
NullableType,
11-
Identifier,
12-
Name,
13-
UnionType,
14-
};
157
use PhpParser\Node\Name\FullyQualified;
168
use PhpParser\Node\Stmt;
179
use PhpParser\Node\Stmt\{
@@ -25,7 +17,7 @@
2517
Use_,
2618
};
2719
use Smeghead\PhpClassDiagram\Php\Doc\PhpDocComment;
28-
use Smeghead\PhpClassDiagram\Php\Finder\FindConstructerProperties;
20+
use Smeghead\PhpClassDiagram\Php\Finders\FindConstructerProperties;
2921

3022
class PhpClass
3123
{
@@ -156,27 +148,13 @@ private function getUsesRec($stmts, $uses = [])
156148
* * useしているクラスに目的のクラスがあるかを探す
157149
* * 自身のクラス名が目的のクラスかどうか ... (不要かもしれない。暗黙の参照と統合可能
158150
* * 暗黙の参照として、自身のnamespaceを返却する
151+
*
152+
* Since name resolution is done with NameResolver, unnecessary processing has been deleted.
159153
*/
160154
private function findNamespaceByTypeParts(array $type_parts): array
161155
{
162-
$type = str_replace('[]', '', array_pop($type_parts));
163-
$primitives = ['string', 'bool', 'boolean', 'int', 'integer', 'float', 'double', 'array', 'object', 'resource'];
164-
if (in_array($type, $primitives)) {
165-
return [];
166-
}
167-
foreach ($this->getUses() as $u) {
168-
if ($u->getName() === $type) {
169-
return $u->getNamespace();
170-
}
171-
}
172-
// 探したいクラスが、自身の型だった場合
173-
$t = $this->getClassType();
174-
if ($t->getName() === $type) {
175-
return $t->getNamespace();
176-
}
177-
178-
// 暗黙的な参照と見做す
179-
return $this->getNamespace();
156+
array_pop($type_parts);
157+
return $type_parts;
180158
}
181159

182160
/** @return PhpMethod[] メソッド一覧 */
@@ -213,25 +191,19 @@ public function getExtends(): array
213191
'',
214192
end($Name->parts)
215193
);
216-
} else {
217-
$parts = $Name->parts;
218-
$namespace = [];
219-
if (count($parts) > 0) {
220-
$namespace = $this->findNamespaceByTypeParts($parts);
221-
$typeName = array_pop($parts);
222-
}
223-
$extends[] = new PhpType(array_merge($namespace, $parts), 'Stmt_Class', $typeName);
224194
}
225195
}
196+
226197
if (!empty($this->syntax->implements)) {
227-
foreach ($this->syntax->implements as $i) {
228-
$parts = $i->parts;
229-
$namespace = [];
230-
if (count($parts) > 0) {
231-
$namespace = $this->findNamespaceByTypeParts($parts);
232-
$typeName = array_pop($parts);
198+
foreach ($this->syntax->implements as $Name) {
199+
// $parts = $i->parts;
200+
if ($Name instanceof FullyQualified) {
201+
$extends[] = new PhpType(
202+
array_slice($Name->parts, 0, count($Name->parts) - 1),
203+
'',
204+
end($Name->parts)
205+
);
233206
}
234-
$extends[] = new PhpType(array_merge($namespace, $parts), 'Stmt_Interface', $typeName);
235207
}
236208
}
237209
return $extends;

src/Php/PhpReader.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
Namespace_,
1212
ClassLike,
1313
};
14+
use PhpParser\NodeTraverser;
15+
use PhpParser\NodeVisitor\NameResolver;
1416
use Smeghead\PhpClassDiagram\Config\Options;
1517

1618
class PhpReader
@@ -50,6 +52,11 @@ public static function parseFile(string $directory, string $filename, Options $o
5052
$parser = (new ParserFactory)->create($targetVesion);
5153
try {
5254
$ast = $parser->parse($code);
55+
$nameResolver = new NameResolver();
56+
$nodeTraverser = new NodeTraverser();
57+
$nodeTraverser->addVisitor($nameResolver);
58+
// Resolve names
59+
$ast = $nodeTraverser->traverse($ast);
5360
} catch (Error $error) {
5461
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
5562
}

src/Php/PhpTypeExpression.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Name;
99
use PhpParser\Node\Name\FullyQualified;
1010
use PhpParser\Node\NullableType;
11-
use PhpParser\Node\Param;
1211
use PhpParser\Node\Stmt\ClassMethod;
1312
use PhpParser\Node\Stmt\Property;
1413
use PhpParser\Node\UnionType;
2.78 KB
Loading
9.43 KB
Loading
3.43 KB
Loading

0 commit comments

Comments
 (0)