@@ -29,7 +29,11 @@ class TypeParser
29
29
private $ useIndexAttributes ;
30
30
31
31
/**
32
- * @param array{lines?: bool, indexes?: bool, comments?: bool} $usedAttributes
32
+ * @param array{
33
+ * lines?: bool,
34
+ * indexes?: bool,
35
+ * comments?: bool
36
+ * } $usedAttributes
33
37
*/
34
38
public function __construct (
35
39
?ConstExprParser $ constExprParser = null ,
@@ -71,25 +75,25 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
71
75
* @template T of Ast\Node
72
76
* @param T $type
73
77
* @param list<Ast\Comment> $comments
78
+ *
74
79
* @return T
75
80
*/
76
81
public function enrichWithAttributes (TokenIterator $ tokens , Ast \Node $ type , int $ startLine , int $ startIndex , array $ comments = []): Ast \Node
77
82
{
78
- if ($ tokens ->currentTokenType () === Lexer::TOKEN_COMMENT ) {
79
- $ comments [] = new Ast \Comment (
80
- $ tokens ->currentTokenValue (),
81
- $ tokens ->currentTokenLine (),
82
- $ tokens ->currentTokenIndex ()
83
- );
84
- $ tokens ->next ();
85
- }
86
-
87
83
if ($ this ->useLinesAttributes ) {
88
84
$ type ->setAttribute (Ast \Attribute::START_LINE , $ startLine );
89
85
$ type ->setAttribute (Ast \Attribute::END_LINE , $ tokens ->currentTokenLine ());
90
86
}
91
87
92
88
if ($ this ->useCommentsAttributes ) {
89
+ if ($ tokens ->currentTokenType () === Lexer::TOKEN_COMMENT ) {
90
+ $ comments [] = new Ast \Comment (
91
+ $ tokens ->currentTokenValue (),
92
+ $ tokens ->currentTokenLine (),
93
+ $ tokens ->currentTokenIndex ()
94
+ );
95
+ $ tokens ->next ();
96
+ }
93
97
$ type ->setAttribute (Ast \Attribute::COMMENTS , $ comments );
94
98
}
95
99
@@ -754,6 +758,11 @@ private function tryParseArrayOrOffsetAccess(TokenIterator $tokens, Ast\Type\Typ
754
758
return $ type ;
755
759
}
756
760
761
+
762
+ /**
763
+ * @phpstan-impure
764
+ * @param Ast\Type\ArrayShapeNode::KIND_* $kind
765
+ */
757
766
private function parseArrayShape (TokenIterator $ tokens , Ast \Type \TypeNode $ type , string $ kind ): Ast \Type \ArrayShapeNode
758
767
{
759
768
$ tokens ->consumeTokenType (Lexer::TOKEN_OPEN_CURLY_BRACKET );
@@ -785,12 +794,14 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
785
794
return new Ast \Type \ArrayShapeNode ($ items , $ sealed , $ kind );
786
795
}
787
796
797
+
788
798
/** @phpstan-impure */
789
799
private function parseArrayShapeItem (TokenIterator $ tokens ): Ast \Type \ArrayShapeItemNode
790
800
{
791
801
$ startLine = $ tokens ->currentTokenLine ();
792
802
$ startIndex = $ tokens ->currentTokenIndex ();
793
803
804
+ // parse any comments above the item
794
805
$ comments = [];
795
806
while (1 ) {
796
807
if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL )) {
@@ -809,6 +820,12 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
809
820
$ optional = $ tokens ->tryConsumeTokenType (Lexer::TOKEN_NULLABLE );
810
821
$ tokens ->consumeTokenType (Lexer::TOKEN_COLON );
811
822
$ value = $ this ->parse ($ tokens );
823
+
824
+ // parse comment to the right of the item
825
+ if ($ tokens ->currentTokenType () === Lexer::TOKEN_COMMENT ) {
826
+ $ comments [] = new Ast \Comment ($ tokens ->currentTokenValue (), $ tokens ->currentTokenLine (), $ tokens ->currentTokenIndex ());
827
+ $ tokens ->next ();
828
+ }
812
829
$ tokens ->dropSavePoint ();
813
830
814
831
return $ this ->enrichWithAttributes (
0 commit comments