Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assigned new to variable in ReturnTypeFromReturnNewRector
- Loading branch information
1 parent
08ae610
commit 0f5a217
Showing
2 changed files
with
203 additions
and
29 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictReturnNewAnalyzer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\New_; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Expr\Yield_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
final class StrictReturnNewAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
private readonly NodeNameResolver $nodeNameResolver, | ||
) { | ||
} | ||
|
||
public function matchAlwaysReturnVariableNew(ClassMethod|Closure|Function_ $functionLike): ?string | ||
{ | ||
if ($functionLike->stmts === null) { | ||
return null; | ||
} | ||
|
||
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class])) { | ||
return null; | ||
} | ||
|
||
/** @var Return_[] $returns */ | ||
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class); | ||
if ($returns === []) { | ||
return null; | ||
} | ||
|
||
// is one statement depth 3? | ||
if (! $this->areExclusiveExprReturns($returns)) { | ||
return null; | ||
} | ||
|
||
// has root return? | ||
if (! $this->hasClassMethodRootReturn($functionLike)) { | ||
return null; | ||
} | ||
|
||
if (count($returns) !== 1) { | ||
return null; | ||
} | ||
|
||
// exact one return of variable | ||
$onlyReturn = $returns[0]; | ||
if (! $onlyReturn->expr instanceof Variable) { | ||
return null; | ||
} | ||
|
||
$createdVariablesToTypes = $this->resolveCreatedVariablesToTypes($functionLike); | ||
|
||
$returnedVariableName = $this->nodeNameResolver->getName($onlyReturn->expr); | ||
|
||
return $createdVariablesToTypes[$returnedVariableName] ?? null; | ||
} | ||
|
||
/** | ||
* @param Return_[] $returns | ||
*/ | ||
private function areExclusiveExprReturns(array $returns): bool | ||
{ | ||
foreach ($returns as $return) { | ||
if (! $return->expr instanceof Expr) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function hasClassMethodRootReturn(ClassMethod|Function_|Closure $functionLike): bool | ||
{ | ||
foreach ((array) $functionLike->stmts as $stmt) { | ||
if ($stmt instanceof Return_) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function resolveCreatedVariablesToTypes(ClassMethod|Function_|Closure $functionLike): array | ||
{ | ||
$createdVariablesToTypes = []; | ||
|
||
// what new is assigned to it? | ||
foreach ((array) $functionLike->stmts as $stmt) { | ||
if (! $stmt instanceof Expression) { | ||
continue; | ||
} | ||
|
||
if (! $stmt->expr instanceof Assign) { | ||
continue; | ||
} | ||
|
||
$assign = $stmt->expr; | ||
if (! $assign->expr instanceof New_) { | ||
continue; | ||
} | ||
|
||
if (! $assign->var instanceof Variable) { | ||
continue; | ||
} | ||
|
||
$variableName = $this->nodeNameResolver->getName($assign->var); | ||
if (! is_string($variableName)) { | ||
continue; | ||
} | ||
|
||
$className = $this->nodeNameResolver->getName($assign->expr->class); | ||
if (! is_string($className)) { | ||
continue; | ||
} | ||
|
||
$createdVariablesToTypes[$variableName] = $className; | ||
} | ||
|
||
return $createdVariablesToTypes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters