2020-09-01 17:56:30 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\TypeDeclaration\TypeInferer;
|
2020-09-01 17:56:30 +00:00
|
|
|
|
2023-09-06 12:33:43 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
2024-03-22 16:53:50 +00:00
|
|
|
use PhpParser\Node\Expr\Exit_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Yield_;
|
2024-02-03 22:50:20 +00:00
|
|
|
use PhpParser\Node\Expr\YieldFrom;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\FunctionLike;
|
|
|
|
use PhpParser\Node\Stmt;
|
2024-03-23 20:33:11 +00:00
|
|
|
use PhpParser\Node\Stmt\Break_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2024-03-23 20:33:11 +00:00
|
|
|
use PhpParser\Node\Stmt\Continue_;
|
2024-03-23 05:12:50 +00:00
|
|
|
use PhpParser\Node\Stmt\Do_;
|
2024-03-22 16:53:50 +00:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2024-03-22 16:53:50 +00:00
|
|
|
use PhpParser\Node\Stmt\Finally_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2024-03-25 15:52:31 +00:00
|
|
|
use PhpParser\Node\Stmt\Goto_;
|
2024-03-22 16:53:50 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use PhpParser\Node\Stmt\Switch_;
|
|
|
|
use PhpParser\Node\Stmt\Throw_;
|
|
|
|
use PhpParser\Node\Stmt\TryCatch;
|
2023-06-12 18:32:15 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
2020-09-01 17:56:30 +00:00
|
|
|
final class SilentVoidResolver
|
|
|
|
{
|
2021-02-16 16:12:37 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2021-02-16 16:12:37 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2023-06-12 18:32:15 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
2023-06-12 18:32:15 +00:00
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
|
|
|
public function __construct(BetterNodeFinder $betterNodeFinder, ReflectionResolver $reflectionResolver)
|
2021-02-16 16:12:37 +00:00
|
|
|
{
|
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2023-06-12 18:32:15 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2021-02-16 16:12:37 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
|
2021-02-16 16:12:37 +00:00
|
|
|
*/
|
2021-06-07 23:40:38 +00:00
|
|
|
public function hasExclusiveVoid($functionLike) : bool
|
2021-02-16 16:12:37 +00:00
|
|
|
{
|
2023-06-12 18:32:15 +00:00
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflection($functionLike);
|
|
|
|
if ($classReflection instanceof ClassReflection && $classReflection->isInterface()) {
|
2021-11-07 04:15:35 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2024-02-03 22:50:20 +00:00
|
|
|
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class, YieldFrom::class])) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-16 16:12:37 +00:00
|
|
|
}
|
2023-09-07 04:03:06 +00:00
|
|
|
$return = $this->betterNodeFinder->findFirstInFunctionLikeScoped($functionLike, static function (Node $node) : bool {
|
|
|
|
return $node instanceof Return_ && $node->expr instanceof Expr;
|
|
|
|
});
|
|
|
|
return !$return instanceof Return_;
|
2021-02-16 16:12:37 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function hasSilentVoid(FunctionLike $functionLike) : bool
|
2020-09-01 17:56:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($functionLike instanceof ArrowFunction) {
|
2021-07-31 11:40:13 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2024-03-22 16:53:50 +00:00
|
|
|
$stmts = (array) $functionLike->getStmts();
|
|
|
|
return !$this->hasStmtsAlwaysReturnOrExit($stmts);
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Stmt[]|Expression[] $stmts
|
|
|
|
*/
|
2024-03-22 16:53:50 +00:00
|
|
|
private function hasStmtsAlwaysReturnOrExit(array $stmts) : bool
|
2020-09-01 17:56:30 +00:00
|
|
|
{
|
|
|
|
foreach ($stmts as $stmt) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($stmt instanceof Expression) {
|
2020-09-01 17:56:30 +00:00
|
|
|
$stmt = $stmt->expr;
|
|
|
|
}
|
2024-03-22 16:53:50 +00:00
|
|
|
if ($this->isStopped($stmt)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
// has switch with always return
|
2024-03-22 20:34:36 +00:00
|
|
|
if ($stmt instanceof Switch_ && $this->isSwitchWithAlwaysReturnOrExit($stmt)) {
|
2024-03-22 16:53:50 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2024-03-22 20:34:36 +00:00
|
|
|
if ($stmt instanceof TryCatch && $this->isTryCatchAlwaysReturnOrExit($stmt)) {
|
2024-03-22 16:53:50 +00:00
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
if ($this->isIfReturn($stmt)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2024-03-23 20:33:11 +00:00
|
|
|
if ($stmt instanceof Do_ && $this->isDoWithAlwaysReturnOrExit($stmt)) {
|
2024-03-23 05:12:50 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2024-03-23 20:33:11 +00:00
|
|
|
private function isDoWithAlwaysReturnOrExit(Do_ $do) : bool
|
|
|
|
{
|
|
|
|
if (!$this->hasStmtsAlwaysReturnOrExit($do->stmts)) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return !(bool) $this->betterNodeFinder->findFirst($do->stmts, static function (Node $node) : bool {
|
2024-03-25 15:52:31 +00:00
|
|
|
return $node instanceof Break_ || $node instanceof Continue_ || $node instanceof Goto_;
|
2024-03-23 20:33:11 +00:00
|
|
|
});
|
|
|
|
}
|
2024-03-22 16:53:50 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt|\PhpParser\Node\Expr $stmt
|
|
|
|
*/
|
|
|
|
private function isIfReturn($stmt) : bool
|
|
|
|
{
|
|
|
|
if (!$stmt instanceof If_) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
foreach ($stmt->elseifs as $elseIf) {
|
|
|
|
if (!$this->hasStmtsAlwaysReturnOrExit($elseIf->stmts)) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$stmt->else instanceof Else_) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if (!$this->hasStmtsAlwaysReturnOrExit($stmt->stmts)) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return $this->hasStmtsAlwaysReturnOrExit($stmt->else->stmts);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt|\PhpParser\Node\Expr $stmt
|
|
|
|
*/
|
|
|
|
private function isStopped($stmt) : bool
|
|
|
|
{
|
2024-03-25 08:45:48 +00:00
|
|
|
return $stmt instanceof Throw_ || $stmt instanceof Exit_ || $stmt instanceof Return_ && $stmt->expr instanceof Expr || $stmt instanceof Yield_ || $stmt instanceof YieldFrom;
|
2024-03-22 16:53:50 +00:00
|
|
|
}
|
2024-03-22 20:34:36 +00:00
|
|
|
private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch) : bool
|
2020-09-01 17:56:30 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasDefault = \false;
|
2021-05-01 12:16:35 +00:00
|
|
|
foreach ($switch->cases as $case) {
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$case->cond instanceof Expr) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasDefault = \true;
|
2021-05-01 12:33:28 +00:00
|
|
|
break;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$hasDefault) {
|
|
|
|
return \false;
|
2021-05-01 12:16:35 +00:00
|
|
|
}
|
2024-03-22 20:34:36 +00:00
|
|
|
$casesWithReturnOrExitCount = $this->resolveReturnOrExitCount($switch);
|
|
|
|
// has same amount of first return or exit nodes as switches
|
|
|
|
return \count($switch->cases) === $casesWithReturnOrExitCount;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2024-03-22 20:34:36 +00:00
|
|
|
private function isTryCatchAlwaysReturnOrExit(TryCatch $tryCatch) : bool
|
2020-09-01 17:56:30 +00:00
|
|
|
{
|
2024-03-22 16:53:50 +00:00
|
|
|
if (!$this->hasStmtsAlwaysReturnOrExit($tryCatch->stmts)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
|
|
|
foreach ($tryCatch->catches as $catch) {
|
2024-03-22 16:53:50 +00:00
|
|
|
if (!$this->hasStmtsAlwaysReturnOrExit($catch->stmts)) {
|
|
|
|
return \false;
|
|
|
|
}
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2024-03-22 16:53:50 +00:00
|
|
|
return !($tryCatch->finally instanceof Finally_ && !$this->hasStmtsAlwaysReturnOrExit($tryCatch->finally->stmts));
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2024-03-22 20:34:36 +00:00
|
|
|
private function resolveReturnOrExitCount(Switch_ $switch) : int
|
2021-05-01 12:16:35 +00:00
|
|
|
{
|
|
|
|
$casesWithReturnCount = 0;
|
|
|
|
foreach ($switch->cases as $case) {
|
2024-03-22 20:34:36 +00:00
|
|
|
if ($this->hasStmtsAlwaysReturnOrExit($case->stmts)) {
|
2021-05-01 12:16:35 +00:00
|
|
|
++$casesWithReturnCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $casesWithReturnCount;
|
|
|
|
}
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|