Skip to content

Commit b86f338

Browse files
committed
Updated Rector to commit a11fc615d18396cef284c18de269711114630676
rectorphp/rector-src@a11fc61 [TypeDeclaration] Remove only void type on ReturnedNodesReturnTypeInfererTypeInferer (#6340)
1 parent a5a5200 commit b86f338

File tree

5 files changed

+19
-46
lines changed

5 files changed

+19
-46
lines changed

rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function add($functionLike, Scope $scope)
6363
return null;
6464
}
6565
$returnType = $this->returnTypeInferer->inferFunctionLike($functionLike);
66-
if ($returnType instanceof UnionType || $returnType->isVoid()->yes()) {
66+
if ($returnType instanceof UnionType) {
6767
return null;
6868
}
6969
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);

rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
use PhpParser\Node;
77
use PhpParser\Node\Expr\ClassConstFetch;
88
use PhpParser\Node\Expr\ConstFetch;
9-
use PhpParser\Node\Expr\Yield_;
10-
use PhpParser\Node\Expr\YieldFrom;
119
use PhpParser\Node\Stmt\ClassMethod;
12-
use PhpParser\Node\Stmt\Function_;
1310
use PhpParser\Node\Stmt\Return_;
1411
use PHPStan\Analyser\Scope;
1512
use PHPStan\Type\Type;
@@ -103,9 +100,6 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node
103100
if ($node->returnType instanceof Node) {
104101
return null;
105102
}
106-
if ($this->hasYield($node)) {
107-
return null;
108-
}
109103
$returns = $this->betterNodeFinder->findReturnsScoped($node);
110104
if (!$this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) {
111105
return null;
@@ -145,11 +139,4 @@ private function matchAlwaysReturnConstFetch(array $returns) : ?Type
145139
}
146140
return $this->typeFactory->createMixedPassedOrUnionType($classConstFetchTypes);
147141
}
148-
/**
149-
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
150-
*/
151-
private function hasYield($functionLike) : bool
152-
{
153-
return $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class, YieldFrom::class]);
154-
}
155142
}

rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeRector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node;
77
use PhpParser\Node\Expr\Closure;
88
use PHPStan\Type\NeverType;
9-
use PHPStan\Type\VoidType;
109
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1110
use Rector\Rector\AbstractRector;
1211
use Rector\StaticTypeMapper\StaticTypeMapper;
@@ -67,7 +66,7 @@ public function refactor(Node $node) : ?Node
6766
}
6867
$closureReturnType = $this->returnTypeInferer->inferFunctionLike($node);
6968
// handled by other rules
70-
if ($closureReturnType instanceof VoidType || $closureReturnType instanceof NeverType) {
69+
if ($closureReturnType instanceof NeverType) {
7170
return null;
7271
}
7372
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($closureReturnType, TypeKind::RETURN);

rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Expr\Closure;
8-
use PhpParser\Node\FunctionLike;
98
use PhpParser\Node\Stmt\ClassMethod;
109
use PhpParser\Node\Stmt\Function_;
1110
use PHPStan\Reflection\ClassReflection;
@@ -72,41 +71,29 @@ public function inferFunctionLike($functionLike) : Type
7271
return new MixedType();
7372
}
7473
$types = [];
74+
// empty returns can have yield, use MixedType() instead
7575
$localReturnNodes = $this->betterNodeFinder->findReturnsScoped($functionLike);
7676
if ($localReturnNodes === []) {
77-
return $this->resolveNoLocalReturnNodes($functionLike, $classReflection);
77+
return new MixedType();
7878
}
79+
$hasVoid = \false;
7980
foreach ($localReturnNodes as $localReturnNode) {
80-
$returnedExprType = $localReturnNode->expr instanceof Expr ? $this->nodeTypeResolver->getNativeType($localReturnNode->expr) : new VoidType();
81+
if (!$localReturnNode->expr instanceof Expr) {
82+
$hasVoid = \true;
83+
$types[] = new VoidType();
84+
continue;
85+
}
86+
$returnedExprType = $this->nodeTypeResolver->getNativeType($localReturnNode->expr);
8187
$types[] = $this->splArrayFixedTypeNarrower->narrow($returnedExprType);
8288
}
83-
if ($this->silentVoidResolver->hasSilentVoid($functionLike)) {
89+
if (!$hasVoid && $this->silentVoidResolver->hasSilentVoid($functionLike)) {
8490
$types[] = new VoidType();
8591
}
86-
return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($types);
87-
}
88-
/**
89-
* @return \PHPStan\Type\VoidType|\PHPStan\Type\MixedType
90-
*/
91-
private function resolveNoLocalReturnNodes(FunctionLike $functionLike, ?ClassReflection $classReflection)
92-
{
93-
// void type
94-
if (!$this->isAbstractMethod($functionLike, $classReflection)) {
95-
return new VoidType();
96-
}
97-
return new MixedType();
98-
}
99-
private function isAbstractMethod(FunctionLike $functionLike, ?ClassReflection $classReflection) : bool
100-
{
101-
if ($functionLike instanceof ClassMethod && $functionLike->isAbstract()) {
102-
return \true;
103-
}
104-
if (!$classReflection instanceof ClassReflection) {
105-
return \false;
106-
}
107-
if (!$classReflection->isClass()) {
108-
return \false;
92+
$returnType = $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($types);
93+
// only void?
94+
if ($returnType->isVoid()->yes()) {
95+
return new MixedType();
10996
}
110-
return $classReflection->isAbstract();
97+
return $returnType;
11198
}
11299
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '54a66206986e685787d7e038929618a66e98ec42';
22+
public const PACKAGE_VERSION = 'a11fc615d18396cef284c18de269711114630676';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2024-10-01 12:25:35';
27+
public const RELEASE_DATE = '2024-10-01 16:53:21';
2828
/**
2929
* @var int
3030
*/

0 commit comments

Comments
 (0)