Skip to content

Commit

Permalink
Fix array type if property is used in TypedPropertyFromStrictConstruc…
Browse files Browse the repository at this point in the history
…torRector (#5339)
  • Loading branch information
TomasVotruba committed Dec 8, 2023
1 parent af55788 commit 0a2dc3d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

final class DefaultArrayMerge
{
private $options = [
'key' => 'value'
];

public function __construct(array $options)
{
$this->options = array_merge($this->options, $options);
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

final class DefaultArrayMerge
{
private array $options = [
'key' => 'value'
];

public function __construct(array $options)
{
$this->options = array_merge($this->options, $options);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PHPStan\Type\ObjectType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
Expand Down Expand Up @@ -65,6 +68,13 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName):

/** @var Assign $assign */
$assign = $node;

// is merged in assign?
if ($this->isPropertyUsedInAssign($assign, $propertyName)) {
$isAssignedInConstructor = false;
return NodeTraverser::STOP_TRAVERSAL;
}

$isFirstLevelStatement = $assign->getAttribute(self::IS_FIRST_LEVEL_STATEMENT);

// cannot be nested
Expand Down Expand Up @@ -178,4 +188,20 @@ private function matchInitializeClassMethod(ClassLike $classLike): array

return $initializingClassMethods;
}

private function isPropertyUsedInAssign(Assign $assign, string $propertyName): bool
{
$nodeFinder = new NodeFinder();
return (bool) $nodeFinder->findFirst($assign->expr, static function (Node $node) use ($propertyName): ?bool {
if (! $node instanceof PropertyFetch) {
return null;
}

if (! $node->name instanceof Identifier) {
return null;
}

return $node->name->toString() === $propertyName;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Type;
use Rector\StaticTypeMapper\StaticTypeMapper;

Expand All @@ -24,6 +25,9 @@ public function doesConflictWithDefaultValue(PropertyProperty $propertyProperty,

// the defaults can be in conflict
$defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyProperty->default);
if ($defaultType instanceof ArrayType && $propertyType instanceof ArrayType) {
return false;
}

// type is not matching, skip it
return ! $defaultType->isSuperTypeOf($propertyType)
Expand Down

0 comments on commit 0a2dc3d

Please sign in to comment.