From e6bfc79aa22bb42a79ddf0cda897d5cc8fb27828 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 22 Mar 2024 20:34:36 +0000 Subject: [PATCH] Updated Rector to commit 8874f25347004e0c19043e8b97b69470193f0941 https://github.com/rectorphp/rector-src/commit/8874f25347004e0c19043e8b97b69470193f0941 [CodeQuality] Move Yield_ and YieldFrom check to SilentVoidResolver (#5759) --- .../ClassMethod/ExplicitReturnNullRector.php | 12 +--------- .../TypeInferer/SilentVoidResolver.php | 24 ++++++++----------- src/Application/VersionResolver.php | 4 ++-- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php index 6e63e5c6f3c..e98dfc02738 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php @@ -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 */ diff --git a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php index fcf8d11cff4..92b5a2b9504 100644 --- a/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php +++ b/rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php @@ -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; diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 90757623e35..718074ed608 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -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 */