[Php81] Skip ReadOnlyPropertyRector on Entity id (#1379)

* [Php81] Skip ReadOnlyPropertyRector on Entity id

* update fixture

* Fixed 🎉

* clean up
This commit is contained in:
Abdul Malik Ikhsan 2021-12-04 14:41:49 +07:00 committed by GitHub
parent df1391d979
commit 8547bdba9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 156 additions and 1 deletions

View File

@ -0,0 +1,66 @@
<?php
namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;
use Ramsey\Uuid\UuidInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* News
*
* @ORM\Table(name="news")
* @ORM\Entity(repositoryClass="DoctrineFixtureDemo\Repository\NewsRepository")
*/
class News
{
/**
* @var integer
*
* @ORM\Id()
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
*/
private UuidInterface $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=50, nullable=false)
*/
private $title;
/**
* Get id.
*
* @return string
*/
public function getId()
{
return (string) $this->id;
}
/**
* Set title.
*
* @param string $title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;
use Ramsey\Uuid\UuidInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* News
*/
#[ORM\Table(name: 'news')]
#[ORM\Entity(repositoryClass: 'DoctrineFixtureDemo\Repository\NewsRepository')]
class News2
{
/**
* @var UuidInterface
*/
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'Ramsey\Uuid\Doctrine\UuidGenerator')]
private UuidInterface $id;
/**
* @var string
*/
#[ORM\Column(name: 'title', type: 'string', length: 50, nullable: false)]
private $title;
/**
* Get id.
*
* @return string
*/
public function getId()
{
return (string) $this->id;
}
/**
* Set title.
*
* @param string $title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}

View File

@ -16,6 +16,7 @@ use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
@ -27,6 +28,7 @@ use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\ReadWrite\Guard\VariableToConstantGuard;
use Rector\ReadWrite\NodeAnalyzer\ReadWritePropertyAnalyzer;
use Symplify\PackageBuilder\Php\TypeChecker;
@ -37,6 +39,14 @@ use Symplify\PackageBuilder\Php\TypeChecker;
*/
final class PropertyManipulator
{
/**
* @var string[]
*/
private const ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES = [
'Doctrine\ORM\Mapping\Entity',
'Doctrine\ORM\Mapping\Table',
];
public function __construct(
private AssignManipulator $assignManipulator,
private BetterNodeFinder $betterNodeFinder,
@ -46,7 +56,8 @@ final class PropertyManipulator
private TypeChecker $typeChecker,
private PropertyFetchFinder $propertyFetchFinder,
private ReflectionResolver $reflectionResolver,
private NodeNameResolver $nodeNameResolver
private NodeNameResolver $nodeNameResolver,
private PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}
@ -96,6 +107,21 @@ final class PropertyManipulator
public function isPropertyChangeableExceptConstructor(Property | Param $propertyOrParam): bool
{
$class = $this->betterNodeFinder->findParentType($propertyOrParam, Class_::class);
if ($class instanceof Class_) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
if ($phpDocInfo->hasByAnnotationClasses(self::ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES)) {
return true;
}
if ($this->phpAttributeAnalyzer->hasPhpAttributes(
$class,
self::ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES
)) {
return true;
}
}
$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($propertyOrParam);
foreach ($propertyFetches as $propertyFetch) {