Skip to content

Commit

Permalink
[Php81] Remove parent attribute on ReadOnlyPropertyRector (#3999)
Browse files Browse the repository at this point in the history
* [Php81] Remove parent attribute on ReadOnlyPropertyRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed May 28, 2023
1 parent 4d4c73d commit 7a739fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
25 changes: 19 additions & 6 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,29 @@ public function getName()
*/
public function getNodeTypes(): array
{
return [Property::class, Param::class];
return [Property::class, ClassMethod::class];
}

/**
* @param Property|Param $node
* @param Property|ClassMethod $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($node instanceof Param) {
return $this->refactorParam($node, $scope);
if ($node instanceof ClassMethod) {
$hasChanged = false;
foreach ($node->params as $param) {
$justChanged = $this->refactorParam($node, $param, $scope);
// different variable to ensure $hasChanged not replaced
if ($justChanged instanceof Param) {
$hasChanged = true;
}
}

if ($hasChanged) {
return $node;
}

return null;
}

return $this->refactorProperty($node, $scope);
Expand Down Expand Up @@ -162,7 +175,7 @@ private function refactorProperty(Property $property, Scope $scope): ?Property
return $property;
}

private function refactorParam(Param $param, Scope $scope): Param | null
private function refactorParam(ClassMethod $classMethod, Param $param, Scope $scope): Param | null
{
if (! $this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
return null;
Expand All @@ -181,7 +194,7 @@ private function refactorParam(Param $param, Scope $scope): Param | null
return null;
}

if ($this->paramAnalyzer->isParamReassign($param)) {
if ($this->paramAnalyzer->isParamReassign($classMethod, $param)) {
return null;
}

Expand Down
9 changes: 1 addition & 8 deletions src/NodeAnalyzer/ParamAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;

final class ParamAnalyzer
Expand Down Expand Up @@ -114,14 +113,8 @@ public function hasDefaultNull(Param $param): bool
return $param->default instanceof ConstFetch && $this->valueResolver->isNull($param->default);
}

public function isParamReassign(Param $param): bool
public function isParamReassign(ClassMethod $classMethod, Param $param): bool
{
$classMethod = $param->getAttribute(AttributeKey::PARENT_NODE);

if (! $classMethod instanceof ClassMethod) {
return false;
}

$paramName = (string) $this->nodeNameResolver->getName($param->var);
return (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($classMethod, function (Node $node) use (
$paramName
Expand Down

0 comments on commit 7a739fd

Please sign in to comment.