@@ -75,16 +75,21 @@ public function parse(TokenIterator $tokens): Ast\PhpDoc\PhpDocNode
75
75
$ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_PHPDOC );
76
76
} catch (ParserException $ e ) {
77
77
$ name = '' ;
78
+ $ startLine = $ tokens ->currentTokenLine ();
79
+ $ startIndex = $ tokens ->currentTokenIndex ();
78
80
if (count ($ children ) > 0 ) {
79
81
$ lastChild = $ children [count ($ children ) - 1 ];
80
82
if ($ lastChild instanceof Ast \PhpDoc \PhpDocTagNode) {
81
83
$ name = $ lastChild ->name ;
84
+ $ startLine = $ tokens ->currentTokenLine ();
85
+ $ startIndex = $ tokens ->currentTokenIndex ();
82
86
}
83
87
}
88
+
84
89
$ 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 ) ]);
88
93
}
89
94
90
95
return new Ast \PhpDoc \PhpDocNode (array_values ($ children ));
@@ -96,40 +101,37 @@ private function parseChild(TokenIterator $tokens): Ast\PhpDoc\PhpDocChildNode
96
101
if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_PHPDOC_TAG )) {
97
102
$ startLine = $ tokens ->currentTokenLine ();
98
103
$ 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 );
114
105
}
115
106
116
107
$ startLine = $ tokens ->currentTokenLine ();
117
108
$ startIndex = $ tokens ->currentTokenIndex ();
118
109
$ 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
+ {
119
121
$ endLine = $ tokens ->currentTokenLine ();
120
122
$ endIndex = $ tokens ->currentTokenIndex ();
121
123
122
124
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 );
125
127
}
126
128
127
129
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 );
130
132
}
131
133
132
- return $ text ;
134
+ return $ tag ;
133
135
}
134
136
135
137
@@ -302,20 +304,7 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
302
304
$ tagValue = new Ast \PhpDoc \InvalidTagValueNode ($ this ->parseOptionalDescription ($ tokens ), $ e );
303
305
}
304
306
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 );
319
308
}
320
309
321
310
0 commit comments