From 25f8668ac76d679e8cfdc9b73a8cb28e8011a65b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Jan 2025 22:32:29 +0700 Subject: [PATCH 1/2] [TypeDeclaration] Skip changed by ref from trait on TypedPropertyFromStrictConstructorRector --- .../skip_by_ref_assign_on_trait.php.inc | 17 +++++++++++++++++ .../Source/ByRefTrait.php | 13 +++++++++++++ src/NodeAnalyzer/PropertyFetchAnalyzer.php | 6 ++---- src/NodeManipulator/AssignManipulator.php | 19 ++++--------------- src/NodeManipulator/PropertyManipulator.php | 3 +-- src/NodeNestingScope/ContextAnalyzer.php | 13 +++++++++++++ 6 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Fixture/skip_by_ref_assign_on_trait.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector/Source/ByRefTrait.php 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 00000000000..a807eae706b --- /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 00000000000..499a86ddef1 --- /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 caa16cf4053..771bf507aba 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 a71313d59f4..29740bc81b3 100644 --- a/src/NodeManipulator/AssignManipulator.php +++ b/src/NodeManipulator/AssignManipulator.php @@ -14,6 +14,7 @@ use PhpParser\Node\FunctionLike; use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\NodeNameResolver\NodeNameResolver; +use Rector\NodeNestingScope\ContextAnalyzer; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php72\ValueObject\ListAndEach; use Rector\PhpParser\Node\BetterNodeFinder; @@ -23,7 +24,8 @@ public function __construct( private NodeNameResolver $nodeNameResolver, private BetterNodeFinder $betterNodeFinder, - private PropertyFetchAnalyzer $propertyFetchAnalyzer + private PropertyFetchAnalyzer $propertyFetchAnalyzer, + private ContextAnalyzer $contextAnalyzer ) { } @@ -61,19 +63,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 +74,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 5d640aa53e8..597c5de7030 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 8c0ea278934..bbc38871353 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; + } } From 1decdb5cb640b41b168e02c5fdce79737c058789 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 11 Jan 2025 15:34:27 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- src/NodeManipulator/AssignManipulator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NodeManipulator/AssignManipulator.php b/src/NodeManipulator/AssignManipulator.php index 29740bc81b3..7240eb46b5e 100644 --- a/src/NodeManipulator/AssignManipulator.php +++ b/src/NodeManipulator/AssignManipulator.php @@ -15,7 +15,6 @@ use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeNestingScope\ContextAnalyzer; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php72\ValueObject\ListAndEach; use Rector\PhpParser\Node\BetterNodeFinder;