Skip to content

Commit

Permalink
[TypeDeclaration] Skip possible void on ReturnTypeFromReturnNewRector (
Browse files Browse the repository at this point in the history
…#3890)

* [TypeDeclaration] Skip possible void on ReturnTypeFromReturnNewRector

* Fixed 🎉

* eol

* increase cache key
  • Loading branch information
samsonasik committed May 18, 2023
1 parent cee58fb commit af2a06e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

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

class SkipPossibleVoid
{
public function bar()
{
if (random_int(0,1)) {
return new self();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
Expand All @@ -27,6 +28,7 @@
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictReturnNewAnalyzer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -42,7 +44,8 @@ public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly ReflectionResolver $reflectionResolver,
private readonly StrictReturnNewAnalyzer $strictReturnNewAnalyzer,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
private readonly ReturnTypeInferer $returnTypeInferer
) {
}

Expand Down Expand Up @@ -161,6 +164,11 @@ private function refactorDirectReturnNew(
return null;
}

$returnType = $this->returnTypeInferer->inferFunctionLike($node);
if ($returnType instanceof UnionType) {
return null;
}

$node->returnType = $returnTypeNode;

return $node;
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v13';
private const CACHE_KEY = 'v14';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit af2a06e

Please sign in to comment.