55use LogicException ;
66use PHPStan \PhpDocParser \Ast ;
77use PHPStan \PhpDocParser \Lexer \Lexer ;
8+ use function array_merge ;
89use function in_array ;
910use function str_replace ;
1011use function strpos ;
@@ -105,6 +106,18 @@ public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int
105106 return $ type ;
106107 }
107108
109+ /**
110+ * @param list<Ast\Comment> $comments
111+ */
112+ public function appendComments (Ast \Node $ type , array $ comments = []): Ast \Node
113+ {
114+ if ($ this ->useCommentsAttributes ) {
115+ $ type ->setAttribute (Ast \Attribute::COMMENTS , array_merge ($ comments , $ type ->getAttribute (Ast \Attribute::COMMENTS ) ?? []));
116+ }
117+
118+ return $ type ;
119+ }
120+
108121 /** @phpstan-impure */
109122 private function subParse (TokenIterator $ tokens ): Ast \Type \TypeNode
110123 {
@@ -770,6 +783,8 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
770783 $ items = [];
771784 $ sealed = true ;
772785
786+ $ done = false ;
787+
773788 do {
774789 $ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
775790
@@ -783,10 +798,20 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
783798 break ;
784799 }
785800
786- $ items [] = $ this ->parseArrayShapeItem ($ tokens );
787-
801+ $ item = $ this ->parseArrayShapeItem ($ tokens );
802+ $ items [] = $ item ;
788803 $ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
789- } while ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_COMMA ));
804+ if (!$ tokens ->tryConsumeTokenType (Lexer::TOKEN_COMMA )) {
805+ $ done = true ;
806+ }
807+ if ($ tokens ->currentTokenType () !== Lexer::TOKEN_COMMENT ) {
808+ continue ;
809+ }
810+
811+ $ this ->appendComments ($ item , [new Ast \Comment ($ tokens ->currentTokenValue (), $ tokens ->currentTokenLine (), $ tokens ->currentTokenIndex ())]);
812+ $ tokens ->next ();
813+
814+ } while (!$ done );
790815
791816 $ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
792817 $ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_CURLY_BRACKET );
0 commit comments