Skip to content

Commit 44dd0d1

Browse files
committed
[DowngradePhp80] Enhance new line indentation on use statement usage
1 parent 757bfc8 commit 44dd0d1

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-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: 29 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;
@@ -107,22 +110,21 @@ public function refactor(Node $node): ?Node
107110
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
108111
$oldTokens = $this->file->getOldTokens();
109112

113+
$node->getAttribute(AttributeKey::SCOPE);
114+
110115
foreach ($node->attrGroups as $attrGroup) {
111116
foreach ($attrGroup->attrs as $key => $attribute) {
112117
if ($this->shouldSkipAttribute($attribute)) {
113118
if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains(
114119
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
115120
"\n"
116121
)) {
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-
124122
// add new line
125-
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $indentation . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
123+
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $this->resolveIndentation(
124+
$node,
125+
$attrGroup,
126+
$attribute
127+
) . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
126128
$this->isDowngraded = true;
127129
}
128130

@@ -179,6 +181,25 @@ public function configure(array $configuration): void
179181
$this->attributesToAnnotations = $configuration;
180182
}
181183

184+
private function resolveIndentation(Node $node, AttributeGroup $attributeGroup, Attribute $attribute): string
185+
{
186+
$scope = $node->getAttribute(AttributeKey::SCOPE);
187+
188+
if (! $scope instanceof Scope) {
189+
return '';
190+
}
191+
192+
if (
193+
($node->getStartTokenPos() === strlen($attribute->name->toString()) - 2)
194+
|| ($node instanceof Class_ && $scope->isInFirstLevelStatement())
195+
) {
196+
return '';
197+
}
198+
199+
$indent = $attributeGroup->getEndTokenPos() - $node->getStartTokenPos() + 2;
200+
return $indent > 0 ? str_repeat(' ', $indent) : '';
201+
}
202+
182203
private function cleanupEmptyAttrGroups(
183204
ClassMethod | Property | Class_ | Interface_ | Param | Function_ $node
184205
): void {

0 commit comments

Comments
 (0)