Skip to content

Commit

Permalink
[Naming] Add Closure and Function_ on RenameParamToMatchTypeRector (#…
Browse files Browse the repository at this point in the history
…2042)

* [Naming] Add Closure and Function_ on RenameParamToMatchTypeRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* rename method name

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 10, 2022
1 parent 9b46906 commit 76894e5
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public function processNodes(array $stmts, SmartFileInfo $smartFileInfo): array
$scope = $this->scopeFactory->createFromFile($smartFileInfo);

// skip chain method calls, performance issue: https://github.com/phpstan/phpstan/issues/254
$nodeCallback = function (Node $node, MutatingScope $scope) use (&$nodeCallback): void {
$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (&$nodeCallback): void {
if ($node instanceof Trait_) {
$traitName = $this->resolveClassName($node);

$traitReflectionClass = $this->reflectionProvider->getClass($traitName);

$traitScope = clone $scope;
$traitScope = clone $mutatingScope;

$scopeContext = $this->privatesAccessor->getPrivatePropertyOfClass(
$traitScope,
Expand Down Expand Up @@ -109,17 +109,17 @@ public function processNodes(array $stmts, SmartFileInfo $smartFileInfo): array
// the class reflection is resolved AFTER entering to class node
// so we need to get it from the first after this one
if ($node instanceof Class_ || $node instanceof Interface_) {
/** @var MutatingScope $scope */
$scope = $this->resolveClassOrInterfaceScope($node, $scope);
/** @var MutatingScope $mutatingScope */
$mutatingScope = $this->resolveClassOrInterfaceScope($node, $mutatingScope);
}

// special case for unreachable nodes
if ($node instanceof UnreachableStatementNode) {
$originalNode = $node->getOriginalStatement();
$originalNode->setAttribute(AttributeKey::IS_UNREACHABLE, true);
$originalNode->setAttribute(AttributeKey::SCOPE, $scope);
$originalNode->setAttribute(AttributeKey::SCOPE, $mutatingScope);
} else {
$node->setAttribute(AttributeKey::SCOPE, $scope);
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function renameFullyQualifiedNamespace(
$phpDocNodeTraverser->traverseWithCallable(
$phpDocInfo->getPhpDocNode(),
'',
function (DocNode $subNode) use ($oldToNewNamespaces): ?DocNode {
if (! $subNode instanceof IdentifierTypeNode) {
function (DocNode $docNode) use ($oldToNewNamespaces): ?DocNode {
if (! $docNode instanceof IdentifierTypeNode) {
return null;
}

$name = $subNode->name;
$trimmedName = ltrim($subNode->name, '\\');
$name = $docNode->name;
$trimmedName = ltrim($docNode->name, '\\');

if ($name === $trimmedName) {
return null;
Expand Down
8 changes: 4 additions & 4 deletions packages/PostRector/Collector/NodesToRemoveCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ private function isUsedInArg(Node $node, Node $parentNode): bool

$paramVariable = $node->var;
if ($paramVariable instanceof Variable) {
return (bool) $this->betterNodeFinder->findFirst((array) $parentNode->stmts, function (Node $variable) use (
return (bool) $this->betterNodeFinder->findFirst((array) $parentNode->stmts, function (Node $node) use (
$paramVariable
): bool {
if (! $this->nodeComparator->areNodesEqual($variable, $paramVariable)) {
if (! $this->nodeComparator->areNodesEqual($node, $paramVariable)) {
return false;
}

$hasArgParent = (bool) $this->betterNodeFinder->findParentType($variable, Arg::class);
$hasArgParent = (bool) $this->betterNodeFinder->findParentType($node, Arg::class);
if (! $hasArgParent) {
return false;
}

return ! (bool) $this->betterNodeFinder->findParentType($variable, StaticCall::class);
return ! (bool) $this->betterNodeFinder->findParentType($node, StaticCall::class);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class CallbackUse
{
public function run(ClassMethod $classMethod, array $items)
{
array_walk($classMethod->stmts, function (Node $stmt) use ($classMethod) {
return $stmt + $classMethod;
array_walk($classMethod->stmts, function (Node $node) use ($classMethod) {
return $node + $classMethod;
});

function someFunction($node)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Fixture;

use Rector\Config\RectorConfig;

return static function (RectorConfig $containerConfigurator): void {

};

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Fixture;

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {

};

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Fixture;

function FunctionCallbackUse(ClassMethod $node, array $items)
{
array_walk($node->stmts, function (Node $stmt) use ($node) {
return $stmt + $node;
});

function someFunction($node)
{
}
}

?>
-----
<?php

namespace Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Fixture;

function FunctionCallbackUse(ClassMethod $classMethod, array $items)
{
array_walk($classMethod->stmts, function (Node $node) use ($classMethod) {
return $node + $classMethod;
});

function someFunction($node)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ private function collectPropertyNamesWithMissingDefaultArray(Class_ $class): arr
*/
private function completeDefaultArrayToPropertyNames(Class_ $class, array $propertyNames): void
{
$this->traverseNodesWithCallable($class, function (Node $class) use ($propertyNames): ?PropertyProperty {
if (! $class instanceof PropertyProperty) {
$this->traverseNodesWithCallable($class, function (Node $node) use ($propertyNames): ?PropertyProperty {
if (! $node instanceof PropertyProperty) {
return null;
}

if (! $this->isNames($class, $propertyNames)) {
if (! $this->isNames($node, $propertyNames)) {
return null;
}

$class->default = new Array_();
$node->default = new Array_();

return $class;
return $node;
});
}

Expand Down
4 changes: 2 additions & 2 deletions rules/Naming/Guard/BreakingVariableRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function shouldSkipVariable(
public function shouldSkipParam(
string $currentName,
string $expectedName,
ClassMethod $classMethod,
ClassMethod|Function_|Closure $classMethod,
Param $param
): bool {
// is the suffix? → also accepted
Expand All @@ -106,7 +106,7 @@ public function shouldSkipParam(
return true;
}

if ($this->overridenExistingNamesResolver->hasNameInClassMethodForParam($expectedName, $classMethod)) {
if ($this->overridenExistingNamesResolver->hasNameInFunctionLikeForParam($expectedName, $classMethod)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/Naming/Naming/ConflictingNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
/**
* @return string[]
*/
public function resolveConflictingVariableNamesForParam(ClassMethod $classMethod): array
public function resolveConflictingVariableNamesForParam(ClassMethod|Function_|Closure $classMethod): array
{
$expectedNames = [];
foreach ($classMethod->params as $param) {
Expand Down
6 changes: 4 additions & 2 deletions rules/Naming/Naming/OverridenExistingNamesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public function hasNameInClassMethodForNew(
return in_array($variableName, $overridenVariableNames, true);
}

public function hasNameInClassMethodForParam(string $expectedName, ClassMethod $classMethod): bool
{
public function hasNameInFunctionLikeForParam(
string $expectedName,
ClassMethod|Function_|Closure $classMethod
): bool {
/** @var Assign[] $assigns */
$assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class);

Expand Down
17 changes: 13 additions & 4 deletions rules/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\Naming\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver;
Expand Down Expand Up @@ -66,11 +68,11 @@ public function run(Apple $apple)
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [ClassMethod::class, Function_::class, Closure::class];
}

/**
* @param ClassMethod $node
* @param ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node): ?Node
{
Expand Down Expand Up @@ -107,15 +109,22 @@ public function refactor(Node $node): ?Node
return $node;
}

private function shouldSkipParam(Param $param, string $expectedName, ClassMethod $classMethod): bool
{
private function shouldSkipParam(
Param $param,
string $expectedName,
ClassMethod|Function_|Closure $classMethod
): bool {
/** @var string $paramName */
$paramName = $this->getName($param);

if ($this->breakingVariableRenameGuard->shouldSkipParam($paramName, $expectedName, $classMethod, $param)) {
return true;
}

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

// promoted property
if (! $this->isName($classMethod, MethodName::CONSTRUCT)) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/NodeAnalyzer/VariableAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public function isStaticOrGlobal(Variable $variable): bool
return true;
}

return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (Node $n) use (
return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (Node $node) use (
$variable
): bool {
if (! in_array($n::class, [Static_::class, Global_::class], true)) {
if (! in_array($node::class, [Static_::class, Global_::class], true)) {
return false;
}

/**
* @var Static_|Global_ $n
* @var Static_|Global_ $node
* @var StaticVar[]|Variable[] $vars
*/
$vars = $n->vars;
$vars = $node->vars;
foreach ($vars as $var) {
$staticVarVariable = $var instanceof StaticVar
? $var->var
Expand Down

0 comments on commit 76894e5

Please sign in to comment.