@@ -125,6 +125,7 @@ public function enterNode(Node $node): void
125
125
}
126
126
// 设置顶级类
127
127
$ this ->topClassReflection = $ reflection ;
128
+ $ this ->parseClassComments ();
128
129
break ;
129
130
case $ node instanceof Node \Stmt \Use_:
130
131
foreach ($ node ->uses as $ use )
@@ -161,19 +162,45 @@ public function leaveNode(Node $node): ?Node
161
162
162
163
return match (true )
163
164
{
164
- $ node instanceof Node \Stmt \ClassLike => $ this ->generateClass (/* @var $node Node\Stmt\ClassLike */ $ node ),
165
+ $ node instanceof Node \Stmt \ClassLike => $ this ->rewriteClass (/* @var $node Node\Stmt\ClassLike */ $ node ),
165
166
$ node instanceof Node \Stmt \ClassMethod => $ this ->generateClassMethodAttributes (/* @var $node Node\Stmt\ClassMethod */ $ node ),
166
167
default => null ,
167
168
};
168
169
}
169
170
170
- private function generateClass (Node \Stmt \ClassLike $ node ): ?Node \Stmt \ClassLike
171
+ private array $ classCommentLines = [];
172
+
173
+ private function parseClassComments (): void
174
+ {
175
+ $ node = $ this ->currentClass ;
176
+
177
+ $ classCommentDoc = Helper::arrayValueLast ($ node ->getComments ());
178
+ $ classCommentText = $ classCommentDoc ?->getText();
179
+
180
+ $ this ->classCommentLines = $ this ->parseCommentsProperty ($ classCommentText );
181
+ }
182
+
183
+ private function rewriteClass (Node \Stmt \ClassLike $ node ): ?Node \Stmt \ClassLike
171
184
{
172
185
if ($ this ->debug )
173
186
{
174
187
$ this ->logger ->debug ("> Class: {$ node ->name }" );
175
188
}
176
189
190
+ $ classCommentLines = array_filter (
191
+ $ this ->classCommentLines ,
192
+ static fn ($ line ) => !('property ' !== $ line ['kind ' ] && (str_contains ($ line ['raw ' ], '@Annotation ' ) || str_contains ($ line ['raw ' ], '@Target ' ))),
193
+ );
194
+ if ($ this ->handleCode ->isModified () || $ this ->classCommentLines !== $ classCommentLines )
195
+ {
196
+ $ classCommentLines = array_column ($ classCommentLines , 'raw ' );
197
+
198
+ $ newClassCommentText = "/** \n" . implode ("\n" , $ classCommentLines ) . "\n */ " ;
199
+ $ node ->setDocComment (new Doc ($ newClassCommentText ));
200
+
201
+ $ this ->handleCode ->setModified ();
202
+ }
203
+
177
204
$ newStmts = [];
178
205
foreach ($ node ->stmts as $ stmt )
179
206
{
@@ -263,33 +290,27 @@ protected function migrationConstruct(Node\Stmt\ClassMethod $node): Node\Stmt\Cl
263
290
}
264
291
}
265
292
266
- $ classCommentDoc = Helper::arrayValueLast ($ this ->currentClass ->getComments ());
267
- $ classComments = $ classCommentDoc ?->getText();
293
+ // $classCommentDoc = Helper::arrayValueLast($this->currentClass->getComments());
294
+ // $classComments = $classCommentDoc?->getText();
268
295
269
296
if ($ isModified )
270
297
{
271
298
// 重写类属性注解
272
- $ classCommentsLines = $ this ->parseCommentsProperty ($ classComments );
273
- $ newClassComments = $ this ->refactorCommentsProperty ($ classCommentsLines , $ newParams );
299
+ $ this ->classCommentLines = $ this ->refactorCommentsProperty ($ this ->classCommentLines , $ newParams );
274
300
275
- if ($ newClassComments !== $ classComments )
301
+ // 重写方法注解
302
+ $ methodCommentDoc = Helper::arrayValueLast ($ node ->getComments ());
303
+ $ methodComments = $ methodCommentDoc ?->getText();
304
+ if ($ methodComments )
276
305
{
277
- $ this ->currentClass ->setDocComment (new Doc ($ newClassComments ));
278
-
279
- // 重写方法注解
280
- $ methodCommentDoc = Helper::arrayValueLast ($ node ->getComments ());
281
- $ methodComments = $ methodCommentDoc ?->getText();
282
- if ($ methodComments )
306
+ $ methodComments = $ this ->cleanAnnotationFromComments ($ methodComments , $ newParams );
307
+ if (self ::isEmptyComments (explode ("\n" , $ methodComments )))
283
308
{
284
- $ methodComments = $ this ->cleanAnnotationFromComments ($ methodComments , $ newParams );
285
- if (self ::isEmptyComments (explode ("\n" , $ methodComments )))
286
- {
287
- $ node ->setDocComment (new Doc ('' ));
288
- }
289
- else
290
- {
291
- $ node ->setDocComment (new Doc ($ methodComments ));
292
- }
309
+ $ node ->setDocComment (new Doc ('' ));
310
+ }
311
+ else
312
+ {
313
+ $ node ->setDocComment (new Doc ($ methodComments ));
293
314
}
294
315
}
295
316
@@ -371,7 +392,7 @@ protected function parseCommentsProperty(string $comments): array
371
392
* }> $comments
372
393
* @param array<Node\Param> $props
373
394
*/
374
- protected function refactorCommentsProperty (array $ comments , array $ props ): ? string
395
+ protected function refactorCommentsProperty (array $ comments , array $ props ): array
375
396
{
376
397
$ classComments = [];
377
398
@@ -386,20 +407,14 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str
386
407
{
387
408
if ('property ' !== $ comment ['kind ' ])
388
409
{
389
- if (
390
- str_contains ($ comment ['raw ' ], '@Annotation ' )
391
- || str_contains ($ comment ['raw ' ], '@Target ' )
392
- ) {
393
- continue ;
394
- }
395
- $ classComments [] = $ comment ['raw ' ];
410
+ $ classComments [] = $ comment ;
396
411
continue ;
397
412
}
398
413
$ meta = $ comment ['meta ' ];
399
414
$ prop = $ propsMap [$ meta ['name ' ]] ?? null ;
400
415
if (empty ($ prop ))
401
416
{
402
- $ classComments [] = $ comment[ ' raw ' ] ;
417
+ $ classComments [] = $ comment ;
403
418
continue ;
404
419
}
405
420
$ commentPropMap [$ meta ['name ' ]] = $ comment ;
@@ -432,7 +447,7 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str
432
447
}
433
448
}
434
449
435
- return " /** \n" . implode ( "\n" , $ classComments) . "\n */ " ;
450
+ return $ classComments ;
436
451
}
437
452
438
453
/**
@@ -464,6 +479,6 @@ protected function cleanAnnotationFromComments(string $comments, array $props):
464
479
}
465
480
}
466
481
467
- return "/** \n" . implode ("\n" , $ newComments ) . "\n*/ " ;
482
+ return "/** \n" . implode ("\n" , $ newComments ) . "\n */ " ;
468
483
}
469
484
}
0 commit comments