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
Expand Up @@ -20,6 +20,9 @@ namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyR
class SomeClass
{
public function __construct(
/**
* @readonly
*/
public string $foo = 'foo'
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyR
class ReadonlyImplicitPropertyPromotion
{
public function __construct(
/**
* @readonly
*/
public string $foo = 'foo'
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -40,7 +43,7 @@ public function __construct(
*/
public function getNodeTypes(): array
{
return [Property::class, Param::class];
return [Property::class, ClassMethod::class];
}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -81,32 +84,60 @@ public function __construct()
}

/**
* @param Property|Param $node
* @param Property|ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->visibilityManipulator->isReadonly($node)) {
if ($node instanceof Property) {
if (! $this->visibilityManipulator->isReadonly($node)) {
return null;
}

$this->addPhpDocTag($node);
$this->visibilityManipulator->removeReadonly($node);
return $node;
}

if (! $this->isName($node, MethodName::CONSTRUCT)) {
return null;
}

if ($node instanceof Property) {
$this->addPhpDocTag($node);
$hasChangedDoc = false;
$hasChanged = false;
foreach ($node->params as $param) {
if (! $this->visibilityManipulator->isReadonly($param)) {
continue;
}

if ($this->addPhpDocTag($param)) {
$hasChangedDoc = true;
}

$this->visibilityManipulator->removeReadonly($param);
$hasChanged = true;
}

if (! $hasChanged) {
return null;
}

$this->visibilityManipulator->removeReadonly($node);
if ($hasChangedDoc) {
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check if this can be tweaked without re-print the whole ClassMethod.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking reprint should not happen on whole class, less pretty, but should be safer. I will create PR for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return $node;
}

private function addPhpDocTag(Property $property): void
private function addPhpDocTag(Property|Param $node): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

if ($phpDocInfo->hasByName(self::TAGNAME)) {
return;
return false;
}

$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode('')));
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ namespace Rector\Tests\Issues\DowngradeReadonlyClassPropertyToPhp80\Fixture;

final class Fixture
{
public function __construct(public array $property)
public function __construct(
/**
* @readonly
*/
public array $property
)
{
}
}
Expand Down