Updated Rector to commit 8874f25347004e0c19043e8b97b69470193f0941

8874f25347 [CodeQuality] Move Yield_ and YieldFrom check to SilentVoidResolver (#5759)
This commit is contained in:
Tomas Votruba 2024-03-22 20:34:36 +00:00
parent 024ebc62a1
commit e6bfc79aa2
3 changed files with 13 additions and 27 deletions

View File

@ -5,12 +5,9 @@ namespace Rector\CodeQuality\Rector\ClassMethod;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Type\NullType; use PHPStan\Type\NullType;
use PHPStan\Type\UnionType; use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType; use PHPStan\Type\VoidType;
@ -114,16 +111,13 @@ CODE_SAMPLE
if ($this->isName($node, MethodName::CONSTRUCT)) { if ($this->isName($node, MethodName::CONSTRUCT)) {
return null; return null;
} }
if ($this->containsYieldOrThrow($node)) { if (!$this->silentVoidResolver->hasSilentVoid($node)) {
return null; return null;
} }
// it has at least some return value in it // it has at least some return value in it
if (!$this->hasReturnsWithValues($node)) { if (!$this->hasReturnsWithValues($node)) {
return null; return null;
} }
if (!$this->silentVoidResolver->hasSilentVoid($node)) {
return null;
}
$node->stmts[] = new Return_(new ConstFetch(new Name('null'))); $node->stmts[] = new Return_(new ConstFetch(new Name('null')));
$this->transformDocUnionVoidToUnionNull($node); $this->transformDocUnionVoidToUnionNull($node);
return $node; return $node;
@ -153,10 +147,6 @@ CODE_SAMPLE
} }
$this->phpDocTypeChanger->changeReturnType($classMethod, $phpDocInfo, $type); $this->phpDocTypeChanger->changeReturnType($classMethod, $phpDocInfo, $type);
} }
private function containsYieldOrThrow(ClassMethod $classMethod) : bool
{
return (bool) $this->betterNodeFinder->findInstancesOf($classMethod, [Yield_::class, Throw_::class, Node\Expr\Throw_::class, YieldFrom::class]);
}
private function hasReturnsWithValues(ClassMethod $classMethod) : bool private function hasReturnsWithValues(ClassMethod $classMethod) : bool
{ {
/** @var Return_[] $returns */ /** @var Return_[] $returns */

View File

@ -80,10 +80,10 @@ final class SilentVoidResolver
return \true; return \true;
} }
// has switch with always return // has switch with always return
if ($stmt instanceof Switch_ && $this->isSwitchWithAlwaysReturn($stmt)) { if ($stmt instanceof Switch_ && $this->isSwitchWithAlwaysReturnOrExit($stmt)) {
return \true; return \true;
} }
if ($stmt instanceof TryCatch && $this->isTryCatchAlwaysReturn($stmt)) { if ($stmt instanceof TryCatch && $this->isTryCatchAlwaysReturnOrExit($stmt)) {
return \true; return \true;
} }
if ($this->isIfReturn($stmt)) { if ($this->isIfReturn($stmt)) {
@ -118,9 +118,9 @@ final class SilentVoidResolver
*/ */
private function isStopped($stmt) : bool private function isStopped($stmt) : bool
{ {
return $stmt instanceof Throw_ || $stmt instanceof Exit_ || $stmt instanceof Return_; return $stmt instanceof Throw_ || $stmt instanceof Exit_ || $stmt instanceof Return_ || $stmt instanceof Yield_ || $stmt instanceof YieldFrom;
} }
private function isSwitchWithAlwaysReturn(Switch_ $switch) : bool private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch) : bool
{ {
$hasDefault = \false; $hasDefault = \false;
foreach ($switch->cases as $case) { foreach ($switch->cases as $case) {
@ -132,11 +132,11 @@ final class SilentVoidResolver
if (!$hasDefault) { if (!$hasDefault) {
return \false; return \false;
} }
$casesWithReturnCount = $this->resolveReturnCount($switch); $casesWithReturnOrExitCount = $this->resolveReturnOrExitCount($switch);
// has same amount of returns as switches // has same amount of first return or exit nodes as switches
return \count($switch->cases) === $casesWithReturnCount; return \count($switch->cases) === $casesWithReturnOrExitCount;
} }
private function isTryCatchAlwaysReturn(TryCatch $tryCatch) : bool private function isTryCatchAlwaysReturnOrExit(TryCatch $tryCatch) : bool
{ {
if (!$this->hasStmtsAlwaysReturnOrExit($tryCatch->stmts)) { if (!$this->hasStmtsAlwaysReturnOrExit($tryCatch->stmts)) {
return \false; return \false;
@ -148,16 +148,12 @@ final class SilentVoidResolver
} }
return !($tryCatch->finally instanceof Finally_ && !$this->hasStmtsAlwaysReturnOrExit($tryCatch->finally->stmts)); return !($tryCatch->finally instanceof Finally_ && !$this->hasStmtsAlwaysReturnOrExit($tryCatch->finally->stmts));
} }
private function resolveReturnCount(Switch_ $switch) : int private function resolveReturnOrExitCount(Switch_ $switch) : int
{ {
$casesWithReturnCount = 0; $casesWithReturnCount = 0;
foreach ($switch->cases as $case) { foreach ($switch->cases as $case) {
foreach ($case->stmts as $caseStmt) { if ($this->hasStmtsAlwaysReturnOrExit($case->stmts)) {
if (!$caseStmt instanceof Return_) {
continue;
}
++$casesWithReturnCount; ++$casesWithReturnCount;
break;
} }
} }
return $casesWithReturnCount; return $casesWithReturnCount;

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = 'b042152eae866b5578ea15cb38e27ce493937001'; public const PACKAGE_VERSION = '8874f25347004e0c19043e8b97b69470193f0941';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2024-03-23 02:53:17'; public const RELEASE_DATE = '2024-03-23 03:32:10';
/** /**
* @var int * @var int
*/ */