|
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Attribute; |
| 9 | +use PhpParser\Node\AttributeGroup; |
9 | 10 | use PhpParser\Node\Param; |
10 | 11 | use PhpParser\Node\Stmt\Class_; |
11 | 12 | use PhpParser\Node\Stmt\ClassMethod; |
12 | 13 | use PhpParser\Node\Stmt\Function_; |
13 | 14 | use PhpParser\Node\Stmt\Interface_; |
14 | 15 | use PhpParser\Node\Stmt\Property; |
| 16 | +use PHPStan\Analyser\Scope; |
15 | 17 | use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; |
16 | 18 | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
17 | 19 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
18 | 20 | use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
19 | 21 | use Rector\Contract\Rector\ConfigurableRectorInterface; |
20 | 22 | use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation; |
21 | 23 | use Rector\NodeFactory\DoctrineAnnotationFactory; |
| 24 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
22 | 25 | use Rector\Rector\AbstractRector; |
23 | 26 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; |
24 | 27 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
@@ -114,15 +117,12 @@ public function refactor(Node $node): ?Node |
114 | 117 | (string) $oldTokens[$attrGroup->getEndTokenPos() + 1], |
115 | 118 | "\n" |
116 | 119 | )) { |
117 | | - if ($node->getStartTokenPos() === strlen($attribute->name->toString()) - 2) { |
118 | | - $indentation = ''; |
119 | | - } else { |
120 | | - $indent = $attrGroup->getEndTokenPos() - $node->getStartTokenPos() + 2; |
121 | | - $indentation = $indent > 0 ? str_repeat(' ', $indent) : ''; |
122 | | - } |
123 | | - |
124 | 120 | // add new line |
125 | | - $oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $indentation . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text; |
| 121 | + $oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $this->resolveIndentation( |
| 122 | + $node, |
| 123 | + $attrGroup, |
| 124 | + $attribute |
| 125 | + ) . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text; |
126 | 126 | $this->isDowngraded = true; |
127 | 127 | } |
128 | 128 |
|
@@ -179,6 +179,25 @@ public function configure(array $configuration): void |
179 | 179 | $this->attributesToAnnotations = $configuration; |
180 | 180 | } |
181 | 181 |
|
| 182 | + private function resolveIndentation(Node $node, AttributeGroup $attributeGroup, Attribute $attribute): string |
| 183 | + { |
| 184 | + $scope = $node->getAttribute(AttributeKey::SCOPE); |
| 185 | + |
| 186 | + if (! $scope instanceof Scope) { |
| 187 | + return ''; |
| 188 | + } |
| 189 | + |
| 190 | + if ( |
| 191 | + ($node->getStartTokenPos() === strlen($attribute->name->toString()) - 2) |
| 192 | + || ($node instanceof Class_ && $scope->isInFirstLevelStatement()) |
| 193 | + ) { |
| 194 | + return ''; |
| 195 | + } |
| 196 | + |
| 197 | + $indent = $attributeGroup->getEndTokenPos() - $node->getStartTokenPos() + 2; |
| 198 | + return $indent > 0 ? str_repeat(' ', $indent) : ''; |
| 199 | + } |
| 200 | + |
182 | 201 | private function cleanupEmptyAttrGroups( |
183 | 202 | ClassMethod | Property | Class_ | Interface_ | Param | Function_ $node |
184 | 203 | ): void { |
|
0 commit comments