Skip to content

Commit

Permalink
[CodingStyle] Skip filled by construct on AddArrayDefaultToArrayPrope…
Browse files Browse the repository at this point in the history
…rtyRector (#4549)
  • Loading branch information
samsonasik committed Jul 20, 2023
1 parent 0ff50c0 commit e2ec807
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector\Fixture;

final class SkipFilledByConstruct
{
/** @var string[] */
private array $arr;

public function __construct(array $arr)
{
$this->arr = $arr;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -32,7 +33,8 @@ final class AddArrayDefaultToArrayPropertyRector extends AbstractRector
public function __construct(
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly IterableTypeAnalyzer $iterableTypeAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator
private readonly VisibilityManipulator $visibilityManipulator,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
}

Expand Down Expand Up @@ -115,7 +117,7 @@ public function refactor(Node $node): ?Node
private function collectPropertyNamesWithMissingDefaultArray(Class_ $class): array
{
$propertyNames = [];
$this->traverseNodesWithCallable($class, function (Node $node) use (&$propertyNames) {
$this->traverseNodesWithCallable($class, function (Node $node) use ($class, &$propertyNames) {
if (! $node instanceof Property) {
return null;
}
Expand All @@ -134,7 +136,12 @@ private function collectPropertyNamesWithMissingDefaultArray(Class_ $class): arr
return null;
}

$propertyNames[] = $this->getName($propertyProperty);
$propertyName = $this->getName($propertyProperty);
if ($this->constructorAssignDetector->isPropertyAssigned($class, $propertyName)) {
return null;
}

$propertyNames[] = $propertyName;
}

return null;
Expand Down

0 comments on commit e2ec807

Please sign in to comment.