Skip to content

Commit

Permalink
update: 调整重写顺序依赖问题
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Nov 2, 2023
1 parent e30807b commit a3d72f4
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions src/Visitor/AttributeRewriteVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -371,7 +391,7 @@ protected function parseCommentsProperty(string $comments): array
* }> $comments
* @param array<Node\Param> $props
*/
protected function refactorCommentsProperty(array $comments, array $props): ?string
protected function refactorCommentsProperty(array $comments, array $props): array
{
$classComments = [];

Expand All @@ -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;
Expand Down Expand Up @@ -432,7 +446,7 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str
}
}

return "/**\n" . implode("\n", $classComments) . "\n*/";
return $classComments;
}

/**
Expand Down

0 comments on commit a3d72f4

Please sign in to comment.