2021-05-02 10:46:55 +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-05-02 10:46:55 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\Yield_;
|
2023-02-13 14:43:04 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2022-06-06 17:12:56 +00:00
|
|
|
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;
|
2023-01-29 13:52:29 +00:00
|
|
|
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
|
2023-05-14 22:40:25 +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-05-02 10:46:55 +00:00
|
|
|
/**
|
|
|
|
* @changelog https://wiki.php.net/rfc/noreturn_type
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\ReturnNeverTypeRectorTest
|
|
|
|
*/
|
2023-05-14 22:40:25 +00:00
|
|
|
final class ReturnNeverTypeRector extends AbstractRector implements MinPhpVersionInterface
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2023-01-29 13:52:29 +00:00
|
|
|
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2023-01-29 13:52:29 +00:00
|
|
|
private $classMethodReturnTypeOverrideGuard;
|
2023-05-14 22:40:25 +00:00
|
|
|
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard)
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2023-01-29 13:52:29 +00:00
|
|
|
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Add "never" return-type for methods that never return anything', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-05-02 10:46:55 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
throw new InvalidException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-05-02 10:46:55 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
2023-05-14 22:40:25 +00:00
|
|
|
public function run(): never
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
|
|
|
throw new InvalidException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class, Function_::class, Closure::class];
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod|Function_|Closure $node
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2021-06-29 12:35:49 +00:00
|
|
|
if ($this->shouldSkip($node)) {
|
2021-05-02 10:46:55 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-14 22:40:25 +00:00
|
|
|
$node->returnType = new Identifier('never');
|
2021-06-29 12:35:49 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2023-05-14 22:40:25 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
|
|
|
return PhpVersionFeature::NEVER_TYPE;
|
|
|
|
}
|
2021-06-29 12:35:49 +00:00
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
|
2021-06-29 12:35:49 +00:00
|
|
|
*/
|
|
|
|
private function shouldSkip($node) : bool
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$hasReturn = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, Return_::class);
|
|
|
|
if ($node instanceof ClassMethod && $node->isMagic()) {
|
2022-04-26 13:03:56 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2021-12-06 19:50:11 +00:00
|
|
|
if ($hasReturn) {
|
2021-06-29 12:35:49 +00:00
|
|
|
return \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2023-04-08 16:24:17 +00:00
|
|
|
$item1Unpacked = ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES;
|
|
|
|
$hasNotNeverNodes = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, \array_merge([Yield_::class], $item1Unpacked));
|
2021-11-10 10:27:52 +00:00
|
|
|
if ($hasNotNeverNodes) {
|
2021-06-29 12:35:49 +00:00
|
|
|
return \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$hasNeverNodes = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, [Throw_::class]);
|
2021-07-25 16:57:05 +00:00
|
|
|
$hasNeverFuncCall = $this->hasNeverFuncCall($node);
|
2021-11-10 10:27:52 +00:00
|
|
|
if (!$hasNeverNodes && !$hasNeverFuncCall) {
|
2021-06-29 12:35:49 +00:00
|
|
|
return \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2023-01-29 13:52:29 +00:00
|
|
|
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node)) {
|
2021-06-29 12:35:49 +00:00
|
|
|
return \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->returnType instanceof Node) {
|
2021-11-28 17:01:20 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return $this->isName($node->returnType, 'never');
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-04-08 16:24:17 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2021-07-25 16:57:05 +00:00
|
|
|
private function hasNeverFuncCall($functionLike) : bool
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasNeverType = \false;
|
2021-05-02 10:46:55 +00:00
|
|
|
foreach ((array) $functionLike->stmts as $stmt) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($stmt instanceof Expression) {
|
2021-05-02 10:46:55 +00:00
|
|
|
$stmt = $stmt->expr;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($stmt instanceof Stmt) {
|
2021-05-02 10:46:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-10-07 17:46:41 +00:00
|
|
|
$stmtType = $this->getType($stmt);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($stmtType instanceof NeverType) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasNeverType = \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $hasNeverType;
|
|
|
|
}
|
|
|
|
}
|