mirror of https://github.com/rectorphp/rector.git
84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\TypeDeclaration\Rector\Closure;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Closure;
|
|
use PHPStan\Analyser\Scope;
|
|
use Rector\Core\Rector\AbstractScopeAwareRector;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
|
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector\AddClosureReturnTypeRectorTest
|
|
*/
|
|
final class AddClosureReturnTypeRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
|
|
*/
|
|
private $returnTypeInferer;
|
|
public function __construct(ReturnTypeInferer $returnTypeInferer)
|
|
{
|
|
$this->returnTypeInferer = $returnTypeInferer;
|
|
}
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Add known return type to functions', [new CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run($meetups)
|
|
{
|
|
return array_filter($meetups, function (Meetup $meetup) {
|
|
return is_object($meetup);
|
|
});
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run($meetups)
|
|
{
|
|
return array_filter($meetups, function (Meetup $meetup): bool {
|
|
return is_object($meetup);
|
|
});
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [Closure::class];
|
|
}
|
|
/**
|
|
* @param Closure $node
|
|
*/
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
|
{
|
|
if ($node->returnType !== null) {
|
|
return null;
|
|
}
|
|
$inferedReturnType = $this->returnTypeInferer->inferFunctionLike($node);
|
|
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($inferedReturnType, TypeKind::RETURN);
|
|
if ($returnTypeNode === null) {
|
|
return null;
|
|
}
|
|
$node->returnType = $returnTypeNode;
|
|
return $node;
|
|
}
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return PhpVersionFeature::SCALAR_TYPES;
|
|
}
|
|
}
|