Skip to content

Commit af6705b

Browse files
authored
[DowngradePhp80] Enhance new line indentation on use statement usage on DowngradeAttributeToAnnotationRector (#329)
* [DowngradePhp80] Enhance new line indentation on use statement usage * [DowngradePhp80] Enhance new line indentation on use statement usage
1 parent 757bfc8 commit af6705b

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
4+
5+
use Attribute;
6+
use ReturnTypeWillChange;
7+
8+
#[Attribute]final class AttributeSameLineFromUse
9+
{
10+
#[ReturnTypeWillChange]public function getValue()
11+
{
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
20+
21+
use Attribute;
22+
use ReturnTypeWillChange;
23+
24+
#[Attribute]
25+
final class AttributeSameLineFromUse
26+
{
27+
#[ReturnTypeWillChange]
28+
public function getValue()
29+
{
30+
}
31+
}
32+
33+
?>

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Attribute;
9+
use PhpParser\Node\AttributeGroup;
910
use PhpParser\Node\Param;
1011
use PhpParser\Node\Stmt\Class_;
1112
use PhpParser\Node\Stmt\ClassMethod;
1213
use PhpParser\Node\Stmt\Function_;
1314
use PhpParser\Node\Stmt\Interface_;
1415
use PhpParser\Node\Stmt\Property;
16+
use PHPStan\Analyser\Scope;
1517
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
1618
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1719
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1820
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1921
use Rector\Contract\Rector\ConfigurableRectorInterface;
2022
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
2123
use Rector\NodeFactory\DoctrineAnnotationFactory;
24+
use Rector\NodeTypeResolver\Node\AttributeKey;
2225
use Rector\Rector\AbstractRector;
2326
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2427
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -114,15 +117,12 @@ public function refactor(Node $node): ?Node
114117
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
115118
"\n"
116119
)) {
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-
124120
// 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;
126126
$this->isDowngraded = true;
127127
}
128128

@@ -179,6 +179,25 @@ public function configure(array $configuration): void
179179
$this->attributesToAnnotations = $configuration;
180180
}
181181

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+
182201
private function cleanupEmptyAttrGroups(
183202
ClassMethod | Property | Class_ | Interface_ | Param | Function_ $node
184203
): void {

0 commit comments

Comments
 (0)