rectorphp/rector-doctrine

ManyToOne relations with JoinColumns are not being replaced with an array of JoinColumn

pedrocasado opened this issue · 7 comments

Looks like there's a bug on the ManyToOne relation with the JoinColumns annotation which ends out with the following error.

[Syntax Error] Expected PlainValue, got '(' at position 166 in property App\Entity\Product::$inventoryPosition.

The documentation says the @JoinColumns needs an array of JoinColumn:

An array of @joincolumn annotations for a @ManyToOne or @OneToOne relation with an entity that has multiple identifiers.

Take a look the code below removing the @Orm\JoinColumn annotation.

     /**
      * @var InventoryPosition
      *
      * @ORM\ManyToOne(targetEntity="App\Entity\InventoryPosition", inversedBy="products", cascade={"persist"})
-     * @ORM\JoinColumns({
-     *   @ORM\JoinColumn(name="inventory_position_id", referencedColumnName="id")
-     * })
+     * @ORM\JoinColumns({(name="inventory_position_id")})
      */
-    private $inventoryPosition;
+    private ?\App\Entity\InventoryPosition $inventoryPosition = null;

I'm using the DoctrineSetList::DOCTRINE_CODE_QUALITY setlist

Thanks for report. Could you try the latest dev-main? It should be fixed

Updated to dev-main a0b6b46

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * Class Teste.
 *
 * @ORM\Entity()
 */
class Teste
{
    /**
     * @var InventoryLocation
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\InventoryLocation", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="inventory_location_id", referencedColumnName="id")
     * })
     */
    private $inventoryLocation;

    public function getInventoryLocation(): ?InventoryLocation
    {
        return $this->inventoryLocation;
    }

    public function setInventoryLocation(?InventoryLocation $inventoryLocation): self
    {
        $this->inventoryLocation = $inventoryLocation;

        return $this;
    }
}

Debug

$ vendor/bin/rector process src/Entity/Teste.php --dry-run --debug
src/Entity/Teste.php
[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\ManagerRegistryGetManagerToEntityManagerRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\InitializeDefaultEntityCollectionRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\RemoveRedundantDefaultClassAnnotationValuesRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\MakeEntityDateTimePropertyDateTimeInterfaceRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\RemoveRedundantDefaultPropertyAnnotationValuesRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\TypedPropertyFromColumnTypeRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\TypedPropertyFromToOneRelationTypeRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\TypedPropertyFromToManyRelationTypeRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\TypedPropertyFromDoctrineCollectionRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\ManagerRegistryGetManagerToEntityManagerRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\InitializeDefaultEntityCollectionRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Class_\RemoveRedundantDefaultClassAnnotationValuesRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\MakeEntityDateTimePropertyDateTimeInterfaceRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\TypedPropertyFromColumnTypeRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\TypedPropertyFromToManyRelationTypeRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\TypedPropertyFromDoctrineCollectionRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector

[file] src/Entity/Teste.php
[rule] Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector


1 file with changes
===================

1) src/Entity/Teste.php:14

    ---------- begin diff ----------
@@ @@
      * @var InventoryLocation
      *
      * @ORM\ManyToOne(targetEntity="App\Entity\InventoryLocation", inversedBy="products", cascade={"persist"})
-     * @ORM\JoinColumns({
-     *   @ORM\JoinColumn(name="inventory_location_id", referencedColumnName="id")
-     * })
+     * @ORM\JoinColumns({(name="inventory_location_id")})
      */
-    private $inventoryLocation;
+    private ?\App\Entity\InventoryLocation $inventoryLocation = null;

     public function getInventoryLocation(): ?InventoryLocation
     {
    ----------- end diff -----------

Applied rules:
 * RemoveRedundantDefaultPropertyAnnotationValuesRector (https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/basic-mapping.html#property-mapping)
 * TypedPropertyFromToOneRelationTypeRector

I'm unable to read this one. Could you create a reproducible repository?

Thanks 👍 Seems only RemoveRedundantDefaultPropertyAnnotationValuesRector is needed.
Could you narrow the demo to the rule instead of the whole set?

Awesome 👍 Thanks.

The last step would be to create a test from that. The "Create Test" button will help you.