diff --git a/src/Visitor/AttributeRewriteVisitor.php b/src/Visitor/AttributeRewriteVisitor.php index e38626f..e51def9 100644 --- a/src/Visitor/AttributeRewriteVisitor.php +++ b/src/Visitor/AttributeRewriteVisitor.php @@ -125,6 +125,7 @@ public function enterNode(Node $node): void } // 设置顶级类 $this->topClassReflection = $reflection; + $this->parseClassComments(); break; case $node instanceof Node\Stmt\Use_: foreach ($node->uses as $use) @@ -161,19 +162,44 @@ public function leaveNode(Node $node): ?Node return match (true) { - $node instanceof Node\Stmt\ClassLike => $this->generateClass(/* @var $node Node\Stmt\ClassLike */ $node), + $node instanceof Node\Stmt\ClassLike => $this->rewriteClass(/* @var $node Node\Stmt\ClassLike */ $node), $node instanceof Node\Stmt\ClassMethod => $this->generateClassMethodAttributes(/* @var $node Node\Stmt\ClassMethod */ $node), default => null, }; } - private function generateClass(Node\Stmt\ClassLike $node): ?Node\Stmt\ClassLike + private array $classCommentLines = []; + + private function parseClassComments(): void + { + $node = $this->currentClass; + + $classCommentDoc = Helper::arrayValueLast($node->getComments()); + $classCommentText = $classCommentDoc?->getText(); + + $this->classCommentLines = $this->parseCommentsProperty($classCommentText); + } + + private function rewriteClass(Node\Stmt\ClassLike $node): ?Node\Stmt\ClassLike { if ($this->debug) { $this->logger->debug("> Class: {$node->name}"); } + if ($this->handleCode->isModified()) { + + $classCommentLines = \array_filter( + $this->classCommentLines, + fn ($line) => !('property' !== $line['kind'] && (str_contains($line['raw'], '@Annotation') || str_contains($line['raw'], '@Target'))), + ); + $classCommentLines = \array_column($classCommentLines, 'raw'); + + $newClassCommentText = "/**\n" . implode("\n", $classCommentLines) . "\n*/"; + $node->setDocComment(new Doc($newClassCommentText)); + } + + $newStmts = []; foreach ($node->stmts as $stmt) { @@ -263,33 +289,27 @@ protected function migrationConstruct(Node\Stmt\ClassMethod $node): Node\Stmt\Cl } } - $classCommentDoc = Helper::arrayValueLast($this->currentClass->getComments()); - $classComments = $classCommentDoc?->getText(); +// $classCommentDoc = Helper::arrayValueLast($this->currentClass->getComments()); +// $classComments = $classCommentDoc?->getText(); if ($isModified) { // 重写类属性注解 - $classCommentsLines = $this->parseCommentsProperty($classComments); - $newClassComments = $this->refactorCommentsProperty($classCommentsLines, $newParams); + $this->classCommentLines = $this->refactorCommentsProperty($this->classCommentLines, $newParams); - if ($newClassComments !== $classComments) + // 重写方法注解 + $methodCommentDoc = Helper::arrayValueLast($node->getComments()); + $methodComments = $methodCommentDoc?->getText(); + if ($methodComments) { - $this->currentClass->setDocComment(new Doc($newClassComments)); - - // 重写方法注解 - $methodCommentDoc = Helper::arrayValueLast($node->getComments()); - $methodComments = $methodCommentDoc?->getText(); - if ($methodComments) + $methodComments = $this->cleanAnnotationFromComments($methodComments, $newParams); + if (self::isEmptyComments(explode("\n", $methodComments))) { - $methodComments = $this->cleanAnnotationFromComments($methodComments, $newParams); - if (self::isEmptyComments(explode("\n", $methodComments))) - { - $node->setDocComment(new Doc('')); - } - else - { - $node->setDocComment(new Doc($methodComments)); - } + $node->setDocComment(new Doc('')); + } + else + { + $node->setDocComment(new Doc($methodComments)); } } @@ -371,7 +391,7 @@ protected function parseCommentsProperty(string $comments): array * }> $comments * @param array $props */ - protected function refactorCommentsProperty(array $comments, array $props): ?string + protected function refactorCommentsProperty(array $comments, array $props): array { $classComments = []; @@ -386,20 +406,14 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str { if ('property' !== $comment['kind']) { - if ( - str_contains($comment['raw'], '@Annotation') - || str_contains($comment['raw'], '@Target') - ) { - continue; - } - $classComments[] = $comment['raw']; + $classComments[] = $comment; continue; } $meta = $comment['meta']; $prop = $propsMap[$meta['name']] ?? null; if (empty($prop)) { - $classComments[] = $comment['raw']; + $classComments[] = $comment; continue; } $commentPropMap[$meta['name']] = $comment; @@ -432,7 +446,7 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str } } - return "/**\n" . implode("\n", $classComments) . "\n*/"; + return $classComments; } /**