From b86f33859b4745a44baaf64ca6d98465b48bfcb8 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 1 Oct 2024 09:55:40 +0000 Subject: [PATCH] Updated Rector to commit a11fc615d18396cef284c18de269711114630676 https://github.com/rectorphp/rector-src/commit/a11fc615d18396cef284c18de269711114630676 [TypeDeclaration] Remove only void type on ReturnedNodesReturnTypeInfererTypeInferer (#6340) --- .../NodeManipulator/AddReturnTypeFromCast.php | 2 +- ...turnTypeFromStrictConstantReturnRector.php | 13 ------ .../Closure/ClosureReturnTypeRector.php | 3 +- ...urnedNodesReturnTypeInfererTypeInferer.php | 43 +++++++------------ src/Application/VersionResolver.php | 4 +- 5 files changed, 19 insertions(+), 46 deletions(-) diff --git a/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php b/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php index 317fabcc3b2..023d08925b7 100644 --- a/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php +++ b/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php @@ -63,7 +63,7 @@ public function add($functionLike, Scope $scope) return null; } $returnType = $this->returnTypeInferer->inferFunctionLike($functionLike); - if ($returnType instanceof UnionType || $returnType->isVoid()->yes()) { + if ($returnType instanceof UnionType) { return null; } $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN); diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php index 2f9e45a657f..7a84591e7f2 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php @@ -6,10 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ConstFetch; -use PhpParser\Node\Expr\Yield_; -use PhpParser\Node\Expr\YieldFrom; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use PHPStan\Type\Type; @@ -103,9 +100,6 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node if ($node->returnType instanceof Node) { return null; } - if ($this->hasYield($node)) { - return null; - } $returns = $this->betterNodeFinder->findReturnsScoped($node); if (!$this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; @@ -145,11 +139,4 @@ private function matchAlwaysReturnConstFetch(array $returns) : ?Type } return $this->typeFactory->createMixedPassedOrUnionType($classConstFetchTypes); } - /** - * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike - */ - private function hasYield($functionLike) : bool - { - return $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class, YieldFrom::class]); - } } diff --git a/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeRector.php b/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeRector.php index 6a6642e45ad..0da137c6d81 100644 --- a/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeRector.php @@ -6,7 +6,6 @@ use PhpParser\Node; use PhpParser\Node\Expr\Closure; use PHPStan\Type\NeverType; -use PHPStan\Type\VoidType; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -67,7 +66,7 @@ public function refactor(Node $node) : ?Node } $closureReturnType = $this->returnTypeInferer->inferFunctionLike($node); // handled by other rules - if ($closureReturnType instanceof VoidType || $closureReturnType instanceof NeverType) { + if ($closureReturnType instanceof NeverType) { return null; } $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($closureReturnType, TypeKind::RETURN); diff --git a/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php b/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php index c5832702509..9e81c1fd919 100644 --- a/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php +++ b/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php @@ -5,7 +5,6 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PHPStan\Reflection\ClassReflection; @@ -72,41 +71,29 @@ public function inferFunctionLike($functionLike) : Type return new MixedType(); } $types = []; + // empty returns can have yield, use MixedType() instead $localReturnNodes = $this->betterNodeFinder->findReturnsScoped($functionLike); if ($localReturnNodes === []) { - return $this->resolveNoLocalReturnNodes($functionLike, $classReflection); + return new MixedType(); } + $hasVoid = \false; foreach ($localReturnNodes as $localReturnNode) { - $returnedExprType = $localReturnNode->expr instanceof Expr ? $this->nodeTypeResolver->getNativeType($localReturnNode->expr) : new VoidType(); + if (!$localReturnNode->expr instanceof Expr) { + $hasVoid = \true; + $types[] = new VoidType(); + continue; + } + $returnedExprType = $this->nodeTypeResolver->getNativeType($localReturnNode->expr); $types[] = $this->splArrayFixedTypeNarrower->narrow($returnedExprType); } - if ($this->silentVoidResolver->hasSilentVoid($functionLike)) { + if (!$hasVoid && $this->silentVoidResolver->hasSilentVoid($functionLike)) { $types[] = new VoidType(); } - return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($types); - } - /** - * @return \PHPStan\Type\VoidType|\PHPStan\Type\MixedType - */ - private function resolveNoLocalReturnNodes(FunctionLike $functionLike, ?ClassReflection $classReflection) - { - // void type - if (!$this->isAbstractMethod($functionLike, $classReflection)) { - return new VoidType(); - } - return new MixedType(); - } - private function isAbstractMethod(FunctionLike $functionLike, ?ClassReflection $classReflection) : bool - { - if ($functionLike instanceof ClassMethod && $functionLike->isAbstract()) { - return \true; - } - if (!$classReflection instanceof ClassReflection) { - return \false; - } - if (!$classReflection->isClass()) { - return \false; + $returnType = $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($types); + // only void? + if ($returnType->isVoid()->yes()) { + return new MixedType(); } - return $classReflection->isAbstract(); + return $returnType; } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 1081b1c102b..82c2b5c46ee 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 = '54a66206986e685787d7e038929618a66e98ec42'; + public const PACKAGE_VERSION = 'a11fc615d18396cef284c18de269711114630676'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-10-01 12:25:35'; + public const RELEASE_DATE = '2024-10-01 16:53:21'; /** * @var int */