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\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use PhpParser\Node\Stmt\Throw_;
|
2023-06-09 17:30:44 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2023-09-20 12:55:35 +00:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2023-06-09 17:30:44 +00:00
|
|
|
use Rector\Core\Rector\AbstractScopeAwareRector;
|
2023-12-23 21:26:40 +00:00
|
|
|
use Rector\Core\Reflection\ClassModifierChecker;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\NodeNestingScope\ValueObject\ControlStructure;
|
2023-12-23 14:51:47 +00:00
|
|
|
use Rector\TypeDeclaration\NodeAnalyzer\NeverFuncCallAnalyzer;
|
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-06-09 17:30:44 +00:00
|
|
|
final class ReturnNeverTypeRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +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-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2023-12-23 14:51:47 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\TypeDeclaration\NodeAnalyzer\NeverFuncCallAnalyzer
|
|
|
|
*/
|
|
|
|
private $neverFuncCallAnalyzer;
|
2023-12-23 21:26:40 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\Reflection\ClassModifierChecker
|
|
|
|
*/
|
|
|
|
private $classModifierChecker;
|
|
|
|
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, BetterNodeFinder $betterNodeFinder, NeverFuncCallAnalyzer $neverFuncCallAnalyzer, ClassModifierChecker $classModifierChecker)
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2023-01-29 13:52:29 +00:00
|
|
|
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2023-12-23 14:51:47 +00:00
|
|
|
$this->neverFuncCallAnalyzer = $neverFuncCallAnalyzer;
|
2023-12-23 21:26:40 +00:00
|
|
|
$this->classModifierChecker = $classModifierChecker;
|
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
|
|
|
*/
|
2023-06-09 17:30:44 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2023-06-09 17:30:44 +00:00
|
|
|
if ($this->shouldSkip($node, $scope)) {
|
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
|
|
|
*/
|
2023-06-09 17:30:44 +00:00
|
|
|
private function shouldSkip($node, Scope $scope) : bool
|
2021-06-29 12:35:49 +00:00
|
|
|
{
|
2023-08-08 15:44:06 +00:00
|
|
|
if ($node->returnType instanceof Node && !$this->isName($node->returnType, 'void')) {
|
|
|
|
return \true;
|
|
|
|
}
|
2023-12-23 14:51:47 +00:00
|
|
|
if ($this->hasReturnOrYields($node)) {
|
2021-06-29 12:35:49 +00:00
|
|
|
return \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2023-12-23 14:51:47 +00:00
|
|
|
if (!$this->hasNeverNodesOrNeverFuncCalls($node)) {
|
2021-06-29 12:35:49 +00:00
|
|
|
return \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2023-06-09 17:30:44 +00:00
|
|
|
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
|
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;
|
|
|
|
}
|
2023-12-23 14:51:47 +00:00
|
|
|
// skip as most likely intentional
|
2023-12-23 21:26:40 +00:00
|
|
|
if (!$this->classModifierChecker->isInsideFinalClass($node) && $this->isName($node->returnType, 'void')) {
|
2023-12-23 14:51:47 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2021-11-28 17:01:20 +00:00
|
|
|
return $this->isName($node->returnType, 'never');
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-12-23 14:51:47 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
|
|
|
|
*/
|
|
|
|
private function hasReturnOrYields($node) : bool
|
|
|
|
{
|
|
|
|
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, Return_::class)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, \array_merge([Yield_::class], ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2023-12-23 14:51:47 +00:00
|
|
|
private function hasNeverNodesOrNeverFuncCalls($node) : bool
|
2021-05-02 10:46:55 +00:00
|
|
|
{
|
2023-12-23 14:51:47 +00:00
|
|
|
$hasNeverNodes = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, [Throw_::class]);
|
|
|
|
if ($hasNeverNodes) {
|
|
|
|
return \true;
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
2023-12-23 14:51:47 +00:00
|
|
|
return $this->neverFuncCallAnalyzer->hasNeverFuncCall($node);
|
2021-05-02 10:46:55 +00:00
|
|
|
}
|
|
|
|
}
|