|
4 | 4 |
|
5 | 5 | namespace Rector\BetterPhpDocParser\PhpDocInfo;
|
6 | 6 |
|
| 7 | +use PhpParser\Comment; |
7 | 8 | use PhpParser\Comment\Doc;
|
8 | 9 | use PhpParser\Node;
|
9 | 10 | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
@@ -35,7 +36,7 @@ public function __construct(
|
35 | 36 | private readonly StaticTypeMapper $staticTypeMapper,
|
36 | 37 | private readonly AnnotationNaming $annotationNaming,
|
37 | 38 | private readonly RectorChangeCollector $rectorChangeCollector,
|
38 |
| - private readonly PhpDocNodeByTypeFinder $phpDocNodeByTypeFinder, |
| 39 | + private readonly PhpDocNodeByTypeFinder $phpDocNodeByTypeFinder |
39 | 40 | ) {
|
40 | 41 | }
|
41 | 42 |
|
@@ -76,6 +77,13 @@ public function createFromNode(Node $node): ?PhpDocInfo
|
76 | 77 | $tokenIterator = new BetterTokenIterator([]);
|
77 | 78 | $phpDocNode = new PhpDocNode([]);
|
78 | 79 | } else {
|
| 80 | + $comments = $node->getComments(); |
| 81 | + $docs = array_filter($comments, static fn (Comment $comment): bool => $comment instanceof Doc); |
| 82 | + |
| 83 | + if (count($docs) > 1) { |
| 84 | + $this->storePreviousDocs($node, $comments, $docComment); |
| 85 | + } |
| 86 | + |
79 | 87 | $text = $docComment->getText();
|
80 | 88 | $tokens = $this->lexer->tokenize($text);
|
81 | 89 | $tokenIterator = new BetterTokenIterator($tokens);
|
@@ -107,6 +115,48 @@ public function createEmpty(Node $node): PhpDocInfo
|
107 | 115 | return $phpDocInfo;
|
108 | 116 | }
|
109 | 117 |
|
| 118 | + /** |
| 119 | + * @param Comment[]|Doc[] $comments |
| 120 | + */ |
| 121 | + private function storePreviousDocs(Node $node, array $comments, Doc $doc): void |
| 122 | + { |
| 123 | + $previousDocsAsComments = []; |
| 124 | + $newMainDoc = null; |
| 125 | + |
| 126 | + foreach ($comments as $comment) { |
| 127 | + // On last Doc, stop |
| 128 | + if ($comment === $doc) { |
| 129 | + break; |
| 130 | + } |
| 131 | + |
| 132 | + // pure comment |
| 133 | + if (! $comment instanceof Doc) { |
| 134 | + $previousDocsAsComments[] = $comment; |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + // make Doc as comment Doc that not last |
| 139 | + $previousDocsAsComments[] = new Comment( |
| 140 | + $comment->getText(), |
| 141 | + $comment->getStartLine(), |
| 142 | + $comment->getStartFilePos(), |
| 143 | + $comment->getStartTokenPos(), |
| 144 | + $comment->getEndLine(), |
| 145 | + $comment->getEndFilePos(), |
| 146 | + $comment->getEndTokenPos() |
| 147 | + ); |
| 148 | + |
| 149 | + /** |
| 150 | + * Make last Doc before main Doc to candidate main Doc |
| 151 | + * so it can immediatelly be used as replacement of Main doc when main doc removed |
| 152 | + */ |
| 153 | + $newMainDoc = $comment; |
| 154 | + } |
| 155 | + |
| 156 | + $node->setAttribute(AttributeKey::PREVIOUS_DOCS_AS_COMMENTS, $previousDocsAsComments); |
| 157 | + $node->setAttribute(AttributeKey::NEW_MAIN_DOC, $newMainDoc); |
| 158 | + } |
| 159 | + |
110 | 160 | /**
|
111 | 161 | * Needed for printing
|
112 | 162 | */
|
|
0 commit comments