mirror of https://github.com/rectorphp/rector.git
135 lines
4.4 KiB
PHP
135 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Closure;
|
|
use PhpParser\Node\Expr\Yield_;
|
|
use PhpParser\Node\Identifier;
|
|
use PhpParser\Node\Stmt;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PhpParser\Node\Stmt\Expression;
|
|
use PhpParser\Node\Stmt\Function_;
|
|
use PhpParser\Node\Stmt\Return_;
|
|
use PhpParser\Node\Stmt\Throw_;
|
|
use PHPStan\Type\NeverType;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\NodeNestingScope\ValueObject\ControlStructure;
|
|
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @changelog https://wiki.php.net/rfc/noreturn_type
|
|
*
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\ReturnNeverTypeRectorTest
|
|
*/
|
|
final class ReturnNeverTypeRector extends AbstractRector implements MinPhpVersionInterface
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
|
|
*/
|
|
private $classMethodReturnTypeOverrideGuard;
|
|
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard)
|
|
{
|
|
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
|
|
}
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Add "never" return-type for methods that never return anything', [new CodeSample(<<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
throw new InvalidException();
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
public function run(): never
|
|
{
|
|
throw new InvalidException();
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [ClassMethod::class, Function_::class, Closure::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod|Function_|Closure $node
|
|
*/
|
|
public function refactor(Node $node) : ?Node
|
|
{
|
|
if ($this->shouldSkip($node)) {
|
|
return null;
|
|
}
|
|
$node->returnType = new Identifier('never');
|
|
return $node;
|
|
}
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return PhpVersionFeature::NEVER_TYPE;
|
|
}
|
|
/**
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
|
|
*/
|
|
private function shouldSkip($node) : bool
|
|
{
|
|
$hasReturn = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, Return_::class);
|
|
if ($node instanceof ClassMethod && $node->isMagic()) {
|
|
return \true;
|
|
}
|
|
if ($hasReturn) {
|
|
return \true;
|
|
}
|
|
$item1Unpacked = ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES;
|
|
$hasNotNeverNodes = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, \array_merge([Yield_::class], $item1Unpacked));
|
|
if ($hasNotNeverNodes) {
|
|
return \true;
|
|
}
|
|
$hasNeverNodes = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, [Throw_::class]);
|
|
$hasNeverFuncCall = $this->hasNeverFuncCall($node);
|
|
if (!$hasNeverNodes && !$hasNeverFuncCall) {
|
|
return \true;
|
|
}
|
|
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node)) {
|
|
return \true;
|
|
}
|
|
if (!$node->returnType instanceof Node) {
|
|
return \false;
|
|
}
|
|
return $this->isName($node->returnType, 'never');
|
|
}
|
|
/**
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
|
|
*/
|
|
private function hasNeverFuncCall($functionLike) : bool
|
|
{
|
|
$hasNeverType = \false;
|
|
foreach ((array) $functionLike->stmts as $stmt) {
|
|
if ($stmt instanceof Expression) {
|
|
$stmt = $stmt->expr;
|
|
}
|
|
if ($stmt instanceof Stmt) {
|
|
continue;
|
|
}
|
|
$stmtType = $this->getType($stmt);
|
|
if ($stmtType instanceof NeverType) {
|
|
$hasNeverType = \true;
|
|
}
|
|
}
|
|
return $hasNeverType;
|
|
}
|
|
}
|