Skip to content

Commit

Permalink
[CodingStyle] Skip readonly type property on AddArrayDefaultToArrayPr…
Browse files Browse the repository at this point in the history
…opertyRector (#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>
  • Loading branch information
samsonasik and pionl committed Apr 29, 2022
1 parent b33f8a5 commit 3e685a6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;

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

Expand Down Expand Up @@ -127,6 +129,15 @@ private function collectPropertyNamesWithMissingDefaultArray(Class_ $class): arr
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;
Expand Down

0 comments on commit 3e685a6

Please sign in to comment.