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_ ;
use PhpParser\Node\Name ;
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\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger ;
use Rector\Core\Php\PhpVersionProvider ;
use Rector\Core\Rector\AbstractRector ;
use Rector\Core\ValueObject\PhpVersionFeature ;
use Rector\NodeNestingScope\ValueObject\ControlStructure ;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard ;
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
*/
2022-06-06 17:12:56 +00:00
final class ReturnNeverTypeRector extends \Rector\Core\Rector\AbstractRector
2021-05-02 10:46:55 +00:00
{
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-10-26 00:34:35 +00:00
* @ var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
2021-05-02 10:46:55 +00:00
*/
private $parentClassMethodTypeOverrideGuard ;
2021-06-29 12:35:49 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-06-29 12:35:49 +00:00
* @ var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger ;
2022-04-28 20:11:07 +00:00
/**
* @ readonly
* @ var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider ;
2022-06-06 17:12:56 +00:00
public function __construct ( \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard , \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger , \Rector\Core\Php\PhpVersionProvider $phpVersionProvider )
2021-05-02 10:46:55 +00:00
{
$this -> parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard ;
2021-06-29 12:35:49 +00:00
$this -> phpDocTypeChanger = $phpDocTypeChanger ;
2022-04-28 20:11:07 +00:00
$this -> phpVersionProvider = $phpVersionProvider ;
2021-05-02 10:46:55 +00:00
}
2022-06-06 17:12:56 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2021-05-02 10:46:55 +00:00
{
2022-06-06 17:12:56 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Add "never" return-type for methods that never return anything' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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
{
2021-06-29 12:35:49 +00:00
/**
* @ return never
*/
public function run ()
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-06 17:12:56 +00:00
return [ \PhpParser\Node\Stmt\ClassMethod :: class , \PhpParser\Node\Stmt\Function_ :: class , \PhpParser\Node\Expr\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-06 17:12:56 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\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 ;
}
2022-06-06 17:12:56 +00:00
if ( $this -> phpVersionProvider -> isAtLeastPhpVersion ( \Rector\Core\ValueObject\PhpVersionFeature :: NEVER_TYPE )) {
2021-06-29 12:35:49 +00:00
// never-type supported natively
2022-06-06 17:12:56 +00:00
$node -> returnType = new \PhpParser\Node\Name ( 'never' );
2021-06-29 12:35:49 +00:00
} else {
// static anlysis based never type
$phpDocInfo = $this -> phpDocInfoFactory -> createFromNodeOrEmpty ( $node );
2022-06-06 17:12:56 +00:00
$hasChanged = $this -> phpDocTypeChanger -> changeReturnType ( $phpDocInfo , new \PHPStan\Type\NeverType ());
2022-02-28 06:43:27 +00:00
if ( ! $hasChanged ) {
return null ;
}
2021-06-29 12:35:49 +00:00
}
return $node ;
}
/**
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-06 17:12:56 +00:00
$hasReturn = $this -> betterNodeFinder -> hasInstancesOfInFunctionLikeScoped ( $node , \PhpParser\Node\Stmt\Return_ :: class );
if ( $node instanceof \PhpParser\Node\Stmt\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
}
2022-06-06 17:12:56 +00:00
$yieldAndConditionalNodes = \array_merge ([ \PhpParser\Node\Expr\Yield_ :: class ], \Rector\NodeNestingScope\ValueObject\ControlStructure :: CONDITIONAL_NODE_SCOPE_TYPES );
2021-12-06 19:50:11 +00:00
$hasNotNeverNodes = $this -> betterNodeFinder -> hasInstancesOfInFunctionLikeScoped ( $node , $yieldAndConditionalNodes );
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-06 17:12:56 +00:00
$hasNeverNodes = $this -> betterNodeFinder -> hasInstancesOfInFunctionLikeScoped ( $node , [ \PhpParser\Node\Stmt\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
}
2022-06-06 17:12:56 +00:00
if ( $node instanceof \PhpParser\Node\Stmt\ClassMethod && ! $this -> parentClassMethodTypeOverrideGuard -> isReturnTypeChangeAllowed ( $node )) {
2021-06-29 12:35:49 +00:00
return \true ;
2021-05-02 10:46:55 +00:00
}
2022-06-06 17:12:56 +00:00
if ( ! $node -> returnType instanceof \PhpParser\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
}
/**
2022-04-26 08:13:18 +00:00
* @ param \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Expr\Closure $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-06 17:12:56 +00:00
if ( $stmt instanceof \PhpParser\Node\Stmt\Expression ) {
2021-05-02 10:46:55 +00:00
$stmt = $stmt -> expr ;
}
2022-06-06 17:12:56 +00:00
if ( $stmt instanceof \PhpParser\Node\Stmt ) {
2021-05-02 10:46:55 +00:00
continue ;
}
2021-10-07 17:46:41 +00:00
$stmtType = $this -> getType ( $stmt );
2022-06-06 17:12:56 +00:00
if ( $stmtType instanceof \PHPStan\Type\NeverType ) {
2021-05-09 20:15:43 +00:00
$hasNeverType = \true ;
2021-05-02 10:46:55 +00:00
}
}
return $hasNeverType ;
}
}