@@ -75,16 +75,21 @@ public function parse(TokenIterator $tokens): Ast\PhpDoc\PhpDocNode
7575 $ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_PHPDOC );
7676 } catch (ParserException $ e ) {
7777 $ name = '' ;
78+ $ startLine = $ tokens ->currentTokenLine ();
79+ $ startIndex = $ tokens ->currentTokenIndex ();
7880 if (count ($ children ) > 0 ) {
7981 $ lastChild = $ children [count ($ children ) - 1 ];
8082 if ($ lastChild instanceof Ast \PhpDoc \PhpDocTagNode) {
8183 $ name = $ lastChild ->name ;
84+ $ startLine = $ tokens ->currentTokenLine ();
85+ $ startIndex = $ tokens ->currentTokenIndex ();
8286 }
8387 }
88+
8489 $ tokens ->forwardToTheEnd ();
85- return new Ast \PhpDoc \PhpDocNode ([
86- new Ast \ PhpDoc \ PhpDocTagNode ( $ name , new Ast \ PhpDoc \ InvalidTagValueNode ( $ e -> getMessage (), $ e )),
87- ]);
90+ $ tag = new Ast \PhpDoc \PhpDocTagNode ( $ name , new Ast \ PhpDoc \ InvalidTagValueNode ( $ e -> getMessage (), $ e ));
91+
92+ return new Ast \ PhpDoc \ PhpDocNode ([ $ this -> enrichWithAttributes ( $ tokens , $ tag , $ startLine , $ startIndex ) ]);
8893 }
8994
9095 return new Ast \PhpDoc \PhpDocNode (array_values ($ children ));
@@ -96,40 +101,37 @@ private function parseChild(TokenIterator $tokens): Ast\PhpDoc\PhpDocChildNode
96101 if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_PHPDOC_TAG )) {
97102 $ startLine = $ tokens ->currentTokenLine ();
98103 $ startIndex = $ tokens ->currentTokenIndex ();
99- $ tag = $ this ->parseTag ($ tokens );
100- $ endLine = $ tokens ->currentTokenLine ();
101- $ endIndex = $ tokens ->currentTokenIndex ();
102-
103- if ($ this ->useLinesAttributes ) {
104- $ tag ->setAttribute (Ast \Attribute::START_LINE , $ startLine );
105- $ tag ->setAttribute (Ast \Attribute::END_LINE , $ endLine );
106- }
107-
108- if ($ this ->useIndexAttributes ) {
109- $ tag ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
110- $ tag ->setAttribute (Ast \Attribute::END_INDEX , $ endIndex );
111- }
112-
113- return $ tag ;
104+ return $ this ->enrichWithAttributes ($ tokens , $ this ->parseTag ($ tokens ), $ startLine , $ startIndex );
114105 }
115106
116107 $ startLine = $ tokens ->currentTokenLine ();
117108 $ startIndex = $ tokens ->currentTokenIndex ();
118109 $ text = $ this ->parseText ($ tokens );
110+
111+ return $ this ->enrichWithAttributes ($ tokens , $ text , $ startLine , $ startIndex );
112+ }
113+
114+ /**
115+ * @template T of Ast\Node
116+ * @param T $tag
117+ * @return T
118+ */
119+ private function enrichWithAttributes (TokenIterator $ tokens , Ast \Node $ tag , int $ startLine , int $ startIndex ): Ast \Node
120+ {
119121 $ endLine = $ tokens ->currentTokenLine ();
120122 $ endIndex = $ tokens ->currentTokenIndex ();
121123
122124 if ($ this ->useLinesAttributes ) {
123- $ text ->setAttribute (Ast \Attribute::START_LINE , $ startLine );
124- $ text ->setAttribute (Ast \Attribute::END_LINE , $ endLine );
125+ $ tag ->setAttribute (Ast \Attribute::START_LINE , $ startLine );
126+ $ tag ->setAttribute (Ast \Attribute::END_LINE , $ endLine );
125127 }
126128
127129 if ($ this ->useIndexAttributes ) {
128- $ text ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
129- $ text ->setAttribute (Ast \Attribute::END_INDEX , $ endIndex );
130+ $ tag ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
131+ $ tag ->setAttribute (Ast \Attribute::END_INDEX , $ endIndex );
130132 }
131133
132- return $ text ;
134+ return $ tag ;
133135 }
134136
135137
@@ -302,20 +304,7 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
302304 $ tagValue = new Ast \PhpDoc \InvalidTagValueNode ($ this ->parseOptionalDescription ($ tokens ), $ e );
303305 }
304306
305- $ endLine = $ tokens ->currentTokenLine ();
306- $ endIndex = $ tokens ->currentTokenIndex ();
307-
308- if ($ this ->useLinesAttributes ) {
309- $ tagValue ->setAttribute (Ast \Attribute::START_LINE , $ startLine );
310- $ tagValue ->setAttribute (Ast \Attribute::END_LINE , $ endLine );
311- }
312-
313- if ($ this ->useIndexAttributes ) {
314- $ tagValue ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
315- $ tagValue ->setAttribute (Ast \Attribute::END_INDEX , $ endIndex );
316- }
317-
318- return $ tagValue ;
307+ return $ this ->enrichWithAttributes ($ tokens , $ tagValue , $ startLine , $ startIndex );
319308 }
320309
321310
0 commit comments