2020-06-08 09:28:03 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-06-08 09:28:03 +00:00
namespace Rector\Php80\Rector\Class_ ;
use PhpParser\Node ;
2021-05-17 18:55:10 +00:00
use PhpParser\Node\Identifier ;
2020-06-08 09:28:03 +00:00
use PhpParser\Node\Param ;
use PhpParser\Node\Stmt\Class_ ;
use PhpParser\Node\Stmt\ClassMethod ;
use PhpParser\Node\Stmt\Property ;
2021-01-20 11:41:35 +00:00
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode ;
2021-05-06 10:59:29 +00:00
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey ;
2020-06-08 09:28:03 +00:00
use Rector\Core\Rector\AbstractRector ;
2020-08-02 13:17:33 +00:00
use Rector\Core\ValueObject\MethodName ;
2021-06-07 14:38:40 +00:00
use Rector\Core\ValueObject\PhpVersionFeature ;
2021-03-21 12:57:56 +00:00
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover ;
2020-12-09 23:40:38 +00:00
use Rector\Naming\VariableRenamer ;
2020-12-02 14:26:14 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2021-06-10 10:46:24 +00:00
use Rector\Php80\NodeAnalyzer\PromotedPropertyCandidateResolver ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2020-06-08 09:28:03 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// wiki . php . net / rfc / constructor_promotion https :// github . com / php / php - src / pull / 5291
2020-06-08 09:28:03 +00:00
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\ClassPropertyAssignToConstructorPromotionRectorTest
2020-06-08 09:28:03 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ClassPropertyAssignToConstructorPromotionRector extends \Rector\Core\Rector\AbstractRector
2020-06-08 09:28:03 +00:00
{
/**
2021-06-10 10:46:24 +00:00
* @ var \Rector\Php80\NodeAnalyzer\PromotedPropertyCandidateResolver
2020-06-08 09:28:03 +00:00
*/
2020-12-09 23:40:38 +00:00
private $promotedPropertyResolver ;
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\Naming\VariableRenamer
2020-12-09 23:40:38 +00:00
*/
private $variableRenamer ;
2021-01-16 16:45:12 +00:00
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover
2021-01-16 16:45:12 +00:00
*/
private $varTagRemover ;
2021-06-10 10:46:24 +00:00
public function __construct ( \Rector\Php80\NodeAnalyzer\PromotedPropertyCandidateResolver $promotedPropertyResolver , \Rector\Naming\VariableRenamer $variableRenamer , \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover $varTagRemover )
2021-05-09 20:15:43 +00:00
{
2020-12-09 23:40:38 +00:00
$this -> promotedPropertyResolver = $promotedPropertyResolver ;
$this -> variableRenamer = $variableRenamer ;
2021-01-16 16:45:12 +00:00
$this -> varTagRemover = $varTagRemover ;
2020-12-09 23:40:38 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-06-08 09:28:03 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change simple property init and assign to constructor promotion' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2020-06-08 09:28:03 +00:00
class SomeClass
{
2020-12-09 23:40:38 +00:00
public float $someVariable ;
public function __construct ( float $someVariable = 0.0 )
{
$this -> someVariable = $someVariable ;
2020-06-08 09:28:03 +00:00
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2020-06-08 09:28:03 +00:00
class SomeClass
{
2020-12-09 23:40:38 +00:00
public function __construct ( private float $someVariable = 0.0 )
{
}
2020-06-08 09:28:03 +00:00
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2020-06-08 09:28:03 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-06-08 09:28:03 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-06-08 09:28:03 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\Class_ :: class ];
2020-06-08 09:28:03 +00:00
}
/**
* @ param Class_ $node
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2020-06-08 09:28:03 +00:00
{
2021-06-07 14:38:40 +00:00
if ( ! $this -> isAtLeastPhpVersion ( \Rector\Core\ValueObject\PhpVersionFeature :: PROPERTY_PROMOTION )) {
return null ;
}
2020-12-09 23:40:38 +00:00
$promotionCandidates = $this -> promotedPropertyResolver -> resolveFromClass ( $node );
2020-06-08 09:28:03 +00:00
if ( $promotionCandidates === []) {
return null ;
}
2020-12-09 23:40:38 +00:00
/** @var ClassMethod $constructClassMethod */
2021-05-10 22:23:08 +00:00
$constructClassMethod = $node -> getMethod ( \Rector\Core\ValueObject\MethodName :: CONSTRUCT );
2021-05-06 10:59:29 +00:00
$classMethodPhpDocInfo = $this -> phpDocInfoFactory -> createFromNodeOrEmpty ( $constructClassMethod );
2020-12-09 23:40:38 +00:00
foreach ( $promotionCandidates as $promotionCandidate ) {
2020-12-02 14:26:14 +00:00
// does property have some useful annotations?
$property = $promotionCandidate -> getProperty ();
2020-12-30 19:31:55 +00:00
$param = $promotionCandidate -> getParam ();
if ( $param -> variadic ) {
continue ;
}
2021-05-17 18:55:10 +00:00
if ( $param -> type instanceof \PhpParser\Node\Identifier && $this -> isName ( $param -> type , 'callable' )) {
continue ;
}
2020-12-02 14:26:14 +00:00
$this -> removeNode ( $property );
2020-06-08 09:28:03 +00:00
$this -> removeNode ( $promotionCandidate -> getAssign ());
$property = $promotionCandidate -> getProperty ();
2021-05-06 10:59:29 +00:00
$paramName = $this -> getName ( $param );
// rename also following calls
$propertyName = $this -> getName ( $property -> props [ 0 ]);
2020-12-09 23:40:38 +00:00
/** @var string $oldName */
$oldName = $this -> getName ( $param -> var );
2021-05-29 22:10:59 +00:00
$this -> variableRenamer -> renameVariableInFunctionLike ( $constructClassMethod , $oldName , $propertyName , null );
2021-05-06 10:59:29 +00:00
$paramTagValueNode = $classMethodPhpDocInfo -> getParamTagValueNodeByName ( $paramName );
2021-05-10 22:23:08 +00:00
if ( ! $paramTagValueNode instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode ) {
2021-05-06 10:59:29 +00:00
$this -> decorateParamWithPropertyPhpDocInfo ( $property , $param );
} elseif ( $paramTagValueNode -> parameterName !== '$' . $propertyName ) {
$paramTagValueNode -> parameterName = '$' . $propertyName ;
2021-05-10 22:23:08 +00:00
$paramTagValueNode -> setAttribute ( \Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey :: ORIG_NODE , null );
2021-05-06 10:59:29 +00:00
}
2020-06-08 09:28:03 +00:00
// property name has higher priority
2020-12-26 23:41:30 +00:00
$propertyName = $this -> getName ( $property );
$param -> var -> name = $propertyName ;
2020-06-08 09:28:03 +00:00
$param -> flags = $property -> flags ;
2021-02-22 09:59:51 +00:00
$this -> processNullableType ( $property , $param );
2020-08-05 20:45:36 +00:00
}
2020-12-09 23:40:38 +00:00
return $node ;
2020-06-08 09:28:03 +00:00
}
2021-05-10 22:23:08 +00:00
private function processNullableType ( \PhpParser\Node\Stmt\Property $property , \PhpParser\Node\Param $param ) : void
2021-02-22 09:59:51 +00:00
{
if ( $this -> nodeTypeResolver -> isNullableType ( $property )) {
$objectType = $this -> getObjectType ( $property );
$param -> type = $this -> staticTypeMapper -> mapPHPStanTypeToPhpParserNode ( $objectType );
}
}
2021-05-10 22:23:08 +00:00
private function decorateParamWithPropertyPhpDocInfo ( \PhpParser\Node\Stmt\Property $property , \PhpParser\Node\Param $param ) : void
2020-12-02 14:26:14 +00:00
{
2021-01-18 21:45:13 +00:00
$propertyPhpDocInfo = $this -> phpDocInfoFactory -> createFromNodeOrEmpty ( $property );
2021-01-20 16:17:59 +00:00
$propertyPhpDocInfo -> markAsChanged ();
2021-05-10 22:23:08 +00:00
$param -> setAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PHP_DOC_INFO , $propertyPhpDocInfo );
2021-01-16 16:45:12 +00:00
// make sure the docblock is useful
if ( $param -> type === null ) {
return ;
}
$paramType = $this -> staticTypeMapper -> mapPhpParserNodePHPStanType ( $param -> type );
$this -> varTagRemover -> removeVarPhpTagValueNodeIfNotComment ( $param , $paramType );
2020-12-02 14:26:14 +00:00
}
2020-06-08 09:28:03 +00:00
}