2021-02-15 16:38:30 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2021-04-28 20:33:20 +00:00
namespace Rector\TypeDeclaration\Rector\Param ;
2021-02-15 16:38:30 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\Assign ;
use PhpParser\Node\Expr\PropertyFetch ;
2021-02-15 19:31:53 +00:00
use PhpParser\Node\FunctionLike ;
2021-02-15 16:38:30 +00:00
use PhpParser\Node\NullableType ;
use PhpParser\Node\Param ;
2021-04-28 21:12:26 +00:00
use PHPStan\Analyser\Scope ;
2021-06-28 10:46:06 +00:00
use PHPStan\Reflection\Php\PhpPropertyReflection ;
2021-05-03 18:03:01 +00:00
use PHPStan\Type\MixedType ;
2021-02-15 16:38:30 +00:00
use PHPStan\Type\Type ;
2021-11-14 12:07:09 +00:00
use PHPStan\Type\UnionType ;
2021-02-15 16:38:30 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-06-28 10:46:06 +00:00
use Rector\Core\Reflection\ReflectionResolver ;
2021-02-15 16:38:30 +00:00
use Rector\Core\ValueObject\PhpVersionFeature ;
2021-04-28 20:33:20 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2021-10-27 19:25:02 +00:00
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind ;
2021-07-21 09:35:57 +00:00
use Rector\VersionBonding\Contract\MinPhpVersionInterface ;
2021-02-15 16:38:30 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
/**
2021-04-28 20:33:20 +00:00
* @ see \Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\ParamTypeFromStrictTypedPropertyRectorTest
2021-02-15 16:38:30 +00:00
*/
2021-07-21 09:35:57 +00:00
final class ParamTypeFromStrictTypedPropertyRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
2021-02-15 16:38:30 +00:00
{
2021-02-15 19:31:53 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-06-28 10:46:06 +00:00
* @ var \Rector\Core\Reflection\ReflectionResolver
2021-02-15 19:31:53 +00:00
*/
2021-06-28 10:46:06 +00:00
private $reflectionResolver ;
public function __construct ( \Rector\Core\Reflection\ReflectionResolver $reflectionResolver )
2021-04-28 20:33:20 +00:00
{
2021-06-28 10:46:06 +00:00
$this -> reflectionResolver = $reflectionResolver ;
2021-02-15 16:38:30 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2021-02-15 16:38:30 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Add param type from $param set to typed property' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2021-02-15 16:38:30 +00:00
final class SomeClass
{
private int $age ;
public function setAge ( $age )
{
$this -> age = $age ;
}
}
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2021-02-15 16:38:30 +00:00
final class SomeClass
{
private int $age ;
public function setAge ( int $age )
{
$this -> age = $age ;
}
}
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2021-02-15 16:38:30 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2021-02-15 16:38:30 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2021-02-15 16:38:30 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Param :: class ];
2021-02-15 16:38:30 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param Param $node
2021-02-15 16:38:30 +00:00
*/
2021-12-10 10:22:23 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2021-02-15 16:38:30 +00:00
{
2021-05-10 22:23:08 +00:00
$parent = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( ! $parent instanceof \PhpParser\Node\FunctionLike ) {
2021-04-28 20:33:20 +00:00
return null ;
2021-02-15 16:38:30 +00:00
}
2021-04-28 20:33:20 +00:00
return $this -> decorateParamWithType ( $parent , $node );
2021-02-15 16:38:30 +00:00
}
2021-12-10 10:22:23 +00:00
public function decorateParamWithType ( \PhpParser\Node\FunctionLike $functionLike , \PhpParser\Node\Param $param ) : ? \PhpParser\Node\Param
2021-02-15 16:38:30 +00:00
{
if ( $param -> type !== null ) {
2021-04-28 20:33:20 +00:00
return null ;
2021-02-15 16:38:30 +00:00
}
2021-05-03 18:03:01 +00:00
$originalParamType = $this -> resolveParamOriginalType ( $param );
2021-04-28 21:12:26 +00:00
$paramName = $this -> getName ( $param );
2021-04-28 20:33:20 +00:00
/** @var Assign[] $assigns */
2021-05-10 22:23:08 +00:00
$assigns = $this -> betterNodeFinder -> findInstanceOf (( array ) $functionLike -> getStmts (), \PhpParser\Node\Expr\Assign :: class );
2021-04-28 20:33:20 +00:00
foreach ( $assigns as $assign ) {
2021-05-09 20:15:43 +00:00
if ( ! $this -> nodeComparator -> areNodesEqual ( $assign -> expr , $param -> var )) {
2021-04-28 20:33:20 +00:00
continue ;
2021-02-15 16:38:30 +00:00
}
2021-05-10 22:23:08 +00:00
if ( ! $assign -> var instanceof \PhpParser\Node\Expr\PropertyFetch ) {
2021-04-28 20:33:20 +00:00
continue ;
2021-02-15 16:38:30 +00:00
}
2021-04-28 21:12:26 +00:00
if ( $this -> hasTypeChangedBeforeAssign ( $assign , $paramName , $originalParamType )) {
return null ;
}
2021-04-28 20:33:20 +00:00
$singlePropertyTypeNode = $this -> matchPropertySingleTypeNode ( $assign -> var );
2021-05-10 22:23:08 +00:00
if ( ! $singlePropertyTypeNode instanceof \PhpParser\Node ) {
2021-02-15 16:38:30 +00:00
return null ;
}
2021-02-15 19:31:53 +00:00
$param -> type = $singlePropertyTypeNode ;
2021-04-28 20:33:20 +00:00
return $param ;
}
return null ;
2021-02-15 16:38:30 +00:00
}
2021-07-21 09:35:57 +00:00
public function provideMinPhpVersion () : int
{
return \Rector\Core\ValueObject\PhpVersionFeature :: TYPED_PROPERTIES ;
}
2021-04-25 23:12:14 +00:00
/**
2021-11-14 12:07:09 +00:00
* @ return Node\Name | Node\ComplexType | null
2021-04-25 23:12:14 +00:00
*/
2021-05-10 22:23:08 +00:00
private function matchPropertySingleTypeNode ( \PhpParser\Node\Expr\PropertyFetch $propertyFetch ) : ? \PhpParser\Node
2021-02-15 16:38:30 +00:00
{
2021-06-28 10:46:06 +00:00
$phpPropertyReflection = $this -> reflectionResolver -> resolvePropertyReflectionFromPropertyFetch ( $propertyFetch );
if ( ! $phpPropertyReflection instanceof \PHPStan\Reflection\Php\PhpPropertyReflection ) {
return null ;
2021-02-15 16:38:30 +00:00
}
2021-06-28 10:46:06 +00:00
$propertyType = $phpPropertyReflection -> getNativeType ();
if ( $propertyType instanceof \PHPStan\Type\MixedType ) {
2021-02-15 16:38:30 +00:00
return null ;
}
2021-06-28 10:46:06 +00:00
if ( $propertyType instanceof \PHPStan\Type\UnionType ) {
2021-02-15 16:38:30 +00:00
return null ;
}
2021-06-28 10:46:06 +00:00
if ( $propertyType instanceof \PhpParser\Node\NullableType ) {
2021-02-15 16:38:30 +00:00
return null ;
}
2021-10-27 19:25:02 +00:00
return $this -> staticTypeMapper -> mapPHPStanTypeToPhpParserNode ( $propertyType , \Rector\PHPStanStaticTypeMapper\Enum\TypeKind :: PROPERTY ());
2021-02-15 16:38:30 +00:00
}
2021-05-10 22:23:08 +00:00
private function hasTypeChangedBeforeAssign ( \PhpParser\Node\Expr\Assign $assign , string $paramName , \PHPStan\Type\Type $originalType ) : bool
2021-04-28 21:12:26 +00:00
{
2021-05-10 22:23:08 +00:00
$scope = $assign -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: SCOPE );
if ( ! $scope instanceof \PHPStan\Analyser\Scope ) {
2021-05-09 20:15:43 +00:00
return \false ;
2021-04-28 21:12:26 +00:00
}
2021-05-09 20:15:43 +00:00
if ( ! $scope -> hasVariableType ( $paramName ) -> yes ()) {
return \false ;
2021-05-03 18:03:01 +00:00
}
2021-04-28 21:12:26 +00:00
$currentParamType = $scope -> getVariableType ( $paramName );
2021-05-09 20:15:43 +00:00
return ! $currentParamType -> equals ( $originalType );
2021-04-28 21:12:26 +00:00
}
2021-05-10 22:23:08 +00:00
private function resolveParamOriginalType ( \PhpParser\Node\Param $param ) : \PHPStan\Type\Type
2021-05-03 18:03:01 +00:00
{
2021-05-10 22:23:08 +00:00
$scope = $param -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: SCOPE );
if ( ! $scope instanceof \PHPStan\Analyser\Scope ) {
return new \PHPStan\Type\MixedType ();
2021-05-03 18:03:01 +00:00
}
$paramName = $this -> getName ( $param );
2021-05-09 20:15:43 +00:00
if ( ! $scope -> hasVariableType ( $paramName ) -> yes ()) {
2021-05-10 22:23:08 +00:00
return new \PHPStan\Type\MixedType ();
2021-05-03 18:03:01 +00:00
}
return $scope -> getVariableType ( $paramName );
}
2021-02-15 16:38:30 +00:00
}