diff --git a/rules/Php81/NodeManipulator/AttributeGroupNewLiner.php b/rules/Php81/NodeManipulator/AttributeGroupNewLiner.php index bb5cae4116b..7de40a8f287 100644 --- a/rules/Php81/NodeManipulator/AttributeGroupNewLiner.php +++ b/rules/Php81/NodeManipulator/AttributeGroupNewLiner.php @@ -4,15 +4,24 @@ namespace Rector\Php81\NodeManipulator; -use PhpParser\Node\Param; -use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\Property; +use PhpParser\Node; +use PhpParser\Node\AttributeGroup; use Rector\ValueObject\Application\File; +use Webmozart\Assert\Assert; final class AttributeGroupNewLiner { - public function newLine(File $file, Property|Param|Class_ $node): void + public function newLine(File $file, Node $node): void { + $attrGroups = $node->attrGroups ?? []; + + if ($attrGroups === []) { + return; + } + + Assert::allIsAOf($attrGroups, AttributeGroup::class); + Assert::isArray($attrGroups); + $oldTokens = $file->getOldTokens(); $startTokenPos = $node->getStartTokenPos(); @@ -25,13 +34,13 @@ public function newLine(File $file, Property|Param|Class_ $node): void } $iteration = 1; - $lastKey = array_key_last($node->attrGroups); + $lastKey = array_key_last($attrGroups); if ($lastKey === null) { return; } - $lastAttributeTokenPos = $node->attrGroups[$lastKey]->getEndTokenPos(); + $lastAttributeTokenPos = $attrGroups[$lastKey]->getEndTokenPos(); while (isset($oldTokens[$startTokenPos + $iteration])) { if ($startTokenPos + $iteration === $lastAttributeTokenPos) {