Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;

use Attribute;
use ReturnTypeWillChange;

#[Attribute]final class AttributeSameLineFromUse
{
#[ReturnTypeWillChange]public function getValue()
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;

use Attribute;
use ReturnTypeWillChange;

#[Attribute]
final class AttributeSameLineFromUse
{
#[ReturnTypeWillChange]
public function getValue()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@

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;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand Down