Skip to content
Merged
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
21 changes: 15 additions & 6 deletions rules/Php81/NodeManipulator/AttributeGroupNewLiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down
Loading