5
5
use LogicException ;
6
6
use PHPStan \PhpDocParser \Ast ;
7
7
use PHPStan \PhpDocParser \Lexer \Lexer ;
8
+ use function array_merge ;
8
9
use function in_array ;
9
10
use function str_replace ;
10
11
use function strpos ;
@@ -105,6 +106,18 @@ public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int
105
106
return $ type ;
106
107
}
107
108
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
+
108
121
/** @phpstan-impure */
109
122
private function subParse (TokenIterator $ tokens ): Ast \Type \TypeNode
110
123
{
@@ -770,6 +783,8 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
770
783
$ items = [];
771
784
$ sealed = true ;
772
785
786
+ $ done = false ;
787
+
773
788
do {
774
789
$ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
775
790
@@ -783,10 +798,20 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
783
798
break ;
784
799
}
785
800
786
- $ items [] = $ this ->parseArrayShapeItem ($ tokens );
787
-
801
+ $ item = $ this ->parseArrayShapeItem ($ tokens );
802
+ $ items [] = $ item ;
788
803
$ 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 );
790
815
791
816
$ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
792
817
$ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_CURLY_BRACKET );
0 commit comments