2021-07-22 23:37:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\Php81\Rector\Property;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2021-11-30 16:50:00 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2021-07-22 23:37:17 +00:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use Rector\Core\NodeManipulator\PropertyManipulator;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-09-02 15:24:03 +00:00
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
2021-12-02 10:13:44 +00:00
|
|
|
use Rector\Core\ValueObject\Visibility;
|
2021-11-24 11:00:16 +00:00
|
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
2021-09-02 15:24:03 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2021-07-22 23:37:17 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
/**
|
|
|
|
* @changelog https://wiki.php.net/rfc/readonly_properties_v2
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest
|
|
|
|
*/
|
2021-09-02 15:24:03 +00:00
|
|
|
final class ReadOnlyPropertyRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\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;
|
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;
|
|
|
|
public function __construct(\Rector\Core\NodeManipulator\PropertyManipulator $propertyManipulator, \Rector\Privatization\NodeManipulator\VisibilityManipulator $visibilityManipulator)
|
2021-07-22 23:37:17 +00:00
|
|
|
{
|
|
|
|
$this->propertyManipulator = $propertyManipulator;
|
2021-11-24 11:00:16 +00:00
|
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
2021-07-22 23:37:17 +00:00
|
|
|
}
|
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
|
|
{
|
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Decorate read-only property with `readonly` attribute', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return [\PhpParser\Node\Stmt\Property::class, \PhpParser\Node\Param::class];
|
|
|
|
}
|
|
|
|
/**
|
2021-12-10 09:57:54 +00:00
|
|
|
* @param \PhpParser\Node $node
|
2021-07-22 23:37:17 +00:00
|
|
|
*/
|
2021-12-10 09:57:54 +00:00
|
|
|
public function refactor($node) : ?\PhpParser\Node
|
2021-07-22 23:37:17 +00:00
|
|
|
{
|
|
|
|
if ($node instanceof \PhpParser\Node\Param) {
|
|
|
|
return $this->refactorParam($node);
|
|
|
|
}
|
|
|
|
// 1. is property read-only?
|
|
|
|
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-12-01 09:10:03 +00:00
|
|
|
if ($node->isReadonly()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($node->props[0]->default instanceof \PhpParser\Node\Expr) {
|
2021-07-22 23:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-11-30 16:51:32 +00:00
|
|
|
if ($node->type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-12-02 10:13:44 +00:00
|
|
|
if ($node->flags !== \Rector\Core\ValueObject\Visibility::PRIVATE) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
$this->visibilityManipulator->makeReadonly($node);
|
|
|
|
return $node;
|
|
|
|
}
|
2021-09-05 00:27:40 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
|
|
|
return \Rector\Core\ValueObject\PhpVersionFeature::READONLY_PROPERTY;
|
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
/**
|
|
|
|
* @return \PhpParser\Node\Param|null
|
|
|
|
*/
|
|
|
|
private function refactorParam(\PhpParser\Node\Param $param)
|
|
|
|
{
|
2021-12-02 10:13:44 +00:00
|
|
|
if ($param->flags !== \Rector\Core\ValueObject\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?
|
|
|
|
if ($this->propertyManipulator->isPropertyChangeableExceptConstructor($param)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
$this->visibilityManipulator->makeReadonly($param);
|
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
}
|