mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
f24ba3e919
ae55eac4c9
[TypeDeclaration] Allow Function_ and ArrowFunction on ReturnTypeFromReturnNewRector (#583)
102 lines
3.5 KiB
PHP
102 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
use PhpParser\Node\Expr\New_;
|
|
use PhpParser\Node\Name;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PhpParser\Node\Stmt\Function_;
|
|
use PhpParser\Node\Stmt\Return_;
|
|
use PHPStan\Type\ObjectType;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
|
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\ReturnTypeFromReturnNewRectorTest
|
|
*/
|
|
final class ReturnTypeFromReturnNewRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
|
|
{
|
|
/**
|
|
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
|
|
*/
|
|
private $typeFactory;
|
|
public function __construct(\Rector\NodeTypeResolver\PHPStan\Type\TypeFactory $typeFactory)
|
|
{
|
|
$this->typeFactory = $typeFactory;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Add return type to function like with return new', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
public function action()
|
|
{
|
|
return new Response();
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
public function action(): Response
|
|
{
|
|
return new Response();
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Function_::class, \PhpParser\Node\Expr\ArrowFunction::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod|Function_|ArrowFunction $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if ($node->returnType !== null) {
|
|
return null;
|
|
}
|
|
if ($node instanceof \PhpParser\Node\Expr\ArrowFunction) {
|
|
$returns = [new \PhpParser\Node\Stmt\Return_($node->expr)];
|
|
} else {
|
|
/** @var Return_[] $returns */
|
|
$returns = $this->betterNodeFinder->findInstanceOf((array) $node->stmts, \PhpParser\Node\Stmt\Return_::class);
|
|
}
|
|
if ($returns === []) {
|
|
return null;
|
|
}
|
|
$newTypes = [];
|
|
foreach ($returns as $return) {
|
|
if (!$return->expr instanceof \PhpParser\Node\Expr\New_) {
|
|
return null;
|
|
}
|
|
$new = $return->expr;
|
|
if (!$new->class instanceof \PhpParser\Node\Name) {
|
|
return null;
|
|
}
|
|
$className = $this->getName($new->class);
|
|
$newTypes[] = new \PHPStan\Type\ObjectType($className);
|
|
}
|
|
$returnType = $this->typeFactory->createMixedPassedOrUnionType($newTypes);
|
|
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, \Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind::RETURN());
|
|
$node->returnType = $returnTypeNode;
|
|
return $node;
|
|
}
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return \Rector\Core\ValueObject\PhpVersionFeature::SCALAR_TYPES;
|
|
}
|
|
}
|