2021-07-22 23:37:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php81\Rector\Property;
|
2021-07-22 23:37:17 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
2022-10-05 09:16:10 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Param;
|
2022-10-05 09:16:10 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Property;
|
2022-10-05 09:16:10 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2023-05-14 10:01:33 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2022-06-17 07:22:25 +00:00
|
|
|
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\NodeManipulator\PropertyFetchAssignManipulator;
|
|
|
|
use Rector\Core\NodeManipulator\PropertyManipulator;
|
2023-05-14 10:01:33 +00:00
|
|
|
use Rector\Core\Rector\AbstractScopeAwareRector;
|
2022-10-05 09:16:10 +00:00
|
|
|
use Rector\Core\ValueObject\MethodName;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\Core\ValueObject\Visibility;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-07-22 23:37:17 +00:00
|
|
|
/**
|
|
|
|
* @changelog https://wiki.php.net/rfc/readonly_properties_v2
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest
|
|
|
|
*/
|
2023-05-14 10:01:33 +00:00
|
|
|
final class ReadOnlyPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
2021-07-22 23:37:17 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-07-22 23:37:17 +00:00
|
|
|
* @var \Rector\Core\NodeManipulator\PropertyManipulator
|
|
|
|
*/
|
|
|
|
private $propertyManipulator;
|
2022-04-17 15:25:10 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\NodeManipulator\PropertyFetchAssignManipulator
|
|
|
|
*/
|
|
|
|
private $propertyFetchAssignManipulator;
|
2022-06-17 07:22:25 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\NodeAnalyzer\ParamAnalyzer
|
|
|
|
*/
|
|
|
|
private $paramAnalyzer;
|
2021-11-24 11:00:16 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-11-24 11:00:16 +00:00
|
|
|
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
|
|
|
|
*/
|
|
|
|
private $visibilityManipulator;
|
2023-01-29 06:15:33 +00:00
|
|
|
public function __construct(PropertyManipulator $propertyManipulator, PropertyFetchAssignManipulator $propertyFetchAssignManipulator, ParamAnalyzer $paramAnalyzer, VisibilityManipulator $visibilityManipulator)
|
2021-07-22 23:37:17 +00:00
|
|
|
{
|
|
|
|
$this->propertyManipulator = $propertyManipulator;
|
2022-04-17 15:25:10 +00:00
|
|
|
$this->propertyFetchAssignManipulator = $propertyFetchAssignManipulator;
|
2022-06-17 07:22:25 +00:00
|
|
|
$this->paramAnalyzer = $paramAnalyzer;
|
2021-11-24 11:00:16 +00:00
|
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
2021-07-22 23:37:17 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-07-22 23:37:17 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Decorate read-only property with `readonly` attribute', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-07-22 23:37:17 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private string $name
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private readonly string $name
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2023-05-30 13:05:48 +00:00
|
|
|
return [Class_::class];
|
2021-07-22 23:37:17 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-30 13:05:48 +00:00
|
|
|
* @param Class_ $node
|
2021-07-22 23:37:17 +00:00
|
|
|
*/
|
2023-05-14 10:01:33 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2021-07-22 23:37:17 +00:00
|
|
|
{
|
2023-05-30 13:05:48 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
|
|
|
foreach ($classMethod->params as $param) {
|
|
|
|
$justChanged = $this->refactorParam($node, $classMethod, $param, $scope);
|
|
|
|
// different variable to ensure $hasRemoved not replaced
|
2023-05-28 14:30:49 +00:00
|
|
|
if ($justChanged instanceof Param) {
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 13:05:48 +00:00
|
|
|
}
|
|
|
|
foreach ($node->getProperties() as $property) {
|
|
|
|
$changedProperty = $this->refactorProperty($node, $property, $scope);
|
|
|
|
if ($changedProperty instanceof Property) {
|
|
|
|
$hasChanged = \true;
|
2023-05-28 14:30:49 +00:00
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
}
|
2023-05-30 13:05:48 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
2022-02-16 21:07:17 +00:00
|
|
|
}
|
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::READONLY_PROPERTY;
|
2022-02-16 21:07:17 +00:00
|
|
|
}
|
2022-12-22 14:22:43 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $node
|
|
|
|
*/
|
|
|
|
private function shouldSkipInReadonlyClass($node) : bool
|
|
|
|
{
|
|
|
|
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
|
|
|
|
if (!$class instanceof Class_) {
|
|
|
|
return \true;
|
|
|
|
}
|
2023-01-29 06:15:33 +00:00
|
|
|
return $class->isReadonly();
|
2022-12-22 14:22:43 +00:00
|
|
|
}
|
2023-05-30 13:05:48 +00:00
|
|
|
private function refactorProperty(Class_ $class, Property $property, Scope $scope) : ?Property
|
2022-02-16 21:07:17 +00:00
|
|
|
{
|
2021-07-22 23:37:17 +00:00
|
|
|
// 1. is property read-only?
|
2022-02-16 21:07:17 +00:00
|
|
|
if ($property->isReadonly()) {
|
2021-12-01 09:10:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($property->props[0]->default instanceof Expr) {
|
2021-07-22 23:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-02-16 21:07:17 +00:00
|
|
|
if ($property->type === null) {
|
2021-11-30 16:51:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-04-16 10:42:58 +00:00
|
|
|
if ($property->isStatic()) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$this->visibilityManipulator->hasVisibility($property, Visibility::PRIVATE)) {
|
2021-12-02 10:13:44 +00:00
|
|
|
return null;
|
2022-03-16 09:24:07 +00:00
|
|
|
}
|
2023-05-30 13:05:48 +00:00
|
|
|
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($class, $property, $scope)) {
|
2022-03-16 09:24:07 +00:00
|
|
|
return null;
|
2022-04-17 15:25:10 +00:00
|
|
|
}
|
2023-05-31 13:27:13 +00:00
|
|
|
if ($this->propertyFetchAssignManipulator->isAssignedMultipleTimesInConstructor($class, $property)) {
|
2022-04-17 15:25:10 +00:00
|
|
|
return null;
|
2021-12-02 10:13:44 +00:00
|
|
|
}
|
2022-12-22 14:22:43 +00:00
|
|
|
if ($this->shouldSkipInReadonlyClass($property)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-02-16 21:07:17 +00:00
|
|
|
$this->visibilityManipulator->makeReadonly($property);
|
2022-02-25 12:34:50 +00:00
|
|
|
$attributeGroups = $property->attrGroups;
|
|
|
|
if ($attributeGroups !== []) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
2022-02-25 12:34:50 +00:00
|
|
|
}
|
2022-02-16 21:07:17 +00:00
|
|
|
return $property;
|
2021-09-05 00:27:40 +00:00
|
|
|
}
|
2023-05-30 13:05:48 +00:00
|
|
|
private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $param, Scope $scope) : ?\PhpParser\Node\Param
|
2021-07-22 23:37:17 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
|
2021-07-22 23:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-12-02 07:35:25 +00:00
|
|
|
if ($param->type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
// promoted property?
|
2023-05-30 13:05:48 +00:00
|
|
|
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($class, $param, $scope)) {
|
2021-07-22 23:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-11-18 19:20:31 +00:00
|
|
|
if ($this->visibilityManipulator->isReadonly($param)) {
|
2021-07-22 23:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-28 14:30:49 +00:00
|
|
|
if ($this->paramAnalyzer->isParamReassign($classMethod, $param)) {
|
2022-06-17 07:22:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-10-05 09:16:10 +00:00
|
|
|
if ($this->isPromotedPropertyAssigned($param)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-12-22 14:22:43 +00:00
|
|
|
if ($this->shouldSkipInReadonlyClass($param)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
$this->visibilityManipulator->makeReadonly($param);
|
|
|
|
return $param;
|
|
|
|
}
|
2022-10-05 09:16:10 +00:00
|
|
|
private function isPromotedPropertyAssigned(Param $param) : bool
|
|
|
|
{
|
|
|
|
$classLike = $this->betterNodeFinder->findParentType($param, ClassLike::class);
|
|
|
|
if (!$classLike instanceof Class_) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$constructClassMethod = $classLike->getMethod(MethodName::CONSTRUCT);
|
|
|
|
if (!$constructClassMethod instanceof ClassMethod) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if ($param->flags === 0) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$propertyFetch = new PropertyFetch(new Variable('this'), $this->getName($param));
|
|
|
|
$stmts = $classLike->stmts;
|
|
|
|
$isAssigned = \false;
|
|
|
|
$this->traverseNodesWithCallable($stmts, function (Node $node) use($propertyFetch, &$isAssigned) : ?int {
|
|
|
|
if ($node instanceof Assign && $this->nodeComparator->areNodesEqual($propertyFetch, $node->var)) {
|
|
|
|
$isAssigned = \true;
|
|
|
|
return NodeTraverser::STOP_TRAVERSAL;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
return $isAssigned;
|
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
}
|