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..d32e757d 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; @@ -114,15 +117,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 +179,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 {