@@ -57,6 +57,12 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
5757 $ type = $ this ->parseIntersection ($ tokens , $ type );
5858 }
5959 }
60+
61+ return $ this ->enrichWithAttributes ($ tokens , $ type , $ startLine , $ startIndex );
62+ }
63+
64+ private function enrichWithAttributes (TokenIterator $ tokens , Ast \Type \TypeNode $ type , int $ startLine , int $ startIndex ): Ast \Type \TypeNode
65+ {
6066 $ endLine = $ tokens ->currentTokenLine ();
6167 $ endIndex = $ tokens ->currentTokenIndex ();
6268
@@ -76,6 +82,9 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
7682 /** @phpstan-impure */
7783 private function subParse (TokenIterator $ tokens ): Ast \Type \TypeNode
7884 {
85+ $ startLine = $ tokens ->currentTokenLine ();
86+ $ startIndex = $ tokens ->currentTokenIndex ();
87+
7988 if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_NULLABLE )) {
8089 $ type = $ this ->parseNullable ($ tokens );
8190
@@ -99,13 +108,16 @@ private function subParse(TokenIterator $tokens): Ast\Type\TypeNode
99108 }
100109 }
101110
102- return $ type ;
111+ return $ this -> enrichWithAttributes ( $ tokens , $ type, $ startLine , $ startIndex ) ;
103112 }
104113
105114
106115 /** @phpstan-impure */
107116 private function parseAtomic (TokenIterator $ tokens ): Ast \Type \TypeNode
108117 {
118+ $ startLine = $ tokens ->currentTokenLine ();
119+ $ startIndex = $ tokens ->currentTokenIndex ();
120+
109121 if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_OPEN_PARENTHESES )) {
110122 $ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
111123 $ type = $ this ->subParse ($ tokens );
@@ -114,20 +126,20 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
114126 $ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_PARENTHESES );
115127
116128 if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
117- return $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ type );
129+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ type );
118130 }
119131
120- return $ type ;
132+ return $ this -> enrichWithAttributes ( $ tokens , $ type, $ startLine , $ startIndex ) ;
121133 }
122134
123135 if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_THIS_VARIABLE )) {
124136 $ type = new Ast \Type \ThisTypeNode ();
125137
126138 if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
127- return $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ type );
139+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ type );
128140 }
129141
130- return $ type ;
142+ return $ this -> enrichWithAttributes ( $ tokens , $ type, $ startLine , $ startIndex ) ;
131143 }
132144
133145 $ currentTokenValue = $ tokens ->currentTokenValue ();
@@ -143,7 +155,7 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
143155 $ isHtml = $ this ->isHtml ($ tokens );
144156 $ tokens ->rollback ();
145157 if ($ isHtml ) {
146- return $ type ;
158+ return $ this -> enrichWithAttributes ( $ tokens , $ type, $ startLine , $ startIndex ) ;
147159 }
148160
149161 $ type = $ this ->parseGeneric ($ tokens , $ type );
@@ -169,7 +181,7 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
169181 }
170182 }
171183
172- return $ type ;
184+ return $ this -> enrichWithAttributes ( $ tokens , $ type, $ startLine , $ startIndex ) ;
173185 } else {
174186 $ tokens ->rollback (); // because of ConstFetchNode
175187 }
@@ -196,7 +208,7 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
196208 throw $ exception ;
197209 }
198210
199- return new Ast \Type \ConstTypeNode ($ constExpr );
211+ return $ this -> enrichWithAttributes ( $ tokens , new Ast \Type \ConstTypeNode ($ constExpr), $ startLine , $ startIndex );
200212 } catch (LogicException $ e ) {
201213 throw $ exception ;
202214 }
0 commit comments