Skip to content

Commit

Permalink
Updated Rector to commit a11fc615d18396cef284c18de269711114630676
Browse files Browse the repository at this point in the history
rectorphp/rector-src@a11fc61 [TypeDeclaration] Remove only void type on ReturnedNodesReturnTypeInfererTypeInferer (#6340)
  • Loading branch information
TomasVotruba committed Oct 1, 2024
1 parent a5a5200 commit b86f338
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit b86f338

Please sign in to comment.