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 ;
use PhpParser\Node\Expr\Yield_ ;
use PhpParser\Node\FunctionLike ;
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\Core\Rector\AbstractRector ;
use Rector\Core\ValueObject\PhpVersionFeature ;
use Rector\Defluent\ConflictGuard\ParentClassMethodTypeOverrideGuard ;
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-05-10 23:39:21 +00:00
* @ var \Rector\Defluent\ConflictGuard\ParentClassMethodTypeOverrideGuard
2021-05-02 10:46:55 +00:00
*/
private $parentClassMethodTypeOverrideGuard ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\Defluent\ConflictGuard\ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard )
2021-05-02 10:46:55 +00:00
{
$this -> parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2021-05-02 10:46:55 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Add "never" 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
{
public function run () : never
{
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-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\ClassMethod :: class , \PhpParser\Node\Stmt\Function_ :: class ];
2021-05-02 10:46:55 +00:00
}
/**
* @ param ClassMethod | Function_ $node
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2021-05-02 10:46:55 +00:00
{
2021-05-10 22:23:08 +00:00
if ( ! $this -> phpVersionProvider -> isAtLeastPhpVersion ( \Rector\Core\ValueObject\PhpVersionFeature :: NEVER_TYPE )) {
2021-05-02 10:46:55 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
$returns = $this -> betterNodeFinder -> findInstanceOf ( $node , \PhpParser\Node\Stmt\Return_ :: class );
2021-05-02 10:46:55 +00:00
if ( $returns !== []) {
return null ;
}
2021-05-10 22:23:08 +00:00
$notNeverNodes = $this -> betterNodeFinder -> findInstancesOf ( $node , [ \PhpParser\Node\Expr\Yield_ :: class ]);
2021-05-02 10:46:55 +00:00
if ( $notNeverNodes !== []) {
return null ;
}
2021-05-10 22:23:08 +00:00
$neverNodes = $this -> betterNodeFinder -> findInstancesOf ( $node , [ \PhpParser\Node\Expr\Throw_ :: class , \PhpParser\Node\Stmt\Throw_ :: class ]);
2021-05-02 10:46:55 +00:00
$hasNeverFuncCall = $this -> resolveHasNeverFuncCall ( $node );
2021-05-09 20:15:43 +00:00
if ( $neverNodes === [] && ! $hasNeverFuncCall ) {
2021-05-02 10:46:55 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
if ( $node instanceof \PhpParser\Node\Stmt\ClassMethod && ! $this -> parentClassMethodTypeOverrideGuard -> isReturnTypeChangeAllowed ( $node )) {
2021-05-02 10:46:55 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
$node -> returnType = new \PhpParser\Node\Name ( 'never' );
2021-05-02 10:46:55 +00:00
return $node ;
}
/**
* @ param ClassMethod | Function_ $functionLike
*/
2021-05-10 22:23:08 +00:00
private function resolveHasNeverFuncCall ( \PhpParser\Node\FunctionLike $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 ;
}
$stmtType = $this -> getStaticType ( $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 ;
}
}