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\Expr\ConstFetch;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Type\NullType;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
@ -114,16 +111,13 @@ CODE_SAMPLE
if ($this->isName($node, MethodName::CONSTRUCT)) {
return null;
}
if ($this->containsYieldOrThrow($node)) {
if (!$this->silentVoidResolver->hasSilentVoid($node)) {
return null;
}
// it has at least some return value in it
if (!$this->hasReturnsWithValues($node)) {
return null;
}
if (!$this->silentVoidResolver->hasSilentVoid($node)) {
return null;
}
$node->stmts[] = new Return_(new ConstFetch(new Name('null')));
$this->transformDocUnionVoidToUnionNull($node);
return $node;
@ -153,10 +147,6 @@ CODE_SAMPLE
}
$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
{
/** @var Return_[] $returns */

View File

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

View File

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