Skip to content

Commit

Permalink
[Core][Naming] Move collecting param names method to FunctionLikeMani…
Browse files Browse the repository at this point in the history
…pulator (#2347)

* [Core][Naming] Move collecting param names method to FunctionLikeManipulator

* use getParams() as interface

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed May 22, 2022
1 parent 68906c7 commit 8fbc658
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
23 changes: 4 additions & 19 deletions rules/Naming/Naming/ConflictingNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\NodeManipulator\FunctionLikeManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver;
use Rector\Naming\PhpArray\ArrayFilter;
use Rector\NodeNameResolver\NodeNameResolver;

final class ConflictingNameResolver
{
Expand All @@ -24,8 +24,8 @@ public function __construct(
private readonly ArrayFilter $arrayFilter,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ExpectedNameResolver $expectedNameResolver,
private readonly NodeNameResolver $nodeNameResolver,
private readonly MatchParamTypeExpectedNameResolver $matchParamTypeExpectedNameResolver
private readonly MatchParamTypeExpectedNameResolver $matchParamTypeExpectedNameResolver,
private readonly FunctionLikeManipulator $functionLikeManipulator
) {
}

Expand Down Expand Up @@ -67,7 +67,7 @@ private function resolveConflictingVariableNamesForNew(ClassMethod | Function_ |
return $this->conflictingVariableNamesByClassMethod[$classMethodHash];
}

$paramNames = $this->collectParamNames($functionLike);
$paramNames = $this->functionLikeManipulator->resolveParamNames($functionLike);
$newAssignNames = $this->resolveForNewAssigns($functionLike);
$nonNewAssignNames = $this->resolveForNonNewAssigns($functionLike);

Expand All @@ -79,21 +79,6 @@ private function resolveConflictingVariableNamesForNew(ClassMethod | Function_ |
return $protectedNames;
}

/**
* @return string[]
*/
private function collectParamNames(ClassMethod | Function_ | Closure $functionLike): array
{
$paramNames = [];

// params
foreach ($functionLike->params as $param) {
$paramNames[] = $this->nodeNameResolver->getName($param);
}

return $paramNames;
}

/**
* @return string[]
*/
Expand Down
18 changes: 3 additions & 15 deletions src/NodeManipulator/ClassMethodPropertyFetchManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ final class ClassMethodPropertyFetchManipulator
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder
private readonly BetterNodeFinder $betterNodeFinder,
private readonly FunctionLikeManipulator $functionLikeManipulator
) {
}

Expand Down Expand Up @@ -93,7 +94,7 @@ public function findAssignsToPropertyName(ClassMethod $classMethod, string $prop
{
$assignExprs = [];

$paramNames = $this->getParamNames($classMethod);
$paramNames = $this->functionLikeManipulator->resolveParamNames($classMethod);

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
Expand Down Expand Up @@ -123,17 +124,4 @@ function (Node $node) use ($propertyName, &$assignExprs, $paramNames, $classMeth

return $assignExprs;
}

/**
* @return string[]
*/
private function getParamNames(ClassMethod $classMethod): array
{
$paramNames = [];
foreach ($classMethod->getParams() as $param) {
$paramNames[] = $this->nodeNameResolver->getName($param);
}

return $paramNames;
}
}
14 changes: 14 additions & 0 deletions src/NodeManipulator/FunctionLikeManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ public function getReturnedLocalPropertyNames(FunctionLike $functionLike): array

return $returnedLocalPropertyNames;
}

/**
* @return string[]
*/
public function resolveParamNames(FunctionLike $functionLike): array
{
$paramNames = [];

foreach ($functionLike->getParams() as $param) {
$paramNames[] = $this->nodeNameResolver->getName($param);
}

return $paramNames;
}
}

0 comments on commit 8fbc658

Please sign in to comment.