Skip to content

Commit

Permalink
[Php81] Skip named constructor on ReadOnlyPropertyRector (#4359)
Browse files Browse the repository at this point in the history
* Add fixture for regression test against ReadOnlyPropertyRector

This should be used to fix #8012

* rename fixture

* FIx

* FIx

---------

Co-authored-by: Alessandro Lai <alessandro.lai85@gmail.com>
  • Loading branch information
samsonasik and Jean85 committed Jun 27, 2023
1 parent d630cc6 commit 5e20015
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

class SkipAssignedInNamedConstructor
{
private \DateTimeInterface $date;

public function __construct() {
$this->date = new \DateTime();
}

public function getDate(): \DateTimeInterface
{
return $this->date;
}

public static function create(\DateTimeInterface $date): self
{
$ack = new self();
$ack->date = $date;

return $ack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private function shouldSkipClass(Class_ $class): bool

private function removePropertyAssigns(Class_ $class, string $propertyName): void
{
$this->traverseNodesWithCallable($class, function (Node $node) use ($propertyName): ?int {
$this->traverseNodesWithCallable($class, function (Node $node) use ($class, $propertyName): ?int {
if (! $node instanceof Expression) {
return null;
}
Expand All @@ -163,7 +163,7 @@ private function removePropertyAssigns(Class_ $class, string $propertyName): voi
}

$assign = $node->expr;
if (! $this->propertyFetchFinder->isLocalPropertyFetchByName($assign->var, $propertyName)) {
if (! $this->propertyFetchFinder->isLocalPropertyFetchByName($assign->var, $class, $propertyName)) {
return null;
}

Expand Down
15 changes: 12 additions & 3 deletions src/PhpParser/NodeFinder/PropertyFetchFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

final class PropertyFetchFinder
{
Expand Down Expand Up @@ -120,7 +121,7 @@ function (Node $subNode) use (&$propertyArrayDimFetches, $propertyName) {
return $propertyArrayDimFetches;
}

public function isLocalPropertyFetchByName(Expr $expr, string $propertyName): bool
public function isLocalPropertyFetchByName(Expr $expr, Class_|Trait_ $class, string $propertyName): bool
{
if (! $expr instanceof PropertyFetch) {
return false;
Expand All @@ -130,7 +131,15 @@ public function isLocalPropertyFetchByName(Expr $expr, string $propertyName): bo
return false;
}

return $this->nodeNameResolver->isName($expr->var, 'this');
if ($this->nodeNameResolver->isName($expr->var, 'this')) {
return true;
}

$type = $this->nodeTypeResolver->getType($expr->var);
return $type instanceof FullyQualifiedObjectType && $this->nodeNameResolver->isName(
$class,
$type->getClassName()
);
}

/**
Expand Down Expand Up @@ -187,7 +196,7 @@ private function isNamePropertyNameEquals(
): bool {
// early check if property fetch name is not equals with property name
// so next check is check var name and var type only
if (! $this->isLocalPropertyFetchByName($propertyFetch, $propertyName)) {
if (! $this->isLocalPropertyFetchByName($propertyFetch, $class, $propertyName)) {
return false;
}

Expand Down

0 comments on commit 5e20015

Please sign in to comment.