2021-05-02 10:46:55 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2021-05-02 10:46:55 +00:00
namespace Rector\TypeDeclaration\Rector\ClassMethod ;
use PhpParser\Node ;
2021-12-09 13:05:29 +00:00
use PhpParser\Node\Expr\Closure ;
2021-05-02 10:46:55 +00:00
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 ;
2021-06-29 12:35:49 +00:00
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger ;
2021-05-02 10:46:55 +00:00
use Rector\Core\Rector\AbstractRector ;
use Rector\Core\ValueObject\PhpVersionFeature ;
2021-11-10 10:27:52 +00:00
use Rector\NodeNestingScope\ValueObject\ControlStructure ;
2021-10-26 00:34:35 +00:00
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard ;
2021-05-02 10:46:55 +00:00
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
*/
2021-05-10 22:23:08 +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 ;
2021-10-26 00:34:35 +00:00
public function __construct ( \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard , \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger )
2021-05-02 10:46:55 +00:00
{
$this -> parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard ;
2021-06-29 12:35:49 +00:00
$this -> phpDocTypeChanger = $phpDocTypeChanger ;
2021-05-02 10:46:55 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2021-05-02 10:46:55 +00:00
{
2021-06-29 12:35:49 +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
{
2021-12-09 13:05:29 +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 09:57:54 +00:00
* @ param \PhpParser\Node $node
2021-05-02 10:46:55 +00:00
*/
2021-12-10 09:57:54 +00:00
public function refactor ( $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 ;
}
2021-06-29 12:35:49 +00:00
if ( $this -> phpVersionProvider -> isAtLeastPhpVersion ( \Rector\Core\ValueObject\PhpVersionFeature :: NEVER_TYPE )) {
// never-type supported natively
$node -> returnType = new \PhpParser\Node\Name ( 'never' );
} else {
// static anlysis based never type
$phpDocInfo = $this -> phpDocInfoFactory -> createFromNodeOrEmpty ( $node );
$this -> phpDocTypeChanger -> changeReturnType ( $phpDocInfo , new \PHPStan\Type\NeverType ());
}
return $node ;
}
/**
2021-12-09 13:05:29 +00:00
* @ param \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Function_ $node
2021-06-29 12:35:49 +00:00
*/
private function shouldSkip ( $node ) : bool
{
2021-12-06 19:50:11 +00:00
$hasReturn = $this -> betterNodeFinder -> hasInstancesOfInFunctionLikeScoped ( $node , \PhpParser\Node\Stmt\Return_ :: class );
if ( $hasReturn ) {
2021-06-29 12:35:49 +00:00
return \true ;
2021-05-02 10:46:55 +00:00
}
2021-11-12 09:48:23 +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
}
2021-12-06 19:50:11 +00:00
$hasNeverNodes = $this -> betterNodeFinder -> hasInstancesOfInFunctionLikeScoped ( $node , [ \PhpParser\Node\Expr\Throw_ :: class , \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
}
2021-05-10 22:23:08 +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
}
2021-11-28 17:01:20 +00:00
if ( ! $node -> returnType instanceof \PhpParser\Node ) {
return \false ;
}
return $this -> isName ( $node -> returnType , 'never' );
2021-05-02 10:46:55 +00:00
}
/**
2021-12-09 13:05:29 +00:00
* @ param \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\ClassMethod | \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 ) {
2021-05-10 22:23:08 +00:00
if ( $stmt instanceof \PhpParser\Node\Stmt\Expression ) {
2021-05-02 10:46:55 +00:00
$stmt = $stmt -> expr ;
}
2021-05-10 22:23:08 +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 );
2021-05-10 22:23:08 +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 ;
}
}