2020-06-08 09:28:03 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
|
namespace Rector\Php80\Rector\Class_;
|
2020-06-08 09:28:03 +00:00
|
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use PhpParser\Node;
|
2023-05-14 15:29:04 +00:00
|
|
|
|
use PhpParser\Node\Expr;
|
2023-05-19 13:30:27 +00:00
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
|
use PhpParser\Node\NullableType;
|
|
|
|
|
use PhpParser\Node\Param;
|
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
|
use PhpParser\Node\UnionType;
|
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
|
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
|
2022-11-29 15:48:49 +00:00
|
|
|
|
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
|
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
|
use Rector\Core\ValueObject\MethodName;
|
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
|
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
|
|
|
|
|
use Rector\Naming\VariableRenamer;
|
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2022-11-29 15:48:49 +00:00
|
|
|
|
use Rector\Php80\Guard\MakePropertyPromotionGuard;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use Rector\Php80\NodeAnalyzer\PromotedPropertyCandidateResolver;
|
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-11-29 15:48:49 +00:00
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
2022-06-07 09:18:30 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2022-11-29 15:48:49 +00:00
|
|
|
|
final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRector implements MinPhpVersionInterface, AllowEmptyConfigurableRectorInterface
|
2020-06-08 09:28:03 +00:00
|
|
|
|
{
|
2022-11-29 15:48:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* @api
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public const INLINE_PUBLIC = 'inline_public';
|
|
|
|
|
/**
|
|
|
|
|
* Default to false, which only apply changes:
|
|
|
|
|
*
|
|
|
|
|
* – private modifier property
|
|
|
|
|
* - protected/public modifier property when property typed
|
|
|
|
|
*
|
|
|
|
|
* Set to true will allow change whether property is typed or not as far as not forbidden, eg: callable type, null type, etc.
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $inlinePublic = \false;
|
2020-06-08 09:28:03 +00:00
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
|
* @readonly
|
2021-06-10 10:46:24 +00:00
|
|
|
|
* @var \Rector\Php80\NodeAnalyzer\PromotedPropertyCandidateResolver
|
2020-06-08 09:28:03 +00:00
|
|
|
|
*/
|
2021-06-18 19:29:49 +00:00
|
|
|
|
private $promotedPropertyCandidateResolver;
|
2020-12-09 23:40:38 +00:00
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
|
* @readonly
|
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-12-04 12:47:17 +00:00
|
|
|
|
* @readonly
|
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-07-14 07:23:54 +00:00
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
|
* @readonly
|
2021-07-14 07:23:54 +00:00
|
|
|
|
* @var \Rector\Core\NodeAnalyzer\ParamAnalyzer
|
|
|
|
|
*/
|
|
|
|
|
private $paramAnalyzer;
|
2021-08-23 11:12:57 +00:00
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
|
* @readonly
|
2021-08-23 11:12:57 +00:00
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
|
|
|
|
|
*/
|
|
|
|
|
private $phpDocTypeChanger;
|
2021-10-29 14:22:26 +00:00
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
|
* @readonly
|
2022-11-29 15:48:49 +00:00
|
|
|
|
* @var \Rector\Php80\Guard\MakePropertyPromotionGuard
|
2021-10-29 14:22:26 +00:00
|
|
|
|
*/
|
2022-11-29 15:48:49 +00:00
|
|
|
|
private $makePropertyPromotionGuard;
|
2022-12-10 11:57:52 +00:00
|
|
|
|
public function __construct(PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver, VariableRenamer $variableRenamer, VarTagRemover $varTagRemover, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, MakePropertyPromotionGuard $makePropertyPromotionGuard)
|
2021-05-09 20:15:43 +00:00
|
|
|
|
{
|
2021-06-18 19:29:49 +00:00
|
|
|
|
$this->promotedPropertyCandidateResolver = $promotedPropertyCandidateResolver;
|
2020-12-09 23:40:38 +00:00
|
|
|
|
$this->variableRenamer = $variableRenamer;
|
2021-01-16 16:45:12 +00:00
|
|
|
|
$this->varTagRemover = $varTagRemover;
|
2021-07-14 07:23:54 +00:00
|
|
|
|
$this->paramAnalyzer = $paramAnalyzer;
|
2021-08-23 11:12:57 +00:00
|
|
|
|
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
2022-11-29 15:48:49 +00:00
|
|
|
|
$this->makePropertyPromotionGuard = $makePropertyPromotionGuard;
|
2020-12-09 23:40:38 +00:00
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-06-08 09:28:03 +00:00
|
|
|
|
{
|
2022-11-29 15:48:49 +00:00
|
|
|
|
return new RuleDefinition('Change simple property init and assign to constructor promotion', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2020-06-08 09:28:03 +00:00
|
|
|
|
class SomeClass
|
|
|
|
|
{
|
2020-12-09 23:40:38 +00:00
|
|
|
|
public float $someVariable;
|
|
|
|
|
|
2021-11-25 19:08:52 +00:00
|
|
|
|
public function __construct(
|
|
|
|
|
float $someVariable = 0.0
|
|
|
|
|
) {
|
2020-12-09 23:40:38 +00:00
|
|
|
|
$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
|
|
|
|
|
{
|
2021-11-25 19:08:52 +00:00
|
|
|
|
public function __construct(
|
2022-07-25 20:31:54 +00:00
|
|
|
|
public float $someVariable = 0.0
|
2021-11-25 19:08:52 +00:00
|
|
|
|
) {
|
2020-12-09 23:40:38 +00:00
|
|
|
|
}
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
|
CODE_SAMPLE
|
2022-11-29 15:48:49 +00:00
|
|
|
|
, [self::INLINE_PUBLIC => \false])]);
|
|
|
|
|
}
|
|
|
|
|
public function configure(array $configuration) : void
|
|
|
|
|
{
|
|
|
|
|
$this->inlinePublic = $configuration[self::INLINE_PUBLIC] ?? (bool) \current($configuration);
|
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
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
|
return [Class_::class];
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
|
* @param Class_ $node
|
2020-06-08 09:28:03 +00:00
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
|
public function refactor(Node $node) : ?Node
|
2020-06-08 09:28:03 +00:00
|
|
|
|
{
|
2021-06-18 19:29:49 +00:00
|
|
|
|
$promotionCandidates = $this->promotedPropertyCandidateResolver->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 */
|
2022-06-07 08:22:29 +00:00
|
|
|
|
$constructClassMethod = $node->getMethod(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();
|
2021-06-30 13:08:35 +00:00
|
|
|
|
if ($this->shouldSkipParam($param)) {
|
2021-05-17 18:55:10 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-29 15:48:49 +00:00
|
|
|
|
if (!$this->makePropertyPromotionGuard->isLegal($node, $property, $param, $this->inlinePublic)) {
|
2021-10-29 14:22:26 +00:00
|
|
|
|
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);
|
2022-07-23 09:02:51 +00:00
|
|
|
|
$paramTagValueNode = $classMethodPhpDocInfo->getParamTagValueByName($paramName);
|
2022-06-07 08:22:29 +00:00
|
|
|
|
if (!$paramTagValueNode instanceof ParamTagValueNode) {
|
2022-06-01 16:36:10 +00:00
|
|
|
|
$this->decorateParamWithPropertyPhpDocInfo($constructClassMethod, $property, $param, $paramName);
|
2021-05-06 10:59:29 +00:00
|
|
|
|
} elseif ($paramTagValueNode->parameterName !== '$' . $propertyName) {
|
|
|
|
|
$paramTagValueNode->parameterName = '$' . $propertyName;
|
2022-06-07 08:22:29 +00:00
|
|
|
|
$paramTagValueNode->setAttribute(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
|
2023-05-19 13:30:27 +00:00
|
|
|
|
$paramName = $this->getName($property);
|
|
|
|
|
$param->var = new Variable($paramName);
|
2020-06-08 09:28:03 +00:00
|
|
|
|
$param->flags = $property->flags;
|
2021-07-22 17:17:29 +00:00
|
|
|
|
// Copy over attributes of the "old" property
|
2022-12-10 11:57:52 +00:00
|
|
|
|
$param->attrGroups = \array_merge($param->attrGroups, $property->attrGroups);
|
2021-02-22 09:59:51 +00:00
|
|
|
|
$this->processNullableType($property, $param);
|
2023-05-29 23:40:49 +00:00
|
|
|
|
$this->phpDocTypeChanger->copyPropertyDocToParam($constructClassMethod, $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-07-21 09:35:57 +00:00
|
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
|
return PhpVersionFeature::PROPERTY_PROMOTION;
|
2021-07-21 09:35:57 +00:00
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
private function processNullableType(Property $property, Param $param) : void
|
2021-02-22 09:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
if ($this->nodeTypeResolver->isNullableType($property)) {
|
2021-10-07 17:46:41 +00:00
|
|
|
|
$objectType = $this->getType($property);
|
2022-06-07 08:22:29 +00:00
|
|
|
|
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType, TypeKind::PARAM);
|
2021-02-22 09:59:51 +00:00
|
|
|
|
}
|
2023-05-14 15:10:59 +00:00
|
|
|
|
if ($param->default instanceof Expr && $this->valueResolver->isNull($param->default)) {
|
|
|
|
|
$paramType = $this->getType($param);
|
|
|
|
|
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType, TypeKind::PARAM);
|
|
|
|
|
}
|
2021-02-22 09:59:51 +00:00
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
private function decorateParamWithPropertyPhpDocInfo(ClassMethod $classMethod, Property $property, Param $param, string $paramName) : 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();
|
2022-06-07 08:22:29 +00:00
|
|
|
|
$param->setAttribute(AttributeKey::PHP_DOC_INFO, $propertyPhpDocInfo);
|
2021-01-16 16:45:12 +00:00
|
|
|
|
// make sure the docblock is useful
|
|
|
|
|
if ($param->type === null) {
|
2022-06-01 16:36:10 +00:00
|
|
|
|
$varTagValueNode = $propertyPhpDocInfo->getVarTagValueNode();
|
2022-06-07 08:22:29 +00:00
|
|
|
|
if (!$varTagValueNode instanceof VarTagValueNode) {
|
2022-06-01 16:36:10 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$paramType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($varTagValueNode, $property);
|
|
|
|
|
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
|
|
|
|
|
$this->phpDocTypeChanger->changeParamType($classMethodPhpDocInfo, $paramType, $param, $paramName);
|
|
|
|
|
} else {
|
|
|
|
|
$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
|
2021-01-16 16:45:12 +00:00
|
|
|
|
}
|
|
|
|
|
$this->varTagRemover->removeVarPhpTagValueNodeIfNotComment($param, $paramType);
|
2020-12-02 14:26:14 +00:00
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
private function shouldSkipParam(Param $param) : bool
|
2021-06-30 13:08:35 +00:00
|
|
|
|
{
|
|
|
|
|
if ($param->variadic) {
|
|
|
|
|
return \true;
|
|
|
|
|
}
|
2021-07-14 07:23:54 +00:00
|
|
|
|
if ($this->paramAnalyzer->isNullable($param)) {
|
|
|
|
|
/** @var NullableType $type */
|
|
|
|
|
$type = $param->type;
|
|
|
|
|
$type = $type->type;
|
|
|
|
|
} else {
|
|
|
|
|
$type = $param->type;
|
|
|
|
|
}
|
2022-10-24 08:19:06 +00:00
|
|
|
|
if ($this->isCallableTypeIdentifier($type)) {
|
|
|
|
|
return \true;
|
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
if (!$type instanceof UnionType) {
|
2021-10-29 14:22:26 +00:00
|
|
|
|
return \false;
|
|
|
|
|
}
|
|
|
|
|
foreach ($type->types as $type) {
|
2022-10-24 08:19:06 +00:00
|
|
|
|
if ($this->isCallableTypeIdentifier($type)) {
|
|
|
|
|
return \true;
|
2021-10-29 14:22:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return \false;
|
2021-06-30 13:08:35 +00:00
|
|
|
|
}
|
2022-10-24 08:19:06 +00:00
|
|
|
|
private function isCallableTypeIdentifier(?Node $node) : bool
|
|
|
|
|
{
|
|
|
|
|
return $node instanceof Identifier && $this->isName($node, 'callable');
|
|
|
|
|
}
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|