[CodingStyle] Skip readonly type property on AddArrayDefaultToArrayPropertyRector (#2196)

* Add failing test fixture for AddArrayDefaultToArrayPropertyRector

# Failing Test for AddArrayDefaultToArrayPropertyRector

Based on https://getrector.org/demo/0cb77e6c-9502-44fa-9ecd-e4d46838e7a2

* Closes #2195

* clean up

Co-authored-by: Martin Kluska <martin@kluska.cz>
This commit is contained in:
Abdul Malik Ikhsan 2022-04-29 20:44:50 +07:00 committed by GitHub
parent b33f8a529d
commit 3e685a63ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Tests\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector\Fixture;
abstract class SkipReadonlyProperty
{
/**
* @var array
*/
public readonly array $changes;
public function __construct(Model $model)
{
$this->changes = $model->getChanges();
}
}
?>

View File

@ -22,6 +22,7 @@ use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -34,7 +35,8 @@ final class AddArrayDefaultToArrayPropertyRector extends AbstractRector
public function __construct(
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly IterableTypeAnalyzer $iterableTypeAnalyzer,
private readonly ArgsAnalyzer $argsAnalyzer
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator
) {
}
@ -127,6 +129,15 @@ CODE_SAMPLE
return null;
}
$property = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $property instanceof Property) {
return null;
}
if ($this->visibilityManipulator->isReadonly($property)) {
return null;
}
$propertyNames[] = $this->getName($node);
return null;