Skip to content

Commit f367c4d

Browse files
committed
Fixed warnings in test code. Improved the method of getting the target Node from the syntax tree.
1 parent 16abe01 commit f367c4d

File tree

3 files changed

+158
-77
lines changed

3 files changed

+158
-77
lines changed

src/Php/PhpClass.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,6 @@ private function getUsesRec($stmts, $uses = [])
142142
return $uses;
143143
}
144144

145-
/**
146-
* クラス名が、どのnamespaceに属しているかを判定する。
147-
*
148-
* * useしているクラスに目的のクラスがあるかを探す
149-
* * 自身のクラス名が目的のクラスかどうか ... (不要かもしれない。暗黙の参照と統合可能
150-
* * 暗黙の参照として、自身のnamespaceを返却する
151-
*
152-
* Since name resolution is done with NameResolver, unnecessary processing has been deleted.
153-
*/
154-
private function findNamespaceByTypeParts(array $type_parts): array
155-
{
156-
array_pop($type_parts);
157-
return $type_parts;
158-
}
159-
160145
/** @return PhpMethod[] メソッド一覧 */
161146
public function getMethods(): array
162147
{

test/PhpDocCommentTest.php

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
declare(strict_types=1);
44

5+
use PhpParser\Node;
6+
use PhpParser\Node\Stmt\Class_;
7+
use PhpParser\Node\Stmt\ClassMethod;
8+
use PhpParser\Node\Stmt\Enum_;
9+
use PhpParser\Node\Stmt\EnumCase;
10+
use PhpParser\Node\Stmt\Property;
11+
use PhpParser\NodeFinder;
512
use PhpParser\ParserFactory;
613
use PHPUnit\Framework\TestCase;
714
use Smeghead\PhpClassDiagram\Php\Doc\PhpDocComment;
@@ -23,9 +30,11 @@ public function testDocString(): void
2330
} catch (Error $error) {
2431
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
2532
}
33+
$finder = new NodeFinder();
34+
$enum = $finder->findFirst($ast, function(Node $node){
35+
return $node instanceof Enum_ && $node->name->toString() === 'Suit';
36+
});
2637

27-
// var_dump($ast[0]->stmts[0]);
28-
$enum = $ast[0]->stmts[0];
2938
$doc = new PhpDocComment($enum);
3039

3140
$this->assertSame('スート', $doc->getText(), 'comment string');
@@ -39,9 +48,10 @@ public function test_enum_getDescription(): void
3948
} catch (Error $error) {
4049
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
4150
}
42-
43-
// var_dump($ast[0]->stmts[0]);
44-
$enum = $ast[0]->stmts[0];
51+
$finder = new NodeFinder();
52+
$enum = $finder->findFirst($ast, function(Node $node){
53+
return $node instanceof Enum_ && $node->name->toString() === 'Suit';
54+
});
4555
$doc = new PhpDocComment($enum);
4656

4757
$this->assertSame('スート', $doc->getDescription(), 'description');
@@ -56,9 +66,13 @@ public function testDocStringMultiLines(): void
5666
} catch (Error $error) {
5767
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
5868
}
59-
60-
// var_dump($ast[0]->stmts[0]);
61-
$enumCase = $ast[0]->stmts[0]->stmts[3];
69+
$finder = new NodeFinder();
70+
$enum = $finder->findFirst($ast, function(Node $node){
71+
return $node instanceof Enum_ && $node->name->toString() === 'Suit';
72+
});
73+
$enumCase = $finder->findFirst($enum, function(Node $node){
74+
return $node instanceof EnumCase && $node->name->toString() === 'Spades';
75+
});
6276
$doc = new PhpDocComment($enumCase);
6377

6478
$this->assertSame("スペード\n説明コメント", $doc->getText(), 'multiline comment string');
@@ -73,9 +87,14 @@ public function test_getDescription(): void
7387
} catch (Error $error) {
7488
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
7589
}
90+
$finder = new NodeFinder();
91+
$enum = $finder->findFirst($ast, function(Node $node){
92+
return $node instanceof Enum_ && $node->name->toString() === 'Suit';
93+
});
94+
$enumCase = $finder->findFirst($enum, function(Node $node){
95+
return $node instanceof EnumCase && $node->name->toString() === 'Spades';
96+
});
7697

77-
// var_dump($ast[0]->stmts[0]);
78-
$enumCase = $ast[0]->stmts[0]->stmts[3];
7998
$doc = new PhpDocComment($enumCase);
8099

81100
$this->assertSame("スペード", $doc->getDescription(), 'description string');
@@ -90,9 +109,11 @@ public function test_getVarType(): void
90109
} catch (Error $error) {
91110
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
92111
}
112+
$finder = new NodeFinder();
113+
$var = $finder->findFirst($ast, function(Node $node){
114+
return $node instanceof Property && $node->props[0]->name->toString() === 'name';
115+
});
93116

94-
// var_dump($ast[0]->stmts[1]->stmts[0]);
95-
$var = $ast[0]->stmts[1]->stmts[0];
96117
$doc = new PhpDocComment($var);
97118

98119
$this->assertSame("Name", $doc->getVarTypeName(), 'var type name.');
@@ -106,9 +127,10 @@ public function test_getParamType(): void
106127
} catch (Error $error) {
107128
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
108129
}
109-
110-
// var_dump($ast[0]->stmts[2]->stmts[10]);
111-
$method = $ast[0]->stmts[2]->stmts[10];
130+
$finder = new NodeFinder();
131+
$method = $finder->findFirst($ast, function(Node $node){
132+
return $node instanceof ClassMethod && $node->name->toString() === 'method1';
133+
});
112134
$doc = new PhpDocComment($method);
113135

114136
$this->assertSame("string|int", $doc->getParamTypeName('param1'), 'param type name.');
@@ -122,9 +144,11 @@ public function test_getReturnType(): void
122144
} catch (Error $error) {
123145
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
124146
}
147+
$finder = new NodeFinder();
148+
$method = $finder->findFirst($ast, function(Node $node){
149+
return $node instanceof ClassMethod && $node->name->toString() === 'method1';
150+
});
125151

126-
// var_dump($ast[0]->stmts[2]->stmts[10]);
127-
$method = $ast[0]->stmts[2]->stmts[10];
128152
$doc = new PhpDocComment($method);
129153

130154
$this->assertSame("int|null", $doc->getReturnTypeName(), 'return type name.');
@@ -138,8 +162,11 @@ public function test_getClassComment(): void
138162
} catch (Error $error) {
139163
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
140164
}
165+
$finder = new NodeFinder();
166+
$class = $finder->findFirst($ast, function(Node $node){
167+
return $node instanceof Class_ && $node->name->toString() === 'Product';
168+
});
141169

142-
$class = $ast[0]->stmts[2];
143170
$doc = new PhpDocComment($class);
144171

145172
$this->assertSame('', $doc->getDescription(), 'class type name.');

0 commit comments

Comments
 (0)