Skip to content

Commit

Permalink
Updated Rector to commit 7ec1be6b828042b27e3f40ed41b3285a6fe78502
Browse files Browse the repository at this point in the history
rectorphp/rector-src@7ec1be6 [DeadCode]: Support functions in RemoveUnusedVariableAssignRector (#5249)
  • Loading branch information
TomasVotruba committed Nov 14, 2023
1 parent 374bab1 commit 9aef117
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
37 changes: 22 additions & 15 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ public function run()
*/
public function getNodeTypes() : array
{
return [ClassMethod::class];
return [ClassMethod::class, Stmt\Function_::class];
}
/**
* @param ClassMethod $node
* @param ClassMethod|Stmt\Function_ $node
* @return null|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_
*/
public function refactorWithScope(Node $node, Scope $scope) : ?ClassMethod
public function refactorWithScope(Node $node, Scope $scope)
{
$classMethodStmts = $node->stmts;
if ($classMethodStmts === null) {
$stmts = $node->stmts;
if ($stmts === null || $stmts === []) {
return null;
}
// we cannot be sure here
Expand All @@ -99,14 +100,14 @@ public function refactorWithScope(Node $node, Scope $scope) : ?ClassMethod
if ($this->containsFileIncludes($node)) {
return null;
}
$assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($classMethodStmts);
$assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($stmts);
$hasChanged = \false;
foreach ($assignedVariableNamesByStmtPosition as $stmtPosition => $variableName) {
if ($this->isVariableUsedInFollowingStmts($node, $stmtPosition, $variableName)) {
continue;
}
/** @var Expression<Assign> $currentStmt */
$currentStmt = $classMethodStmts[$stmtPosition];
$currentStmt = $stmts[$stmtPosition];
/** @var Assign $assign */
$assign = $currentStmt->expr;
if ($this->hasCallLikeInAssignExpr($assign, $scope)) {
Expand Down Expand Up @@ -138,12 +139,15 @@ private function hasCallLikeInAssignExpr(Expr $expr, Scope $scope) : bool
return $this->sideEffectNodeDetector->detectCallExpr($subNode, $scope);
});
}
private function isVariableUsedInFollowingStmts(ClassMethod $classMethod, int $assignStmtPosition, string $variableName) : bool
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
*/
private function isVariableUsedInFollowingStmts($functionLike, int $assignStmtPosition, string $variableName) : bool
{
if ($classMethod->stmts === null) {
if ($functionLike->stmts === null) {
return \false;
}
foreach ($classMethod->stmts as $key => $stmt) {
foreach ($functionLike->stmts as $key => $stmt) {
// do not look yet
if ($key <= $assignStmtPosition) {
continue;
Expand All @@ -160,21 +164,24 @@ private function isVariableUsedInFollowingStmts(ClassMethod $classMethod, int $a
return \false;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node $node
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
*/
private function containsCompactFuncCall($node) : bool
private function containsCompactFuncCall($functionLike) : bool
{
$compactFuncCall = $this->betterNodeFinder->findFirst($node, function (Node $node) : bool {
$compactFuncCall = $this->betterNodeFinder->findFirst($functionLike, function (Node $node) : bool {
if (!$node instanceof FuncCall) {
return \false;
}
return $this->isName($node, 'compact');
});
return $compactFuncCall instanceof FuncCall;
}
private function containsFileIncludes(ClassMethod $classMethod) : bool
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
*/
private function containsFileIncludes($functionLike) : bool
{
return (bool) $this->betterNodeFinder->findInstancesOf($classMethod, [Include_::class]);
return (bool) $this->betterNodeFinder->findInstancesOf($functionLike, [Include_::class]);
}
/**
* @param array<int, Stmt> $stmts
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '0.18.8';
public const PACKAGE_VERSION = '7ec1be6b828042b27e3f40ed41b3285a6fe78502';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-11-14 18:44:59';
public const RELEASE_DATE = '2023-11-14 22:16:03';
/**
* @var int
*/
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@
},
{
"name": "illuminate\/container",
"version": "v10.31.0",
"version_normalized": "10.31.0.0",
"version": "v10.32.0",
"version_normalized": "10.32.0.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/illuminate\/container.git",
Expand Down Expand Up @@ -561,8 +561,8 @@
},
{
"name": "illuminate\/contracts",
"version": "v10.31.0",
"version_normalized": "10.31.0.0",
"version": "v10.32.0",
"version_normalized": "10.32.0.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/illuminate\/contracts.git",
Expand Down
Loading

0 comments on commit 9aef117

Please sign in to comment.