diff --git a/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Fixture/skip_by_ref_assign_on_trait.php.inc b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Fixture/skip_by_ref_assign_on_trait.php.inc new file mode 100644 index 0000000000..a807eae706 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Fixture/skip_by_ref_assign_on_trait.php.inc @@ -0,0 +1,17 @@ +str = $str; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Source/ByRefTrait.php b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Source/ByRefTrait.php new file mode 100644 index 0000000000..499a86ddef --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Source/ByRefTrait.php @@ -0,0 +1,13 @@ +str; + $str = null; + } +} diff --git a/src/NodeAnalyzer/PropertyFetchAnalyzer.php b/src/NodeAnalyzer/PropertyFetchAnalyzer.php index caa16cf405..771bf507ab 100644 --- a/src/NodeAnalyzer/PropertyFetchAnalyzer.php +++ b/src/NodeAnalyzer/PropertyFetchAnalyzer.php @@ -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; @@ -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 ) { } @@ -128,7 +126,7 @@ function (Node $node) use ($propertyName): bool { return true; } - return $this->propertyWriteonlyAnalyzer->arePropertyFetchesExclusivelyBeingAssignedTo([$node]); + return $this->contextAnalyzer->isLeftPartOfAssign($node); } ); } diff --git a/src/NodeManipulator/AssignManipulator.php b/src/NodeManipulator/AssignManipulator.php index a71313d59f..7240eb46b5 100644 --- a/src/NodeManipulator/AssignManipulator.php +++ b/src/NodeManipulator/AssignManipulator.php @@ -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; @@ -23,7 +23,8 @@ public function __construct( private NodeNameResolver $nodeNameResolver, private BetterNodeFinder $betterNodeFinder, - private PropertyFetchAnalyzer $propertyFetchAnalyzer + private PropertyFetchAnalyzer $propertyFetchAnalyzer, + private ContextAnalyzer $contextAnalyzer ) { } @@ -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 @@ -85,7 +73,7 @@ public function resolveAssignsToLocalPropertyFetches(FunctionLike $functionLike) return false; } - return $this->isLeftPartOfAssign($node); + return $this->contextAnalyzer->isLeftPartOfAssign($node); }); } } diff --git a/src/NodeManipulator/PropertyManipulator.php b/src/NodeManipulator/PropertyManipulator.php index 5d640aa53e..597c5de703 100644 --- a/src/NodeManipulator/PropertyManipulator.php +++ b/src/NodeManipulator/PropertyManipulator.php @@ -50,7 +50,6 @@ ]; public function __construct( - private AssignManipulator $assignManipulator, private BetterNodeFinder $betterNodeFinder, private PhpDocInfoFactory $phpDocInfoFactory, private PropertyFetchFinder $propertyFetchFinder, @@ -91,7 +90,7 @@ public function isPropertyChangeableExceptConstructor( continue; } - if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) { + if ($this->contextAnalyzer->isLeftPartOfAssign($propertyFetch)) { return true; } diff --git a/src/NodeNestingScope/ContextAnalyzer.php b/src/NodeNestingScope/ContextAnalyzer.php index 8c0ea27893..bbc3887135 100644 --- a/src/NodeNestingScope/ContextAnalyzer.php +++ b/src/NodeNestingScope/ContextAnalyzer.php @@ -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; + } }