2021-02-19 12:01:23 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
2021-02-19 12:01:23 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\New_;
|
|
|
|
use PhpParser\Node\Name;
|
2022-07-09 14:55:41 +00:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2023-06-09 17:30:44 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
|
|
|
use PHPStan\Type\ObjectType;
|
2023-08-06 09:54:45 +00:00
|
|
|
use PHPStan\Type\ObjectWithoutClassType;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\StaticType;
|
2022-07-09 14:55:41 +00:00
|
|
|
use PHPStan\Type\Type;
|
2023-05-18 21:38:08 +00:00
|
|
|
use PHPStan\Type\UnionType;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
|
|
|
use Rector\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\NodeAnalyzer\ClassAnalyzer;
|
2023-08-06 09:54:45 +00:00
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver\NewTypeResolver;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
2023-09-29 12:48:57 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;
|
2022-07-09 14:55:41 +00:00
|
|
|
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictReturnNewAnalyzer;
|
2023-05-18 21:38:08 +00:00
|
|
|
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-11-28 20:42:41 +00:00
|
|
|
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-02-19 12:01:23 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\ReturnTypeFromReturnNewRectorTest
|
2021-02-19 12:01:23 +00:00
|
|
|
*/
|
2023-06-09 17:30:44 +00:00
|
|
|
final class ReturnTypeFromReturnNewRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
2021-02-19 12:01:23 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
|
2021-02-19 12:01:23 +00:00
|
|
|
*/
|
|
|
|
private $typeFactory;
|
2021-10-26 20:57:53 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-10-26 20:57:53 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
2022-05-04 10:46:11 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
2022-05-04 10:46:11 +00:00
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
2022-07-09 14:55:41 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-07-09 14:55:41 +00:00
|
|
|
* @var \Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictReturnNewAnalyzer
|
|
|
|
*/
|
|
|
|
private $strictReturnNewAnalyzer;
|
2022-11-28 20:42:41 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-11-28 20:42:41 +00:00
|
|
|
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
|
|
|
|
*/
|
|
|
|
private $classMethodReturnTypeOverrideGuard;
|
2023-05-18 21:38:08 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2023-05-18 21:38:08 +00:00
|
|
|
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
|
|
|
|
*/
|
|
|
|
private $returnTypeInferer;
|
2023-08-06 09:54:45 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\ClassAnalyzer
|
2023-08-06 09:54:45 +00:00
|
|
|
*/
|
|
|
|
private $classAnalyzer;
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver\NewTypeResolver
|
|
|
|
*/
|
|
|
|
private $newTypeResolver;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2023-09-29 12:48:57 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
|
|
|
public function __construct(TypeFactory $typeFactory, ReflectionProvider $reflectionProvider, ReflectionResolver $reflectionResolver, StrictReturnNewAnalyzer $strictReturnNewAnalyzer, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnTypeInferer $returnTypeInferer, ClassAnalyzer $classAnalyzer, NewTypeResolver $newTypeResolver, BetterNodeFinder $betterNodeFinder, StaticTypeMapper $staticTypeMapper)
|
2021-02-19 12:01:23 +00:00
|
|
|
{
|
|
|
|
$this->typeFactory = $typeFactory;
|
2021-10-26 20:57:53 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2022-05-04 10:46:11 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2022-07-09 14:55:41 +00:00
|
|
|
$this->strictReturnNewAnalyzer = $strictReturnNewAnalyzer;
|
2022-11-28 20:42:41 +00:00
|
|
|
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
|
2023-05-18 21:38:08 +00:00
|
|
|
$this->returnTypeInferer = $returnTypeInferer;
|
2023-08-06 09:54:45 +00:00
|
|
|
$this->classAnalyzer = $classAnalyzer;
|
|
|
|
$this->newTypeResolver = $newTypeResolver;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2023-09-29 12:48:57 +00:00
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
2021-02-19 12:01:23 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-02-19 12:01:23 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Add return type to function like with return new', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-02-19 12:01:23 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function action()
|
|
|
|
{
|
|
|
|
return new Response();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-02-19 12:01:23 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
2021-06-17 15:58:43 +00:00
|
|
|
public function action(): Response
|
2021-02-19 12:01:23 +00:00
|
|
|
{
|
|
|
|
return new Response();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-02-19 12:01:23 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-02-19 12:01:23 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-02-19 12:01:23 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
|
2021-02-19 12:01:23 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod|Function_|ArrowFunction $node
|
2021-02-19 12:01:23 +00:00
|
|
|
*/
|
2023-06-09 17:30:44 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2021-02-19 12:01:23 +00:00
|
|
|
{
|
|
|
|
if ($node->returnType !== null) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 17:30:44 +00:00
|
|
|
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
|
2022-11-28 20:42:41 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-09 14:55:41 +00:00
|
|
|
if (!$node instanceof ArrowFunction) {
|
|
|
|
$returnedNewClassName = $this->strictReturnNewAnalyzer->matchAlwaysReturnVariableNew($node);
|
|
|
|
if (\is_string($returnedNewClassName)) {
|
|
|
|
$node->returnType = new FullyQualified($returnedNewClassName);
|
|
|
|
return $node;
|
2021-02-19 12:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-09 14:55:41 +00:00
|
|
|
return $this->refactorDirectReturnNew($node);
|
2021-02-19 12:01:23 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::SCALAR_TYPES;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2021-10-26 20:57:53 +00:00
|
|
|
/**
|
2023-08-06 09:54:45 +00:00
|
|
|
* @return \PHPStan\Type\ObjectType|\PHPStan\Type\ObjectWithoutClassType|\PHPStan\Type\StaticType|null
|
2021-10-26 20:57:53 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createObjectTypeFromNew(New_ $new)
|
2021-10-26 20:57:53 +00:00
|
|
|
{
|
2023-08-06 09:54:45 +00:00
|
|
|
if ($this->classAnalyzer->isAnonymousClass($new->class)) {
|
|
|
|
$newType = $this->newTypeResolver->resolve($new);
|
|
|
|
if (!$newType instanceof ObjectWithoutClassType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $newType;
|
2023-08-06 09:54:38 +00:00
|
|
|
}
|
2023-08-06 09:54:45 +00:00
|
|
|
if (!$new->class instanceof Name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$className = $this->getName($new->class);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($className === ObjectReference::STATIC || $className === ObjectReference::SELF) {
|
2022-05-04 10:46:11 +00:00
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflection($new);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
|
|
|
throw new ShouldNotHappenException();
|
2021-10-26 20:57:53 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($className === ObjectReference::SELF) {
|
|
|
|
return new SelfStaticType($classReflection);
|
2021-10-26 20:57:53 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new StaticType($classReflection);
|
2021-10-26 20:57:53 +00:00
|
|
|
}
|
|
|
|
$classReflection = $this->reflectionProvider->getClass($className);
|
2022-06-07 08:22:29 +00:00
|
|
|
return new ObjectType($className, null, $classReflection);
|
2021-10-26 20:57:53 +00:00
|
|
|
}
|
2022-07-09 14:55:41 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\ArrowFunction|\PhpParser\Node\Expr\Closure $node
|
2022-08-11 14:46:08 +00:00
|
|
|
* @return null|\PhpParser\Node\Expr\ArrowFunction|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure
|
2022-07-09 14:55:41 +00:00
|
|
|
*/
|
|
|
|
private function refactorDirectReturnNew($node)
|
|
|
|
{
|
|
|
|
if ($node instanceof ArrowFunction) {
|
|
|
|
$returns = [new Return_($node->expr)];
|
|
|
|
} else {
|
|
|
|
/** @var Return_[] $returns */
|
|
|
|
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Return_::class);
|
|
|
|
}
|
|
|
|
if ($returns === []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$newTypes = $this->resolveReturnNewType($returns);
|
|
|
|
if ($newTypes === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$returnType = $this->typeFactory->createMixedPassedOrUnionType($newTypes);
|
|
|
|
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
|
|
|
|
if (!$returnTypeNode instanceof Node) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-18 21:38:08 +00:00
|
|
|
$returnType = $this->returnTypeInferer->inferFunctionLike($node);
|
|
|
|
if ($returnType instanceof UnionType) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-07-09 14:55:41 +00:00
|
|
|
$node->returnType = $returnTypeNode;
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Return_[] $returns
|
|
|
|
* @return Type[]|null
|
|
|
|
*/
|
|
|
|
private function resolveReturnNewType(array $returns) : ?array
|
|
|
|
{
|
|
|
|
$newTypes = [];
|
|
|
|
foreach ($returns as $return) {
|
|
|
|
if (!$return->expr instanceof New_) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-04 13:14:06 +00:00
|
|
|
$newType = $this->createObjectTypeFromNew($return->expr);
|
|
|
|
if (!$newType instanceof Type) {
|
2022-07-09 14:55:41 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-08-06 09:54:45 +00:00
|
|
|
$newTypes[] = $newType;
|
2022-07-09 14:55:41 +00:00
|
|
|
}
|
|
|
|
return $newTypes;
|
|
|
|
}
|
2021-02-19 12:01:23 +00:00
|
|
|
}
|