From 44dd0d19b00d41b2ca47f88a0d6005cd9d0a9967 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 9 Sep 2025 19:57:03 +0700 Subject: [PATCH 1/2] [DowngradePhp80] Enhance new line indentation on use statement usage --- .../attribute_same_line_from_use.php.inc | 33 +++++++++++++++++ .../DowngradeAttributeToAnnotationRector.php | 37 +++++++++++++++---- 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 rules-tests/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector/Fixture/attribute_same_line_from_use.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector/Fixture/attribute_same_line_from_use.php.inc b/rules-tests/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector/Fixture/attribute_same_line_from_use.php.inc new file mode 100644 index 00000000..2b3847f5 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector/Fixture/attribute_same_line_from_use.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index f4509687..b18cc42b 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -6,12 +6,14 @@ use PhpParser\Node; use PhpParser\Node\Attribute; +use PhpParser\Node\AttributeGroup; use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Property; +use PHPStan\Analyser\Scope; use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; @@ -19,6 +21,7 @@ use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation; use Rector\NodeFactory\DoctrineAnnotationFactory; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -107,6 +110,8 @@ public function refactor(Node $node): ?Node $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); $oldTokens = $this->file->getOldTokens(); + $node->getAttribute(AttributeKey::SCOPE); + foreach ($node->attrGroups as $attrGroup) { foreach ($attrGroup->attrs as $key => $attribute) { if ($this->shouldSkipAttribute($attribute)) { @@ -114,15 +119,12 @@ public function refactor(Node $node): ?Node (string) $oldTokens[$attrGroup->getEndTokenPos() + 1], "\n" )) { - if ($node->getStartTokenPos() === strlen($attribute->name->toString()) - 2) { - $indentation = ''; - } else { - $indent = $attrGroup->getEndTokenPos() - $node->getStartTokenPos() + 2; - $indentation = $indent > 0 ? str_repeat(' ', $indent) : ''; - } - // add new line - $oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $indentation . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text; + $oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $this->resolveIndentation( + $node, + $attrGroup, + $attribute + ) . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text; $this->isDowngraded = true; } @@ -179,6 +181,25 @@ public function configure(array $configuration): void $this->attributesToAnnotations = $configuration; } + private function resolveIndentation(Node $node, AttributeGroup $attributeGroup, Attribute $attribute): string + { + $scope = $node->getAttribute(AttributeKey::SCOPE); + + if (! $scope instanceof Scope) { + return ''; + } + + if ( + ($node->getStartTokenPos() === strlen($attribute->name->toString()) - 2) + || ($node instanceof Class_ && $scope->isInFirstLevelStatement()) + ) { + return ''; + } + + $indent = $attributeGroup->getEndTokenPos() - $node->getStartTokenPos() + 2; + return $indent > 0 ? str_repeat(' ', $indent) : ''; + } + private function cleanupEmptyAttrGroups( ClassMethod | Property | Class_ | Interface_ | Param | Function_ $node ): void { From b2cd1224b31edd0a9e8001aaa90cd571d35679bc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 9 Sep 2025 19:57:45 +0700 Subject: [PATCH 2/2] [DowngradePhp80] Enhance new line indentation on use statement usage --- .../Rector/Class_/DowngradeAttributeToAnnotationRector.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index b18cc42b..d32e757d 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -110,8 +110,6 @@ public function refactor(Node $node): ?Node $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); $oldTokens = $this->file->getOldTokens(); - $node->getAttribute(AttributeKey::SCOPE); - foreach ($node->attrGroups as $attrGroup) { foreach ($attrGroup->attrs as $key => $attribute) { if ($this->shouldSkipAttribute($attribute)) {