From 9050951e51304d0042b0d3f8fe71c1811cf30e77 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 21:29:48 +0100 Subject: [PATCH 1/8] remove is func calls names to keep type inside the called lines --- packages/NodeNameResolver/NodeNameResolver.php | 12 ------------ rules/CodeQuality/NodeAnalyzer/ForAnalyzer.php | 7 +++++-- .../BooleanAnd/SimplifyEmptyArrayCheckRector.php | 6 +++++- .../CodeQuality/Rector/For_/ForToForeachRector.php | 14 ++++++++------ .../FuncCall/ParseStrWithResultArgumentRector.php | 10 +++++++--- .../Rector/NotIdentical/StrContainsRector.php | 12 ++++++++++-- 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/packages/NodeNameResolver/NodeNameResolver.php b/packages/NodeNameResolver/NodeNameResolver.php index 91f4f9fe6350..fb20c948f5a9 100644 --- a/packages/NodeNameResolver/NodeNameResolver.php +++ b/packages/NodeNameResolver/NodeNameResolver.php @@ -230,18 +230,6 @@ public function isLocalStaticPropertyFetchNamed(Node $node, string $name): bool return $this->isName($node->name, $name); } - /** - * @param string[] $names - */ - public function isFuncCallNames(Node $node, array $names): bool - { - if (! $node instanceof FuncCall) { - return false; - } - - return $this->isNames($node, $names); - } - /** * Ends with ucname * Starts with adjective, e.g. (Post $firstPost, Post $secondPost) diff --git a/rules/CodeQuality/NodeAnalyzer/ForAnalyzer.php b/rules/CodeQuality/NodeAnalyzer/ForAnalyzer.php index 57a43b02a800..725f31ec9abf 100644 --- a/rules/CodeQuality/NodeAnalyzer/ForAnalyzer.php +++ b/rules/CodeQuality/NodeAnalyzer/ForAnalyzer.php @@ -11,6 +11,7 @@ use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\Greater; use PhpParser\Node\Expr\BinaryOp\Smaller; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\PostInc; use PhpParser\Node\Expr\PreInc; use PhpParser\Node\Expr\Variable; @@ -206,10 +207,12 @@ private function isArgParentCount(Node $node): bool if (! $node instanceof Arg) { return false; } + $parent = $node->getAttribute(AttributeKey::PARENT_NODE); - if (! $parent instanceof Node) { + if (! $parent instanceof FuncCall) { return false; } - return $this->nodeNameResolver->isFuncCallName($parent, self::COUNT); + + return $this->nodeNameResolver->isName($parent, self::COUNT); } } diff --git a/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php b/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php index b85a28dba0af..d6c9fa8b0521 100644 --- a/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php +++ b/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\Empty_; +use PhpParser\Node\Expr\FuncCall; use Rector\Core\NodeManipulator\BinaryOpManipulator; use Rector\Core\Rector\AbstractRector; use Rector\Php71\ValueObject\TwoNodeMatch; @@ -57,7 +58,10 @@ public function refactor(Node $node): ?Node $node, // is_array(...) function (Node $node): bool { - return $this->nodeNameResolver->isFuncCallName($node, 'is_array'); + if (! $node instanceof FuncCall) { + return false; + } + return $this->isName($node, 'is_array'); }, Empty_::class ); diff --git a/rules/CodeQuality/Rector/For_/ForToForeachRector.php b/rules/CodeQuality/Rector/For_/ForToForeachRector.php index 4cf6fee6dd70..c543cf481eee 100644 --- a/rules/CodeQuality/Rector/For_/ForToForeachRector.php +++ b/rules/CodeQuality/Rector/For_/ForToForeachRector.php @@ -253,7 +253,7 @@ private function matchInit(array $initExprs): void $funcCall = $initExpr->expr; - if ($this->nodeNameResolver->isFuncCallName($funcCall, self::COUNT)) { + if ($this->nodeNameResolver->isName($funcCall, self::COUNT)) { $this->countValueVariableExpr = $initExpr->var; $this->countValueName = $this->getName($initExpr->var); $this->iteratedExpr = $funcCall->args[0]->value; @@ -280,11 +280,13 @@ private function isConditionMatch(array $condExprs): bool return false; } - // count($values) - if ($this->nodeNameResolver->isFuncCallName($condExprs[0]->right, self::COUNT)) { - /** @var FuncCall $countFuncCall */ - $countFuncCall = $condExprs[0]->right; - $this->iteratedExpr = $countFuncCall->args[0]->value; + $funcCall = $condExprs[0]->right; + if (! $funcCall instanceof FuncCall) { + return false; + } + + if ($this->nodeNameResolver->isName($funcCall, self::COUNT)) { + $this->iteratedExpr = $funcCall->args[0]->value; return true; } diff --git a/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php b/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php index ea7574858bcf..787bd2539b0d 100644 --- a/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php +++ b/rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php @@ -75,11 +75,15 @@ public function refactor(Node $node): ?Node } $this->traverseNodesWithCallable($nextExpression, function (Node $node) use ($resultVariable): ?Variable { - if ($this->nodeNameResolver->isFuncCallName($node, 'get_defined_vars')) { - return $resultVariable; + if (! $node instanceof FuncCall) { + return null; } - return null; + if (! $this->isName($node, 'get_defined_vars')) { + return null; + } + + return $resultVariable; }); return $node; diff --git a/rules/Php80/Rector/NotIdentical/StrContainsRector.php b/rules/Php80/Rector/NotIdentical/StrContainsRector.php index 1cff2b202ef0..43c5506de8d8 100644 --- a/rules/Php80/Rector/NotIdentical/StrContainsRector.php +++ b/rules/Php80/Rector/NotIdentical/StrContainsRector.php @@ -91,7 +91,11 @@ public function refactor(Node $node): ?Node private function matchIdenticalOrNotIdenticalToFalse(Expr $expr): ?FuncCall { if ($this->valueResolver->isFalse($expr->left)) { - if (! $this->nodeNameResolver->isFuncCallNames($expr->right, self::OLD_STR_NAMES)) { + if (! $expr->right instanceof FuncCall) { + return null; + } + + if (! $this->isNames($expr->right, self::OLD_STR_NAMES)) { return null; } @@ -101,7 +105,11 @@ private function matchIdenticalOrNotIdenticalToFalse(Expr $expr): ?FuncCall } if ($this->valueResolver->isFalse($expr->right)) { - if (! $this->nodeNameResolver->isFuncCallNames($expr->left, self::OLD_STR_NAMES)) { + if (! $expr->left instanceof FuncCall) { + return null; + } + + if (! $this->isNames($expr->left, self::OLD_STR_NAMES)) { return null; } From 6b3b6a8e94be925c3c27be1a521a3e3dfd3b9b83 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 21:35:18 +0100 Subject: [PATCH 2/8] remove isFuncCallName() --- .../NodeNameResolver/NodeNameResolver.php | 13 ------------ ...ayKeysAndInArrayToArrayKeyExistsRector.php | 8 ++++++-- .../Identical/SimplifyArraySearchRector.php | 6 +++++- .../Rector/If_/ExplicitBoolCompareRector.php | 10 +++++----- ...VariableForSprintfInSymfonyStyleRector.php | 7 ++++++- .../StringClassNameToClassConstantRector.php | 5 +++-- ...laceEachAssignmentWithKeyCurrentRector.php | 6 +++++- .../ArraySpreadInsteadOfArrayMergeRector.php | 6 +++++- .../Rector/FuncCall/ClassOnObjectRector.php | 6 +++++- .../FuncCall/TokenGetAllToObjectRector.php | 2 +- .../Rector/Identical/StrEndsWithRector.php | 6 +++++- .../Rector/Ternary/GetDebugTypeRector.php | 16 +++++++++++---- ...PHPUnitStaticToKernelTestCaseGetRector.php | 20 ------------------- 13 files changed, 58 insertions(+), 53 deletions(-) diff --git a/packages/NodeNameResolver/NodeNameResolver.php b/packages/NodeNameResolver/NodeNameResolver.php index fb20c948f5a9..7d0e7a806bac 100644 --- a/packages/NodeNameResolver/NodeNameResolver.php +++ b/packages/NodeNameResolver/NodeNameResolver.php @@ -7,7 +7,6 @@ use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Expr; -use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\PropertyFetch; @@ -275,18 +274,6 @@ public function isLocalMethodCallsNamed(Node $node, array $names): bool return false; } - /** - * @deprecated Helper function causes to lose the type on the outside. Better avoid it - */ - public function isFuncCallName(Node $node, string $name): bool - { - if (! $node instanceof FuncCall) { - return false; - } - - return $this->isName($node, $name); - } - /** * @deprecated Helper function causes to lose the type on the outside. Better avoid it */ diff --git a/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php b/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php index a11033ac2688..2516580b5148 100644 --- a/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php +++ b/rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php @@ -61,7 +61,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isFuncCallName($node, 'in_array')) { + if (! $this->nodeNameResolver->isName($node, 'in_array')) { return null; } @@ -84,7 +84,11 @@ public function refactor(Node $node): ?Node return false; } - return $this->nodeNameResolver->isFuncCallName($node->expr, 'array_keys'); + if (! $node->expr instanceof FuncCall) { + return false; + } + + return $this->nodeNameResolver->isName($node->expr, 'array_keys'); }); if (! $previousAssignArraysKeysFuncCall instanceof Assign) { diff --git a/rules/CodeQuality/Rector/Identical/SimplifyArraySearchRector.php b/rules/CodeQuality/Rector/Identical/SimplifyArraySearchRector.php index 4636baa73909..bd9eb2b5bade 100644 --- a/rules/CodeQuality/Rector/Identical/SimplifyArraySearchRector.php +++ b/rules/CodeQuality/Rector/Identical/SimplifyArraySearchRector.php @@ -60,7 +60,11 @@ public function refactor(Node $node): ?Node $twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode( $node, function (Node $node): bool { - return $this->nodeNameResolver->isFuncCallName($node, 'array_search'); + if (! $node instanceof FuncCall) { + return false; + } + + return $this->nodeNameResolver->isName($node, 'array_search'); }, function (Node $node): bool { return $this->valueResolver->isFalse($node); diff --git a/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php b/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php index 12abbe0baa2a..0143d4972539 100644 --- a/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php +++ b/rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php @@ -13,6 +13,7 @@ use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Cast\Bool_; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\DNumber; @@ -131,8 +132,7 @@ public function refactor(Node $node): ?Node private function resolveNewConditionNode(Expr $expr, bool $isNegated): ?BinaryOp { - // various cases - if ($this->nodeNameResolver->isFuncCallName($expr, 'count')) { + if ($expr instanceof FuncCall && $this->nodeNameResolver->isName($expr, 'count')) { return $this->resolveCount($isNegated, $expr); } @@ -162,16 +162,16 @@ private function resolveNewConditionNode(Expr $expr, bool $isNegated): ?BinaryOp /** * @return Identical|Greater */ - private function resolveCount(bool $isNegated, Expr $expr): BinaryOp + private function resolveCount(bool $isNegated, FuncCall $funcCall): BinaryOp { $lNumber = new LNumber(0); // compare === 0, assumption if ($isNegated) { - return new Identical($expr, $lNumber); + return new Identical($funcCall, $lNumber); } - return new Greater($expr, $lNumber); + return new Greater($funcCall, $lNumber); } /** diff --git a/rules/CodingStyle/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector.php b/rules/CodingStyle/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector.php index 27b9e8b6e881..86965bc1a171 100644 --- a/rules/CodingStyle/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector.php +++ b/rules/CodingStyle/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PHPStan\Type\ObjectType; @@ -75,7 +76,11 @@ public function refactor(Node $node): ?Node } $argValue = $node->args[0]->value; - if (! $this->nodeNameResolver->isFuncCallName($argValue, 'sprintf')) { + if (! $argValue instanceof FuncCall) { + return null; + } + + if (! $this->nodeNameResolver->isName($argValue, 'sprintf')) { return null; } diff --git a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php index d50a805010ad..f463aac73f9c 100644 --- a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php +++ b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassConst; @@ -148,11 +149,11 @@ private function isPartOfIsAFuncCall(String_ $string): bool } $parentParent = $parent->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentParent instanceof Node) { + if (! $parentParent instanceof FuncCall) { return false; } - return $this->nodeNameResolver->isFuncCallName($parentParent, 'is_a'); + return $this->nodeNameResolver->isName($parentParent, 'is_a'); } private function shouldSkip(string $classLikeName, String_ $string): bool diff --git a/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php b/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php index 7f27772740fd..f290d6d5916e 100644 --- a/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php +++ b/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php @@ -83,7 +83,11 @@ public function refactor(Node $node): ?Node private function shouldSkip(Assign $assign): bool { - if (! $this->nodeNameResolver->isFuncCallName($assign->expr, 'each')) { + if (! $assign->expr instanceof FuncCall) { + return true; + } + + if (! $this->nodeNameResolver->isName($assign->expr, 'each')) { return true; } diff --git a/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index f3a09bb34c34..b1f0ed8dec08 100644 --- a/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -193,6 +193,10 @@ private function isConstantArrayTypeWithStringKeyType(Type $type): bool private function isIteratorToArrayFuncCall(Expr $expr): bool { - return $this->nodeNameResolver->isFuncCallName($expr, 'iterator_to_array'); + if (! $expr instanceof FuncCall) { + return false; + } + + return $this->nodeNameResolver->isName($expr, 'iterator_to_array'); } } diff --git a/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php b/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php index c6f0b70918bc..34de0917ea32 100644 --- a/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php +++ b/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php @@ -66,7 +66,11 @@ public function refactor(Node $node): ?Node return null; } - if (! $this->nodeNameResolver->isFuncCallName($node, 'get_class')) { + if (! $node instanceof FuncCall) { + return null; + } + + if (! $this->nodeNameResolver->isName($node, 'get_class')) { return null; } diff --git a/rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php b/rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php index 3a9440117023..d66a7789ce04 100644 --- a/rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php +++ b/rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php @@ -92,7 +92,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isFuncCallName($node, 'token_get_all')) { + if (! $this->nodeNameResolver->isName($node, 'token_get_all')) { return null; } diff --git a/rules/Php80/Rector/Identical/StrEndsWithRector.php b/rules/Php80/Rector/Identical/StrEndsWithRector.php index e5f58327e79e..a6784531f859 100644 --- a/rules/Php80/Rector/Identical/StrEndsWithRector.php +++ b/rules/Php80/Rector/Identical/StrEndsWithRector.php @@ -133,7 +133,11 @@ private function matchUnaryMinusStrlenFuncCallArgValue(Node $node): ?Expr return null; } - if (! $this->nodeNameResolver->isFuncCallName($node->expr, 'strlen')) { + if (! $node->expr instanceof FuncCall) { + return null; + } + + if (! $this->nodeNameResolver->isName($node->expr, 'strlen')) { return null; } diff --git a/rules/Php80/Rector/Ternary/GetDebugTypeRector.php b/rules/Php80/Rector/Ternary/GetDebugTypeRector.php index a5e4bd6a9550..887205a1fd80 100644 --- a/rules/Php80/Rector/Ternary/GetDebugTypeRector.php +++ b/rules/Php80/Rector/Ternary/GetDebugTypeRector.php @@ -77,19 +77,27 @@ public function refactor(Node $node): ?Node private function shouldSkip(Ternary $ternary): bool { - if (! $this->nodeNameResolver->isFuncCallName($ternary->cond, 'is_object')) { + if (! $ternary->cond instanceof FuncCall) { return true; } - if ($ternary->if === null) { + if (! $this->nodeNameResolver->isName($ternary->cond, 'is_object')) { return true; } - if (! $this->nodeNameResolver->isFuncCallName($ternary->if, 'get_class')) { + if (! $ternary->if instanceof FuncCall) { return true; } - return ! $this->nodeNameResolver->isFuncCallName($ternary->else, 'gettype'); + if (! $this->nodeNameResolver->isName($ternary->if, 'get_class')) { + return true; + } + + if (! $ternary->else instanceof FuncCall) { + return true; + } + + return ! $this->nodeNameResolver->isName($ternary->else, 'gettype'); } private function areValuesIdentical(Ternary $ternary): bool diff --git a/rules/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php b/rules/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php index ae030dc59612..dbd8dccf0b9e 100644 --- a/rules/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php +++ b/rules/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php @@ -5,15 +5,11 @@ namespace Rector\RemovingStatic\Rector\Class_; use PhpParser\Node; -use PhpParser\Node\Arg; use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; @@ -21,7 +17,6 @@ use PhpParser\Node\Stmt\Property; use PHPStan\Type\ObjectType; use Rector\Core\Contract\Rector\ConfigurableRectorInterface; -use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\NodeManipulator\ClassInsertManipulator; use Rector\Core\Rector\AbstractRector; use Rector\Core\ValueObject\MethodName; @@ -342,21 +337,6 @@ private function createPropertyFromType(ObjectType $objectType): Property return $this->nodeFactory->createPrivatePropertyFromNameAndType($propertyName, $objectType); } -// private function createContainerGetTypeMethodCall(ObjectType $objectType): MethodCall -// { -// $staticPropertyFetch = new StaticPropertyFetch(new Name('self'), 'container'); -// $getMethodCall = new MethodCall($staticPropertyFetch, 'get'); -// -// $className = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType); -// if (! $className instanceof Name) { -// throw new ShouldNotHappenException(); -// } -// -// $getMethodCall->args[] = new Arg(new ClassConstFetch($className, 'class')); -// -// return $getMethodCall; -// } - private function getParentSetUpStaticCallPosition(ClassMethod $setupClassMethod): ?int { foreach ((array) $setupClassMethod->stmts as $position => $methodStmt) { From a57e09fffc68d228c817ed058978eced42367beb Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 22:07:17 +0100 Subject: [PATCH 3/8] remove isVariableName() --- .../NodeNameResolver/NodeNameResolver.php | 27 ------------------- .../NodeAnalyzer/ForeachAnalyzer.php | 9 +++++-- .../VarInlineAnnotationToAssertRector.php | 13 ++++++--- .../CatchExceptionNameMatchingTypeRector.php | 2 +- .../NewlineBeforeNewAssignSetRector.php | 7 ++++- .../ClassMethod/ReturnThisRemoveRector.php | 5 ++-- .../Class_/OrderPrivateMethodsByUseRector.php | 7 ++++- .../MethodCall/IncreaseColumnIndexRector.php | 6 ++++- .../ChangeIOFactoryArgumentRector.php | 12 +++++---- ...isCallOnStaticMethodToStaticCallRector.php | 7 ++++- .../PhpSpecPromisesToPHPUnitAssertRector.php | 6 ++++- ...riableWithDefaultValueToConstantRector.php | 6 ++++- .../PrivatizeLocalGetterToPropertyRector.php | 7 ++++- 13 files changed, 67 insertions(+), 47 deletions(-) diff --git a/packages/NodeNameResolver/NodeNameResolver.php b/packages/NodeNameResolver/NodeNameResolver.php index 7d0e7a806bac..60273b9839f5 100644 --- a/packages/NodeNameResolver/NodeNameResolver.php +++ b/packages/NodeNameResolver/NodeNameResolver.php @@ -294,33 +294,6 @@ public function isStaticCallNamed(Node $node, string $className, string $methodN return $this->isName($node->name, $methodName); } - /** - * @deprecated Helper function causes to lose the type on the outside. Better avoid it - * @param string[] $methodNames - */ - public function isStaticCallsNamed(Node $node, string $className, array $methodNames): bool - { - foreach ($methodNames as $methodName) { - if ($this->isStaticCallNamed($node, $className, $methodName)) { - return true; - } - } - - return false; - } - - /** - * @deprecated Helper function causes to lose the type on the outside. Better avoid it - */ - public function isVariableName(Node $node, string $name): bool - { - if (! $node instanceof Variable) { - return false; - } - - return $this->isName($node, $name); - } - /** * @param ObjectType[] $desiredObjectTypes */ diff --git a/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php b/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php index fb2d1c32e2f7..18414919957b 100644 --- a/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php +++ b/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; @@ -111,11 +112,15 @@ function (Node $node) use ($foreachedValue, $singleValue, $keyValueName): ?Expr } // is dim same as key value name, ...[$i] - if ($node->dim === null) { +// if ($node->dim === null) { +// return null; +// } +// + if (! $node->dim instanceof Variable) { return null; } - if (! $this->nodeNameResolver->isVariableName($node->dim, $keyValueName)) { + if (! $this->nodeNameResolver->isName($node->dim, $keyValueName)) { return null; } diff --git a/rules/CodeQualityStrict/Rector/Stmt/VarInlineAnnotationToAssertRector.php b/rules/CodeQualityStrict/Rector/Stmt/VarInlineAnnotationToAssertRector.php index 8750bd7b2013..bebe961127b0 100644 --- a/rules/CodeQualityStrict/Rector/Stmt/VarInlineAnnotationToAssertRector.php +++ b/rules/CodeQualityStrict/Rector/Stmt/VarInlineAnnotationToAssertRector.php @@ -125,8 +125,12 @@ private function getVarDocVariableName(PhpDocInfo $phpDocInfo): ?string private function findVariableByName(Stmt $stmt, string $docVariableName): ?Node { - return $this->betterNodeFinder->findFirst($stmt, function (Node $stmt) use ($docVariableName): bool { - return $this->nodeNameResolver->isVariableName($stmt, $docVariableName); + return $this->betterNodeFinder->findFirst($stmt, function (Node $node) use ($docVariableName): bool { + if (! $node instanceof Variable) { + return false; + } + + return $this->nodeNameResolver->isName($node, $docVariableName); }); } @@ -141,9 +145,12 @@ private function isVariableJustCreated(Stmt $stmt, string $docVariableName): boo } $assign = $stmt->expr; + if (! $assign->var instanceof Variable) { + return false; + } // the variable is on the left side = just created - return $this->nodeNameResolver->isVariableName($assign->var, $docVariableName); + return $this->nodeNameResolver->isName($assign->var, $docVariableName); } private function refactorFreshlyCreatedNode(Stmt $stmt, PhpDocInfo $phpDocInfo, Variable $variable): ?Node diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index 65fd2ded7c79..db3fc3ced4b4 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -134,7 +134,7 @@ private function renameVariableInStmts(Catch_ $catch, string $oldVariableName, s return; } - if (! $this->nodeNameResolver->isVariableName($node, $oldVariableName)) { + if (! $this->nodeNameResolver->isName($node, $oldVariableName)) { return; } diff --git a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php index ec9ae9b4d8a9..6e6257bd908a 100644 --- a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; @@ -142,8 +143,12 @@ private function shouldAddEmptyLine(?string $currentStmtVariableName, Node $node */ private function shouldSkipLeftVariable(Node $node): bool { + if (! $node->var instanceof Variable) { + return false; + } + // local method call - return $this->nodeNameResolver->isVariableName($node->var, 'this'); + return $this->nodeNameResolver->isName($node->var, 'this'); } private function isNewVariableThanBefore(?string $currentStmtVariableName): bool diff --git a/rules/Defluent/Rector/ClassMethod/ReturnThisRemoveRector.php b/rules/Defluent/Rector/ClassMethod/ReturnThisRemoveRector.php index 2e3eaec66026..e0149056dcb2 100644 --- a/rules/Defluent/Rector/ClassMethod/ReturnThisRemoveRector.php +++ b/rules/Defluent/Rector/ClassMethod/ReturnThisRemoveRector.php @@ -5,6 +5,7 @@ namespace Rector\Defluent\Rector\ClassMethod; use PhpParser\Node; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; @@ -122,11 +123,11 @@ private function matchSingleReturnThis(ClassMethod $classMethod): ?Return_ } $return = $returns[0]; - if ($return->expr === null) { + if (! $return->expr instanceof Variable) { return null; } - if (! $this->nodeNameResolver->isVariableName($return->expr, 'this')) { + if (! $this->nodeNameResolver->isName($return->expr, 'this')) { return null; } diff --git a/rules/Order/Rector/Class_/OrderPrivateMethodsByUseRector.php b/rules/Order/Rector/Class_/OrderPrivateMethodsByUseRector.php index 6640e28931aa..965ff0c429ef 100644 --- a/rules/Order/Rector/Class_/OrderPrivateMethodsByUseRector.php +++ b/rules/Order/Rector/Class_/OrderPrivateMethodsByUseRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; @@ -156,7 +157,11 @@ private function getLocalPrivateMethodCallOrder(ClassLike $classLike): array return null; } - if (! $this->nodeNameResolver->isVariableName($node->var, 'this')) { + if (! $node->var instanceof Variable) { + return null; + } + + if (! $this->nodeNameResolver->isName($node->var, 'this')) { return null; } diff --git a/rules/PHPOffice/Rector/MethodCall/IncreaseColumnIndexRector.php b/rules/PHPOffice/Rector/MethodCall/IncreaseColumnIndexRector.php index a209f1632ef4..97c847d11252 100644 --- a/rules/PHPOffice/Rector/MethodCall/IncreaseColumnIndexRector.php +++ b/rules/PHPOffice/Rector/MethodCall/IncreaseColumnIndexRector.php @@ -162,7 +162,11 @@ private function findVariableAssignName(array $node, string $variableName): ?Nod return false; } - return $this->nodeNameResolver->isVariableName($node->var, $variableName); + if (! $node->var instanceof Variable) { + return false; + } + + return $this->nodeNameResolver->isName($node->var, $variableName); }); } } diff --git a/rules/PHPOffice/Rector/StaticCall/ChangeIOFactoryArgumentRector.php b/rules/PHPOffice/Rector/StaticCall/ChangeIOFactoryArgumentRector.php index 36a3bddae6cc..ac1c384c4cff 100644 --- a/rules/PHPOffice/Rector/StaticCall/ChangeIOFactoryArgumentRector.php +++ b/rules/PHPOffice/Rector/StaticCall/ChangeIOFactoryArgumentRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Scalar\String_; +use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -77,11 +78,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isStaticCallsNamed( - $node, - 'PHPExcel_IOFactory', - ['createReader', 'createWriter', 'identify'] - )) { + $callerType = $this->getObjectType($node->class); + if (! $callerType->isSuperTypeOf(new ObjectType('PHPExcel_IOFactory'))->yes()) { + return null; + } + + if (! $this->isNames($node->name, ['createReader', 'createWriter', 'identify'])) { return null; } diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index 9884fb984482..141558e7c0e0 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PHPStan\Reflection\Php\PhpMethodReflection; use PHPStan\Type\ObjectType; @@ -88,7 +89,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isVariableName($node->var, 'this')) { + if (! $node->var instanceof Variable) { + return null; + } + + if (! $this->nodeNameResolver->isName($node->var, 'this')) { return null; } diff --git a/rules/PhpSpecToPHPUnit/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php b/rules/PhpSpecToPHPUnit/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php index 8f72632150bc..607457e3fc53 100644 --- a/rules/PhpSpecToPHPUnit/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php +++ b/rules/PhpSpecToPHPUnit/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php @@ -287,7 +287,11 @@ private function processMatchersKeys(MethodCall $methodCall): void private function shouldSkip(MethodCall $methodCall): bool { - if (! $this->nodeNameResolver->isVariableName($methodCall->var, self::THIS)) { + if (! $methodCall->var instanceof Variable) { + return true; + } + + if (! $this->nodeNameResolver->isName($methodCall->var, self::THIS)) { return true; } diff --git a/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php b/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php index 8abb8e8753ff..456d7dc67548 100644 --- a/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php +++ b/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php @@ -267,7 +267,11 @@ private function replaceVariableWithClassConstFetch( $variableName, $constantName ): ?ClassConstFetch { - if (! $this->nodeNameResolver->isVariableName($node, $variableName)) { + if (! $node instanceof Variable) { + return null; + } + + if (! $this->nodeNameResolver->isName($node, $variableName)) { return null; } diff --git a/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php b/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php index 9b1122ffeb3d..835389961883 100644 --- a/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php +++ b/rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; @@ -76,7 +77,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isVariableName($node->var, 'this')) { + if (! $node->var instanceof Variable) { + return null; + } + + if (! $this->nodeNameResolver->isName($node->var, 'this')) { return null; } From 83dd98ecc3c322e84f7f209e8f4c7bbd6f703727 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 22:12:19 +0100 Subject: [PATCH 4/8] misc --- .../ClassMethodParamVendorLockResolver.php | 17 ++++++++-- .../ClassReflectionAncestorAnalyzer.php | 32 +------------------ .../NodeAnalyzer/ForeachAnalyzer.php | 4 --- 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php index be62c305d52c..5dfd3b92096e 100644 --- a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php +++ b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php @@ -59,11 +59,22 @@ public function isVendorLocked(ClassMethod $classMethod, int $paramPosition): bo return false; } - if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) { - return false; + $methodName = $this->nodeNameResolver->getName($classMethod); + foreach ($classReflection->getAncestors() as $ancestorClassReflection) { + // skip self + if ($ancestorClassReflection === $classReflection) { + continue; + } + + if (! $classReflection->hasNativeMethod($methodName)) { + continue; + } } - $methodName = $this->nodeNameResolver->getName($classMethod); +// if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) { +// return false; +// } + if ($classReflection->getParentClass() !== false) { $vendorLock = $this->isParentClassVendorLocking( diff --git a/packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php b/packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php index 9abaf1839f16..50440b7db471 100644 --- a/packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php +++ b/packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php @@ -5,41 +5,11 @@ namespace Rector\VendorLocker\Reflection; use PHPStan\Reflection\ClassReflection; -use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer; final class ClassReflectionAncestorAnalyzer { - /** - * @var FamilyRelationsAnalyzer - */ - private $familyRelationsAnalyzer; - - public function __construct(FamilyRelationsAnalyzer $familyRelationsAnalyzer) - { - $this->familyRelationsAnalyzer = $familyRelationsAnalyzer; - } - public function hasAncestors(ClassReflection $classReflection): bool { - if ($classReflection->isClass()) { - // has at least interface - if ($classReflection->getInterfaces() !== []) { - return true; - } - - // has at least one parent class - if ($classReflection->getParents() !== []) { - return true; - } - - $childrenClassReflections = $this->familyRelationsAnalyzer->getChildrenOfClassReflection($classReflection); - return $childrenClassReflections !== []; - } - - if ($classReflection->isInterface()) { - return $classReflection->getInterfaces() !== []; - } - - return false; + return $classReflection->getAncestors() !== [$classReflection]; } } diff --git a/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php b/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php index 18414919957b..0e9eeaee8e04 100644 --- a/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php +++ b/rules/CodeQuality/NodeAnalyzer/ForeachAnalyzer.php @@ -112,10 +112,6 @@ function (Node $node) use ($foreachedValue, $singleValue, $keyValueName): ?Expr } // is dim same as key value name, ...[$i] -// if ($node->dim === null) { -// return null; -// } -// if (! $node->dim instanceof Variable) { return null; } From d2595f534cff7cae265d297d43b44ec0aea4e1fa Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 22:48:52 +0100 Subject: [PATCH 5/8] remove isLocalStaticPropertyFetchNamed() --- .../NodeNameResolver/NodeNameResolver.php | 26 ------------------- .../PropertyRenamer/PropertyFetchRenamer.php | 14 +++++----- src/HttpKernel/RectorKernel.php | 2 +- 3 files changed, 8 insertions(+), 34 deletions(-) diff --git a/packages/NodeNameResolver/NodeNameResolver.php b/packages/NodeNameResolver/NodeNameResolver.php index 60273b9839f5..f145f375af50 100644 --- a/packages/NodeNameResolver/NodeNameResolver.php +++ b/packages/NodeNameResolver/NodeNameResolver.php @@ -11,7 +11,6 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Name; @@ -183,22 +182,6 @@ public function getNames(array $nodes): array return $names; } - /** - * @param Node[] $nodes - */ - public function haveName(array $nodes, string $name): bool - { - foreach ($nodes as $node) { - if (! $this->isName($node, $name)) { - continue; - } - - return true; - } - - return false; - } - public function isLocalPropertyFetchNamed(Node $node, string $name): bool { if (! $node instanceof PropertyFetch) { @@ -220,15 +203,6 @@ public function isLocalPropertyFetchNamed(Node $node, string $name): bool return $this->isName($node->name, $name); } - public function isLocalStaticPropertyFetchNamed(Node $node, string $name): bool - { - if (! $node instanceof StaticPropertyFetch) { - return false; - } - - return $this->isName($node->name, $name); - } - /** * Ends with ucname * Starts with adjective, e.g. (Post $firstPost, Post $secondPost) diff --git a/rules/Naming/PropertyRenamer/PropertyFetchRenamer.php b/rules/Naming/PropertyRenamer/PropertyFetchRenamer.php index 92c81c8ca75c..5602a059da27 100644 --- a/rules/Naming/PropertyRenamer/PropertyFetchRenamer.php +++ b/rules/Naming/PropertyRenamer/PropertyFetchRenamer.php @@ -47,16 +47,16 @@ function (Node $node) use ($currentName, $expectedName): ?Node { return $node; } - if ($this->nodeNameResolver->isLocalStaticPropertyFetchNamed($node, $currentName)) { - if (! $node instanceof StaticPropertyFetch) { - return null; - } + if (! $node instanceof StaticPropertyFetch) { + return null; + } - $node->name = new VarLikeIdentifier($expectedName); - return $node; + if (! $this->nodeNameResolver->isName($node->name, $currentName)) { + return null; } - return null; + $node->name = new VarLikeIdentifier($expectedName); + return $node; } ); } diff --git a/src/HttpKernel/RectorKernel.php b/src/HttpKernel/RectorKernel.php index 6d9de0f4ade7..567db38a20bc 100644 --- a/src/HttpKernel/RectorKernel.php +++ b/src/HttpKernel/RectorKernel.php @@ -90,7 +90,7 @@ public function registerBundles(): iterable new ConsoleColorDiffBundle(), new ComposerJsonManipulatorBundle(), new SkipperBundle(), - new SimplePhpDocParserBundle() + new SimplePhpDocParserBundle(), ]; return $bundles; From cc72d3bdfdaa21c4179cb06dc9ca7a87e8de333d Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 23:22:15 +0100 Subject: [PATCH 6/8] improve ClassMethodParamVendorLockResolver --- .../ClassMethodParamVendorLockResolver.php | 63 +++++-------------- .../local_scope_with_parent_class.php.inc | 3 - .../Fixture/skip_interface_extends.php.inc | 7 +-- src/HttpKernel/RectorKernel.php | 4 +- 4 files changed, 19 insertions(+), 58 deletions(-) diff --git a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php index 5dfd3b92096e..8e6ba3fe4459 100644 --- a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php +++ b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php @@ -59,6 +59,10 @@ public function isVendorLocked(ClassMethod $classMethod, int $paramPosition): bo return false; } + if ($classMethod->isMagic()) { + return true; + } + $methodName = $this->nodeNameResolver->getName($classMethod); foreach ($classReflection->getAncestors() as $ancestorClassReflection) { // skip self @@ -66,62 +70,25 @@ public function isVendorLocked(ClassMethod $classMethod, int $paramPosition): bo continue; } - if (! $classReflection->hasNativeMethod($methodName)) { + if (! $ancestorClassReflection->hasNativeMethod($methodName)) { continue; } - } - -// if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) { -// return false; -// } - - if ($classReflection->getParentClass() !== false) { - $vendorLock = $this->isParentClassVendorLocking( - $classReflection->getParentClass(), - $paramPosition, - $methodName - ); - if ($vendorLock !== null) { - return $vendorLock; + // class is vendor, its locking us + $classLike = $this->nodeRepository->findClassLike($ancestorClassReflection->getName()); + if ($classLike === null) { + return true; } - } - - if ($classReflection->isClass()) { - return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName); - } - if ($classReflection->isInterface()) { - return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName); - } - - return false; - } - - private function isParentClassVendorLocking( - ClassReflection $parentClassReflection, - int $paramPosition, - string $methodName - ): ?bool { - $parentClass = $this->nodeRepository->findClass($parentClassReflection->getName()); - if ($parentClass !== null) { - $parentClassMethod = $parentClass->getMethod($methodName); - // parent class method in local scope → it's ok - if ($parentClassMethod !== null) { - // parent method has no type → we cannot change it here - if (! isset($parentClassMethod->params[$paramPosition])) { - return false; - } - return $parentClassMethod->params[$paramPosition]->type === null; + $classMethod = $classLike->getMethod($methodName); + if ($classMethod === null) { + continue; } - } - if ($parentClassReflection->hasMethod($methodName)) { - // parent class method in external scope → it's not ok - // if not, look for it's parent parent - return true; + $paramType = $classMethod->params[$paramPosition]->type; + return $paramType !== null; } - return null; + return false; } } diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc index b36ff770cc74..51c98425a70c 100644 --- a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/local_scope_with_parent_class.php.inc @@ -41,9 +41,6 @@ final class LocalChildClass extends AbstractLocalParentClass { } - /** - * @param int $number - */ public function changeToo(int $number) { } diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc index f835cb46ae78..2ad952b3877e 100644 --- a/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_interface_extends.php.inc @@ -6,7 +6,7 @@ interface ParentInterface /** * @param float|int|string $value */ - public function __construct($value); + public function run($value); } interface IntermediateInterface extends ParentInterface @@ -14,7 +14,7 @@ interface IntermediateInterface extends ParentInterface /** * @param string $countryCode */ - public function __construct($countryCode); + public function run($countryCode); } final class SkipInterfaceExtends implements IntermediateInterface @@ -22,8 +22,7 @@ final class SkipInterfaceExtends implements IntermediateInterface /** * @param string $countryCode */ - public function __construct($countryCode) + public function run(string $countryCode) { } } -?> diff --git a/src/HttpKernel/RectorKernel.php b/src/HttpKernel/RectorKernel.php index 567db38a20bc..48f3e747035d 100644 --- a/src/HttpKernel/RectorKernel.php +++ b/src/HttpKernel/RectorKernel.php @@ -86,14 +86,12 @@ public function setConfigs(array $configs): void */ public function registerBundles(): iterable { - $bundles = [ + return [ new ConsoleColorDiffBundle(), new ComposerJsonManipulatorBundle(), new SkipperBundle(), new SimplePhpDocParserBundle(), ]; - - return $bundles; } protected function build(ContainerBuilder $containerBuilder): void From c1848ff83640994fdecc99cf45dcb39791a793d5 Mon Sep 17 00:00:00 2001 From: kaizen-ci Date: Fri, 19 Mar 2021 22:39:44 +0000 Subject: [PATCH 7/8] [ci-review] Rector Rectify --- .../ClassMethodParamVendorLockResolver.php | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php index 8e6ba3fe4459..47b66406f11b 100644 --- a/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php +++ b/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php @@ -4,27 +4,16 @@ namespace Rector\VendorLocker\NodeVendorLocker; +use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use Rector\NodeCollector\NodeCollector\NodeRepository; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\VendorLocker\Reflection\ClassReflectionAncestorAnalyzer; -use Rector\VendorLocker\Reflection\MethodReflectionContractAnalyzer; final class ClassMethodParamVendorLockResolver { - /** - * @var ClassReflectionAncestorAnalyzer - */ - private $classReflectionAncestorAnalyzer; - - /** - * @var MethodReflectionContractAnalyzer - */ - private $methodReflectionContractAnalyzer; - /** * @var NodeNameResolver */ @@ -35,14 +24,8 @@ final class ClassMethodParamVendorLockResolver */ private $nodeRepository; - public function __construct( - ClassReflectionAncestorAnalyzer $classReflectionAncestorAnalyzer, - MethodReflectionContractAnalyzer $methodReflectionContractAnalyzer, - NodeNameResolver $nodeNameResolver, - NodeRepository $nodeRepository - ) { - $this->classReflectionAncestorAnalyzer = $classReflectionAncestorAnalyzer; - $this->methodReflectionContractAnalyzer = $methodReflectionContractAnalyzer; + public function __construct(NodeNameResolver $nodeNameResolver, NodeRepository $nodeRepository) + { $this->nodeNameResolver = $nodeNameResolver; $this->nodeRepository = $nodeRepository; } @@ -76,12 +59,12 @@ public function isVendorLocked(ClassMethod $classMethod, int $paramPosition): bo // class is vendor, its locking us $classLike = $this->nodeRepository->findClassLike($ancestorClassReflection->getName()); - if ($classLike === null) { + if (! $classLike instanceof ClassLike) { return true; } $classMethod = $classLike->getMethod($methodName); - if ($classMethod === null) { + if (! $classMethod instanceof ClassMethod) { continue; } From 1a668b56fcbce86ff665c169a2df9e5fee3279b2 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 19 Mar 2021 23:43:37 +0100 Subject: [PATCH 8/8] remove unused class --- .../ClassMethodReturnVendorLockResolver.php | 10 +-------- .../PropertyTypeVendorLockResolver.php | 10 +-------- .../ClassReflectionAncestorAnalyzer.php | 15 ------------- .../skip_already_set_return_type.php.inc | 21 +++++++++++++++++++ ...veOutMethodCallInsideIfConditionRector.php | 7 ++++--- 5 files changed, 27 insertions(+), 36 deletions(-) delete mode 100644 packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php create mode 100644 rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture/skip_already_set_return_type.php.inc diff --git a/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php b/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php index bc1e38b51e5a..92751d1cd277 100644 --- a/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php +++ b/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnVendorLockResolver.php @@ -11,16 +11,10 @@ use PHPStan\Type\MixedType; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\VendorLocker\Reflection\ClassReflectionAncestorAnalyzer; use Rector\VendorLocker\Reflection\MethodReflectionContractAnalyzer; final class ClassMethodReturnVendorLockResolver { - /** - * @var ClassReflectionAncestorAnalyzer - */ - private $classReflectionAncestorAnalyzer; - /** * @var MethodReflectionContractAnalyzer */ @@ -32,11 +26,9 @@ final class ClassMethodReturnVendorLockResolver private $nodeNameResolver; public function __construct( - ClassReflectionAncestorAnalyzer $classReflectionAncestorAnalyzer, MethodReflectionContractAnalyzer $methodReflectionContractAnalyzer, NodeNameResolver $nodeNameResolver ) { - $this->classReflectionAncestorAnalyzer = $classReflectionAncestorAnalyzer; $this->methodReflectionContractAnalyzer = $methodReflectionContractAnalyzer; $this->nodeNameResolver = $nodeNameResolver; } @@ -53,7 +45,7 @@ public function isVendorLocked(ClassMethod $classMethod): bool return false; } - if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) { + if (count($classReflection->getAncestors()) === 1) { return false; } diff --git a/packages/VendorLocker/NodeVendorLocker/PropertyTypeVendorLockResolver.php b/packages/VendorLocker/NodeVendorLocker/PropertyTypeVendorLockResolver.php index ab8bcf384a57..91bf4932ef4e 100644 --- a/packages/VendorLocker/NodeVendorLocker/PropertyTypeVendorLockResolver.php +++ b/packages/VendorLocker/NodeVendorLocker/PropertyTypeVendorLockResolver.php @@ -10,15 +10,9 @@ use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\VendorLocker\Reflection\ClassReflectionAncestorAnalyzer; final class PropertyTypeVendorLockResolver { - /** - * @var ClassReflectionAncestorAnalyzer - */ - private $classReflectionAncestorAnalyzer; - /** * @var NodeNameResolver */ @@ -30,11 +24,9 @@ final class PropertyTypeVendorLockResolver private $familyRelationsAnalyzer; public function __construct( - ClassReflectionAncestorAnalyzer $classReflectionAncestorAnalyzer, NodeNameResolver $nodeNameResolver, FamilyRelationsAnalyzer $familyRelationsAnalyzer ) { - $this->classReflectionAncestorAnalyzer = $classReflectionAncestorAnalyzer; $this->nodeNameResolver = $nodeNameResolver; $this->familyRelationsAnalyzer = $familyRelationsAnalyzer; } @@ -49,7 +41,7 @@ public function isVendorLocked(Property $property): bool return false; } - if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) { + if (count($classReflection->getAncestors()) === 1) { return false; } diff --git a/packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php b/packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php deleted file mode 100644 index 50440b7db471..000000000000 --- a/packages/VendorLocker/Reflection/ClassReflectionAncestorAnalyzer.php +++ /dev/null @@ -1,15 +0,0 @@ -getAncestors() !== [$classReflection]; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture/skip_already_set_return_type.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture/skip_already_set_return_type.php.inc new file mode 100644 index 000000000000..585c6195aa95 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture/skip_already_set_return_type.php.inc @@ -0,0 +1,21 @@ +getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + return false; + } - return $scope->hasVariableType($variableName) - ->yes(); + return $scope->hasVariableType($variableName)->yes(); } }