22
33use PhpParser \ParserFactory ;
44use PHPUnit \Framework \TestCase ;
5-
5+ use Smeghead \ PhpClassDiagram \ Php \ PhpType ;
66use Smeghead \PhpClassDiagram \Php \PhpTypeExpression ;
77
88final class PhpTypeExpressionTest extends TestCase {
@@ -21,7 +21,7 @@ public function testNullableString(): void {
2121 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
2222 }
2323
24- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [0 ], ['hoge ' , 'fuga ' , 'product ' ]);
24+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [0 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
2525 $ types = $ expression ->getTypes ();
2626
2727 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -37,7 +37,7 @@ public function testIntOrString(): void {
3737 } catch (Error $ error ) {
3838 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
3939 }
40- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [1 ], ['hoge ' , 'fuga ' , 'product ' ]);
40+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [1 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
4141 $ types = $ expression ->getTypes ();
4242
4343 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -56,7 +56,7 @@ public function testPrice(): void {
5656 } catch (Error $ error ) {
5757 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
5858 }
59- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [2 ], ['hoge ' , 'fuga ' , 'product ' ]);
59+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [2 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
6060 $ types = $ expression ->getTypes ();
6161
6262 $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' ], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -72,7 +72,7 @@ public function testException(): void {
7272 } catch (Error $ error ) {
7373 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
7474 }
75- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [4 ], ['hoge ' , 'fuga ' , 'product ' ]);
75+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [4 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
7676 $ types = $ expression ->getTypes ();
7777
7878 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -88,7 +88,7 @@ public function testRelated(): void {
8888 } catch (Error $ error ) {
8989 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
9090 }
91- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [5 ], ['hoge ' , 'fuga ' , 'product ' ]);
91+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [5 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
9292 $ types = $ expression ->getTypes ();
9393
9494 $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' , 'bar ' ], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -104,7 +104,7 @@ public function testAbsolute(): void {
104104 } catch (Error $ error ) {
105105 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
106106 }
107- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [6 ], ['hoge ' , 'fuga ' , 'product ' ]);
107+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [6 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
108108 $ types = $ expression ->getTypes ();
109109
110110 $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' , 'bar ' ], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -121,7 +121,7 @@ public function testDocString(): void {
121121 } catch (Error $ error ) {
122122 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
123123 }
124- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [7 ], ['hoge ' , 'fuga ' , 'product ' ]);
124+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [7 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
125125 $ types = $ expression ->getTypes ();
126126
127127 $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' , 'bur ' ], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -138,7 +138,7 @@ public function testDocStringUnion(): void {
138138 } catch (Error $ error ) {
139139 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
140140 }
141- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [8 ], ['hoge ' , 'fuga ' , 'product ' ]);
141+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [8 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
142142 $ types = $ expression ->getTypes ();
143143
144144 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -158,7 +158,7 @@ public function testDocStringUnion2(): void {
158158 } catch (Error $ error ) {
159159 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
160160 }
161- $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [1 ]->stmts [9 ], ['hoge ' , 'fuga ' , 'product ' ]);
161+ $ expression = PhpTypeExpression::buildByVar ($ ast [0 ]->stmts [2 ]->stmts [9 ], ['hoge ' , 'fuga ' , 'product ' ], [ ]);
162162 $ types = $ expression ->getTypes ();
163163
164164 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -178,10 +178,10 @@ public function testMethodParameterInt(): void {
178178 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
179179 }
180180 // var_dump($ast[0]->stmts[1]->stmts[8]);die();
181- $ doc = $ ast [0 ]->stmts [1 ]->stmts [10 ]->getDocComment ();
181+ $ doc = $ ast [0 ]->stmts [2 ]->stmts [10 ]->getDocComment ();
182182 $ docString = $ doc ->getText ();
183- $ param = $ ast [0 ]->stmts [1 ]->stmts [10 ]->getParams ()[0 ];
184- $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ docString , 'paramint ' );
183+ $ param = $ ast [0 ]->stmts [2 ]->stmts [10 ]->getParams ()[0 ];
184+ $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ docString , 'paramint ' , [] );
185185 $ types = $ expression ->getTypes ();
186186
187187 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -198,10 +198,10 @@ public function testMethodParameterPrice(): void {
198198 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
199199 }
200200 // var_dump($ast[0]->stmts[1]->stmts[8]);die();
201- $ doc = $ ast [0 ]->stmts [1 ]->stmts [10 ]->getDocComment ();
201+ $ doc = $ ast [0 ]->stmts [2 ]->stmts [10 ]->getDocComment ();
202202 $ docString = $ doc ->getText ();
203- $ param = $ ast [0 ]->stmts [1 ]->stmts [10 ]->getParams ()[1 ];
204- $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ docString , 'paramint ' );
203+ $ param = $ ast [0 ]->stmts [2 ]->stmts [10 ]->getParams ()[1 ];
204+ $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ docString , 'paramint ' , [] );
205205 $ types = $ expression ->getTypes ();
206206
207207 $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' ], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -218,10 +218,10 @@ public function testMethodParameterDocString(): void {
218218 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
219219 }
220220 // var_dump($ast[0]->stmts[1]->stmts[8]);die();
221- $ doc = $ ast [0 ]->stmts [1 ]->stmts [10 ]->getDocComment ();
221+ $ doc = $ ast [0 ]->stmts [2 ]->stmts [10 ]->getDocComment ();
222222 $ docString = $ doc ->getText ();
223- $ param = $ ast [0 ]->stmts [1 ]->stmts [10 ]->getParams ()[2 ];
224- $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ docString , 'param1 ' );
223+ $ param = $ ast [0 ]->stmts [2 ]->stmts [10 ]->getParams ()[2 ];
224+ $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ docString , 'param1 ' , [] );
225225 $ types = $ expression ->getTypes ();
226226
227227 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -240,9 +240,9 @@ public function testMethodReturnInt(): void {
240240 } catch (Error $ error ) {
241241 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
242242 }
243- $ method = $ ast [0 ]->stmts [1 ]->stmts [10 ];
243+ $ method = $ ast [0 ]->stmts [2 ]->stmts [10 ];
244244 // var_dump($method);die();
245- $ expression = PhpTypeExpression::buildByMethodReturn ($ method , ['hoge ' , 'fuga ' , 'product ' ]);
245+ $ expression = PhpTypeExpression::buildByMethodReturn ($ method , ['hoge ' , 'fuga ' , 'product ' ], [] );
246246 $ types = $ expression ->getTypes ();
247247
248248 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -258,9 +258,9 @@ public function testMethodReturnProduct(): void {
258258 } catch (Error $ error ) {
259259 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
260260 }
261- $ method = $ ast [0 ]->stmts [1 ]->stmts [11 ];
261+ $ method = $ ast [0 ]->stmts [2 ]->stmts [11 ];
262262 // var_dump($method);die();
263- $ expression = PhpTypeExpression::buildByMethodReturn ($ method , ['hoge ' , 'fuga ' , 'product ' ]);
263+ $ expression = PhpTypeExpression::buildByMethodReturn ($ method , ['hoge ' , 'fuga ' , 'product ' ], [] );
264264 $ types = $ expression ->getTypes ();
265265
266266 $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' ], $ types [0 ]->getNamespace (), 'namespace ' );
@@ -276,14 +276,34 @@ public function testMethodReturnArray(): void {
276276 } catch (Error $ error ) {
277277 throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
278278 }
279- $ method = $ ast [0 ]->stmts [1 ]->stmts [12 ];
279+ $ method = $ ast [0 ]->stmts [2 ]->stmts [12 ];
280280 // var_dump($method);die();
281- $ expression = PhpTypeExpression::buildByMethodReturn ($ method , ['hoge ' , 'fuga ' , 'product ' ]);
281+ $ expression = PhpTypeExpression::buildByMethodReturn ($ method , ['hoge ' , 'fuga ' , 'product ' ], [] );
282282 $ types = $ expression ->getTypes ();
283283
284284 $ this ->assertSame ([], $ types [0 ]->getNamespace (), 'namespace ' );
285285 $ this ->assertSame ('array ' , $ types [0 ]->getName (), 'name ' );
286286 $ this ->assertSame (false , $ types [0 ]->getNullable (), 'nullable ' );
287287 }
288+ public function testMethodParameterTag (): void {
289+ // /** @params string|int $param1 */
290+ $ parser = (new ParserFactory )->create (ParserFactory::PREFER_PHP7 );
291+ $ filename = sprintf ('%s/php8/product/Product.php ' , $ this ->fixtureDir );
292+ try {
293+ $ ast = $ parser ->parse (file_get_contents ($ filename ));
294+ } catch (Error $ error ) {
295+ throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
296+ }
297+ // var_dump($ast[0]->stmts[1]->stmts[8]);die();
298+ $ docString = '' ;
299+ $ param = $ ast [0 ]->stmts [2 ]->stmts [13 ]->getParams ()[0 ];
300+ $ uses = [new PhpType (['hoge ' , 'fuga ' , 'product ' , 'tag ' ], '' , 'Tag ' )];
301+ $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ docString , 'paramint ' , $ uses );
302+ $ types = $ expression ->getTypes ();
303+
304+ $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' , 'tag ' ], $ types [0 ]->getNamespace (), 'namespace ' );
305+ $ this ->assertSame ('Tag ' , $ types [0 ]->getName (), 'name ' );
306+ $ this ->assertSame (false , $ types [0 ]->getNullable (), 'nullable ' );
307+ }
288308
289309}
0 commit comments