Skip to content

Commit

Permalink
[TypeDeclaration] Skip changed by ref from trait on TypedPropertyFrom…
Browse files Browse the repository at this point in the history
…StrictConstructorRector (#6666)

* [TypeDeclaration] Skip changed by ref from trait on TypedPropertyFromStrictConstructorRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Jan 11, 2025
1 parent d347dd3 commit 9d81eba
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

use Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Source\ByRefTrait;

final class SkipByRefAssignOnTrait
{
use ByRefTrait;

private $str;

public function __construct(string $str)
{
$this->str = $str;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

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

trait ByRefTrait
{
public function run()
{
$str = &$this->str;
$str = null;
}
}
6 changes: 2 additions & 4 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer;
use Rector\Enum\ObjectReference;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\ContextAnalyzer;
Expand All @@ -44,8 +43,7 @@ public function __construct(
private AstResolver $astResolver,
private NodeTypeResolver $nodeTypeResolver,
private ReflectionResolver $reflectionResolver,
private ContextAnalyzer $contextAnalyzer,
private PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer
private ContextAnalyzer $contextAnalyzer
) {
}

Expand Down Expand Up @@ -128,7 +126,7 @@ function (Node $node) use ($propertyName): bool {
return true;
}

return $this->propertyWriteonlyAnalyzer->arePropertyFetchesExclusivelyBeingAssignedTo([$node]);
return $this->contextAnalyzer->isLeftPartOfAssign($node);
}
);
}
Expand Down
20 changes: 4 additions & 16 deletions src/NodeManipulator/AssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PhpParser\Node\FunctionLike;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeNestingScope\ContextAnalyzer;
use Rector\Php72\ValueObject\ListAndEach;
use Rector\PhpParser\Node\BetterNodeFinder;

Expand All @@ -23,7 +23,8 @@
public function __construct(
private NodeNameResolver $nodeNameResolver,
private BetterNodeFinder $betterNodeFinder,
private PropertyFetchAnalyzer $propertyFetchAnalyzer
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
private ContextAnalyzer $contextAnalyzer
) {
}

Expand Down Expand Up @@ -61,19 +62,6 @@ public function matchListAndEach(Assign $assign): ?ListAndEach
return new ListAndEach($assign->var, $bareExpr);
}

public function isLeftPartOfAssign(Node $node): bool
{
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true) {
return true;
}

if ($node->getAttribute(AttributeKey::IS_ASSIGN_REF_EXPR) === true) {
return true;
}

return $node->getAttribute(AttributeKey::IS_ASSIGN_OP_VAR) === true;
}

/**
* @api doctrine
* @return array<PropertyFetch|StaticPropertyFetch>
Expand All @@ -85,7 +73,7 @@ public function resolveAssignsToLocalPropertyFetches(FunctionLike $functionLike)
return false;
}

return $this->isLeftPartOfAssign($node);
return $this->contextAnalyzer->isLeftPartOfAssign($node);
});
}
}
3 changes: 1 addition & 2 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
];

public function __construct(
private AssignManipulator $assignManipulator,
private BetterNodeFinder $betterNodeFinder,
private PhpDocInfoFactory $phpDocInfoFactory,
private PropertyFetchFinder $propertyFetchFinder,
Expand Down Expand Up @@ -91,7 +90,7 @@ public function isPropertyChangeableExceptConstructor(
continue;
}

if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) {
if ($this->contextAnalyzer->isLeftPartOfAssign($propertyFetch)) {
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions src/NodeNestingScope/ContextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ public function isChangeableContext(

return $propertyFetch->getAttribute(AttributeKey::IS_INCREMENT_OR_DECREMENT, false) === true;
}

public function isLeftPartOfAssign(Node $node): bool
{
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true) {
return true;
}

if ($node->getAttribute(AttributeKey::IS_ASSIGN_REF_EXPR) === true) {
return true;
}

return $node->getAttribute(AttributeKey::IS_ASSIGN_OP_VAR) === true;
}
}

0 comments on commit 9d81eba

Please sign in to comment.