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 ;
use PhpParser\Node\Stmt\Property ;
use PhpParser\Node\UnionType ;
2021-04-28 21:12:26 +00:00
use PHPStan\Analyser\Scope ;
2021-05-03 18:03:01 +00:00
use PHPStan\Type\MixedType ;
2021-02-15 16:38:30 +00:00
use PHPStan\Type\Type ;
use Rector\Core\Rector\AbstractRector ;
use Rector\Core\ValueObject\PhpVersionFeature ;
2021-04-28 20:33:20 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2021-02-15 19:31:53 +00:00
use Rector\TypeDeclaration\Reflection\ReflectionTypeResolver ;
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-05-10 22:23:08 +00:00
final class ParamTypeFromStrictTypedPropertyRector extends \Rector\Core\Rector\AbstractRector
2021-02-15 16:38:30 +00:00
{
2021-02-15 19:31:53 +00:00
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\TypeDeclaration\Reflection\ReflectionTypeResolver
2021-02-15 19:31:53 +00:00
*/
private $reflectionTypeResolver ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\TypeDeclaration\Reflection\ReflectionTypeResolver $reflectionTypeResolver )
2021-04-28 20:33:20 +00:00
{
2021-02-15 19:31:53 +00:00
$this -> reflectionTypeResolver = $reflectionTypeResolver ;
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-04-28 20:33:20 +00:00
* @ param Param $node
2021-02-15 16:38:30 +00:00
*/
2021-05-10 22:23:08 +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
if ( ! $this -> isAtLeastPhpVersion ( \Rector\Core\ValueObject\PhpVersionFeature :: TYPED_PROPERTIES )) {
2021-02-15 16:38:30 +00:00
return null ;
}
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-05-10 22:23:08 +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-04-25 23:12:14 +00:00
/**
* @ return Node\Identifier | Node\Name | UnionType | NullableType | null
*/
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
{
$property = $this -> nodeRepository -> findPropertyByPropertyFetch ( $propertyFetch );
2021-05-10 22:23:08 +00:00
if ( ! $property instanceof \PhpParser\Node\Stmt\Property ) {
2021-02-15 19:31:53 +00:00
// code from /vendor
$propertyFetchType = $this -> reflectionTypeResolver -> resolvePropertyFetchType ( $propertyFetch );
2021-05-10 22:23:08 +00:00
if ( ! $propertyFetchType instanceof \PHPStan\Type\Type ) {
2021-02-15 19:31:53 +00:00
return null ;
}
return $this -> staticTypeMapper -> mapPHPStanTypeToPhpParserNode ( $propertyFetchType );
2021-02-15 16:38:30 +00:00
}
if ( $property -> type === null ) {
return null ;
}
// move type to param if not union type
2021-05-10 22:23:08 +00:00
if ( $property -> type instanceof \PhpParser\Node\UnionType ) {
2021-02-15 16:38:30 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
if ( $property -> type instanceof \PhpParser\Node\NullableType ) {
2021-02-15 16:38:30 +00:00
return null ;
}
2021-04-28 20:33:20 +00:00
// needed to avoid reprinting original tokens bug
$typeNode = clone $property -> type ;
2021-05-10 22:23:08 +00:00
$typeNode -> setAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: ORIGINAL_NODE , null );
2021-04-28 20:33:20 +00:00
return $typeNode ;
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
}