mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-05 02:40:50 +00:00
7e7a2f067a
476cfb00cb
disable fallback for now
117 lines
3.7 KiB
PHP
117 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Array_;
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
use PhpParser\Node\Expr\Closure;
|
|
use PhpParser\Node\Identifier;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PhpParser\Node\Stmt\Function_;
|
|
use PhpParser\Node\Stmt\Return_;
|
|
use PHPStan\Analyser\Scope;
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
|
use Rector\ValueObject\PhpVersionFeature;
|
|
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector\ReturnTypeFromReturnDirectArrayRectorTest
|
|
*/
|
|
final class ReturnTypeFromReturnDirectArrayRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
|
|
*/
|
|
private $classMethodReturnTypeOverrideGuard;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
|
|
*/
|
|
private $returnTypeInferer;
|
|
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnTypeInferer $returnTypeInferer)
|
|
{
|
|
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
|
|
$this->returnTypeInferer = $returnTypeInferer;
|
|
}
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Add return type from return direct array', [new CodeSample(<<<'CODE_SAMPLE'
|
|
final class AddReturnArray
|
|
{
|
|
public function getArray()
|
|
{
|
|
return [1, 2, 3];
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
final class AddReturnArray
|
|
{
|
|
public function getArray(): array
|
|
{
|
|
return [1, 2, 3];
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod|Function_|ArrowFunction $node
|
|
*/
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
|
{
|
|
if ($node->returnType !== null) {
|
|
return null;
|
|
}
|
|
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
|
|
return null;
|
|
}
|
|
if (!$this->hasReturnArray($node)) {
|
|
return null;
|
|
}
|
|
$type = $this->returnTypeInferer->inferFunctionLike($node);
|
|
if (!$type->isArray()->yes()) {
|
|
return null;
|
|
}
|
|
$node->returnType = new Identifier('array');
|
|
return $node;
|
|
}
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return PhpVersionFeature::SCALAR_TYPES;
|
|
}
|
|
/**
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $functionLike
|
|
*/
|
|
private function hasReturnArray($functionLike) : bool
|
|
{
|
|
$stmts = $functionLike instanceof ArrowFunction ? $functionLike->getStmts() : $functionLike->stmts;
|
|
if (!\is_array($stmts)) {
|
|
return \false;
|
|
}
|
|
foreach ($stmts as $stmt) {
|
|
if (!$stmt instanceof Return_) {
|
|
continue;
|
|
}
|
|
if (!$stmt->expr instanceof Array_) {
|
|
continue;
|
|
}
|
|
return \true;
|
|
}
|
|
return \false;
|
|
}
|
|
}
|