Skip to content

Commit

Permalink
[TypeDeclaration] Skip multi return types on ReturnTypeFromReturnDire…
Browse files Browse the repository at this point in the history
…ctArrayRector (#3164)

* [TypeDeclaration] Skip multi return types on ReturnTypeFromReturnDirectArrayRector

* Fixed 🎉

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* Fix

* Fix

* [ci-review] Rector Rectify

* Fix

* Final touch: eol

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 7, 2022
1 parent f35326d commit 045ada5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector\Fixture;

final class SkipMultiTypesReturn
{
public function getArray($action)
{
switch ($action) {
case 'save':
return 17;
default:
return null;
}
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -25,7 +26,8 @@
final class ReturnTypeFromReturnDirectArrayRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
private readonly ReturnTypeInferer $returnTypeInferer
) {
}

Expand Down Expand Up @@ -82,6 +84,11 @@ public function refactor(Node $node): ?Node
return null;
}

$type = $this->returnTypeInferer->inferFunctionLike($node);
if (! $type->isArray()->yes()) {
return null;
}

$node->returnType = new Identifier('array');

return $node;
Expand Down
27 changes: 21 additions & 6 deletions rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function __construct(
) {
}

public function inferFunctionLike(ClassMethod|Function_|Closure $functionLike): Type
public function inferFunctionLike(ClassMethod|Function_|Closure|ArrowFunction $functionLike): Type
{
$isSupportedStaticReturnType = $this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::STATIC_RETURN_TYPE
Expand Down Expand Up @@ -120,9 +121,15 @@ public function verifyThisType(Type $type, FunctionLike $functionLike): ?Type
return new MixedType();
}

private function resolveTypeWithVoidHandling(ClassMethod|Function_|Closure $functionLike, Type $resolvedType): Type
{
private function resolveTypeWithVoidHandling(
ClassMethod|Function_|Closure|ArrowFunction $functionLike,
Type $resolvedType
): Type {
if ($resolvedType instanceof VoidType) {
if ($functionLike instanceof ArrowFunction) {
return new MixedType();
}

$hasReturnValue = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$functionLike,
static function (Node $subNode): bool {
Expand Down Expand Up @@ -150,7 +157,7 @@ static function (Node $subNode): bool {
}

private function resolveBenevolentUnionTypeInteger(
ClassMethod|Function_|Closure $functionLike,
ClassMethod|Function_|Closure|ArrowFunction $functionLike,
UnionType $unionType
): UnionType|IntegerType {
$types = $unionType->getTypes();
Expand All @@ -164,8 +171,16 @@ private function resolveBenevolentUnionTypeInteger(
return $unionType;
}

$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class);
$returnsWithExpr = array_filter($returns, static fn (Return_ $return): bool => $return->expr instanceof Expr);
if (! $functionLike instanceof ArrowFunction) {
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class);
$returnsWithExpr = array_filter(
$returns,
static fn (Return_ $return): bool => $return->expr instanceof Expr
);
} else {
$returns = $functionLike->getStmts();
$returnsWithExpr = $returns;
}

if ($returns !== $returnsWithExpr) {
return $unionType;
Expand Down

0 comments on commit 045ada5

Please sign in to comment.