@@ -456,4 +456,84 @@ public function testMethodReturnObjectArray(): void
456456 $ this ->assertSame ('Tag[] ' , $ types [0 ]->getName (), 'name ' );
457457 $ this ->assertSame (false , $ types [0 ]->getNullable (), 'nullable ' );
458458 }
459+ public function testVarArrayAlternative (): void
460+ {
461+ // /** @var array<int, Tag> 付与されたタグ一覧 */
462+ $ parser = (new ParserFactory )->create (ParserFactory::PREFER_PHP7 );
463+ $ filename = sprintf ('%s/phpdoc/product/Product.php ' , $ this ->fixtureDir );
464+ try {
465+ $ ast = $ parser ->parse (file_get_contents ($ filename ));
466+ } catch (Error $ error ) {
467+ throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
468+ }
469+ $ finder = new NodeFinder ();
470+ $ target = $ finder ->findFirst ($ ast , function (Node $ node ){
471+ return $ node instanceof Property && $ node ->props [0 ]->name ->toString () === 'alternativeTags ' ;
472+ });
473+ $ expression = PhpTypeExpression::buildByVar ($ target , ['hoge ' , 'fuga ' , 'product ' ], []);
474+ $ types = $ expression ->getTypes ();
475+
476+ $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' ], $ types [0 ]->getNamespace (), 'namespace ' );
477+ $ this ->assertSame ('array<int, Tag> ' , $ types [0 ]->getName (), 'name ' );
478+ $ this ->assertSame (false , $ types [0 ]->getNullable (), 'nullable ' );
479+
480+ }
481+ public function testMethodParameterAltaernativeTag (): void
482+ {
483+ // /**
484+ // * @param array<int, Tag> $tags tags
485+ // * @return array<int, Tag> tags
486+ // */
487+ // public function arrayTags(array $tags): array
488+ $ parser = (new ParserFactory )->create (ParserFactory::PREFER_PHP7 );
489+ $ filename = sprintf ('%s/phpdoc/product/Product.php ' , $ this ->fixtureDir );
490+ try {
491+ $ ast = $ parser ->parse (file_get_contents ($ filename ));
492+ } catch (Error $ error ) {
493+ throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
494+ }
495+ $ finder = new NodeFinder ();
496+ $ method = $ finder ->findFirst ($ ast , function (Node $ node ){
497+ return $ node instanceof ClassMethod && $ node ->name ->toString () === 'arrayTags ' ;
498+ });
499+ $ param = $ finder ->findFirst ($ method , function (Node $ node ) {
500+ return $ node instanceof Param && $ node ->var ->name === 'tags ' ;
501+ });
502+ $ uses = [new PhpType (['hoge ' , 'fuga ' , 'product ' ], '' , 'Tag ' )];
503+ $ expression = PhpTypeExpression::buildByMethodParam ($ param , ['hoge ' , 'fuga ' , 'product ' ], $ method , 'tags ' , $ uses );
504+ $ types = $ expression ->getTypes ();
505+
506+ $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' ], $ types [0 ]->getNamespace (), 'namespace ' );
507+ $ this ->assertSame ('array<int, Tag> ' , $ types [0 ]->getName (), 'name ' );
508+ $ this ->assertSame (false , $ types [0 ]->getNullable (), 'nullable ' );
509+ }
510+ public function testMethodReturnAltaernativeTag (): void
511+ {
512+ // /**
513+ // * @param array<int, Tag> $tags tags
514+ // * @return array<int, Tag> tags
515+ // */
516+ // public function arrayTags(array $tags): array
517+ $ parser = (new ParserFactory )->create (ParserFactory::PREFER_PHP7 );
518+ $ filename = sprintf ('%s/phpdoc/product/Product.php ' , $ this ->fixtureDir );
519+ try {
520+ $ ast = $ parser ->parse (file_get_contents ($ filename ));
521+ } catch (Error $ error ) {
522+ throw new \Exception ("Parse error: {$ error ->getMessage ()} file: {$ filename }\n" );
523+ }
524+ $ finder = new NodeFinder ();
525+ $ method = $ finder ->findFirst ($ ast , function (Node $ node ){
526+ return $ node instanceof ClassMethod && $ node ->name ->toString () === 'arrayTags ' ;
527+ });
528+ $ param = $ finder ->findFirst ($ method , function (Node $ node ) {
529+ return $ node instanceof Param && $ node ->var ->name === 'tags ' ;
530+ });
531+ $ uses = [new PhpType (['hoge ' , 'fuga ' , 'product ' ], '' , 'Tag ' )];
532+ $ expression = PhpTypeExpression::buildByMethodReturn ($ method , ['hoge ' , 'fuga ' , 'product ' ], $ uses );
533+ $ types = $ expression ->getTypes ();
534+
535+ $ this ->assertSame (['hoge ' , 'fuga ' , 'product ' ], $ types [0 ]->getNamespace (), 'namespace ' );
536+ $ this ->assertSame ('array<int, Tag> ' , $ types [0 ]->getName (), 'name ' );
537+ $ this ->assertSame (false , $ types [0 ]->getNullable (), 'nullable ' );
538+ }
459539}
0 commit comments