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,42 @@
<?php

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

final class WithAttributeInline
{
#[Serializer\Since(Option::SINCE_20211124)]private string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
-----
<?php

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

final class WithAttributeInline
{
#[Serializer\Since(Option::SINCE_20211124)]
private readonly string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
#[SomeAttribute]
final class ClassWithAttribute
{
private readonly string $property;
private readonly string $property;
}

?>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

#[SomeAttribute]final class ClassWithAttributeInline
{
private readonly string $property;
}

?>
-----
<?php

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

#[SomeAttribute]
final readonly class ClassWithAttributeInline
{
private string $property;
}

?>
57 changes: 57 additions & 0 deletions rules/Php81/NodeManipulator/AttributeGroupNewLiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Rector\Php81\NodeManipulator;

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\ValueObject\Application\File;

final class AttributeGroupNewLiner
{
public function newLine(File $file, Property|Param|Class_ $node): void
{
$oldTokens = $file->getOldTokens();
$startTokenPos = $node->getStartTokenPos();

if (! isset($oldTokens[$startTokenPos])) {
return;
}

if ($oldTokens[$startTokenPos]->text !== '#[') {
return;
}

$iteration = 1;
$lastKey = array_key_last($node->attrGroups);

if ($lastKey === null) {
return;
}

$lastAttributeTokenPos = $node->attrGroups[$lastKey]->getEndTokenPos();

while (isset($oldTokens[$startTokenPos + $iteration])) {
if ($startTokenPos + $iteration === $lastAttributeTokenPos) {
if ($oldTokens[$startTokenPos + $iteration]->text !== ']') {
break;
}

if (trim($oldTokens[$startTokenPos + $iteration + 1]->text ?? '') === '') {
$space = ltrim($oldTokens[$startTokenPos + $iteration + 1]->text ?? '', "\r\n");
} elseif (trim($oldTokens[$startTokenPos - 1]->text ?? '') === '') {
$space = ltrim($oldTokens[$startTokenPos - 1]->text ?? '', "\r\n");
} else {
$space = '';
}

$oldTokens[$startTokenPos + $iteration]->text = "]\n" . $space;
break;
}

++$iteration;
}
}
}
9 changes: 5 additions & 4 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Rector\NodeAnalyzer\ParamAnalyzer;
use Rector\NodeManipulator\PropertyFetchAssignManipulator;
use Rector\NodeManipulator\PropertyManipulator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
Expand All @@ -47,7 +47,8 @@ public function __construct(
private readonly VisibilityManipulator $visibilityManipulator,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockUpdater $docBlockUpdater
private readonly DocBlockUpdater $docBlockUpdater,
private readonly AttributeGroupNewLiner $attributeGroupNewLiner
) {
}

Expand Down Expand Up @@ -174,7 +175,7 @@ private function refactorProperty(Class_ $class, Property $property, Scope $scop

$attributeGroups = $property->attrGroups;
if ($attributeGroups !== []) {
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->attributeGroupNewLiner->newLine($this->file, $property);
}

$this->removeReadOnlyDoc($property);
Expand Down Expand Up @@ -235,7 +236,7 @@ private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $p
}

if ($param->attrGroups !== []) {
$param->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->attributeGroupNewLiner->newLine($this->file, $param);
}

$this->visibilityManipulator->makeReadonly($param);
Expand Down
14 changes: 6 additions & 8 deletions rules/Php82/Rector/Class_/ReadOnlyClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php81\Enum\AttributeName;
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
Expand All @@ -37,7 +37,8 @@ public function __construct(
private readonly ClassAnalyzer $classAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly ReflectionProvider $reflectionProvider
private readonly ReflectionProvider $reflectionProvider,
private readonly AttributeGroupNewLiner $attributeGroupNewLiner
) {
}

Expand Down Expand Up @@ -96,8 +97,7 @@ public function refactor(Node $node): ?Node
$this->visibilityManipulator->removeReadonly($param);

if ($param->attrGroups !== []) {
// invoke reprint with correct newline
$param->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->attributeGroupNewLiner->newLine($this->file, $param);
}
}
}
Expand All @@ -106,14 +106,12 @@ public function refactor(Node $node): ?Node
$this->visibilityManipulator->removeReadonly($property);

if ($property->attrGroups !== []) {
// invoke reprint with correct newline
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->attributeGroupNewLiner->newLine($this->file, $property);
}
}

if ($node->attrGroups !== []) {
// invoke reprint with correct readonly newline
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->attributeGroupNewLiner->newLine($this->file, $node);
}

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -21,6 +21,7 @@ final class FinalizeTestCaseClassRector extends AbstractRector
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly AttributeGroupNewLiner $attributeGroupNewLiner
) {
}

Expand Down Expand Up @@ -85,7 +86,7 @@ public function refactor(Node $node): ?Node
}

if ($node->attrGroups !== []) {
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->attributeGroupNewLiner->newLine($this->file, $node);
}

$this->visibilityManipulator->makeFinal($node);
Expand Down
Loading