-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform manyToMany from yaml to attributes (#308)
- Loading branch information
Showing
9 changed files
with
236 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...deQuality/Rector/Class_/YamlToAttributeDoctrineMappingRector/Fixture/many_to_many.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\YamlToAttributeDoctrineMappingRector\Fixture; | ||
|
||
final class ManyToMany | ||
{ | ||
public $myObjects; | ||
|
||
public $theirObjects; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\YamlToAttributeDoctrineMappingRector\Fixture; | ||
|
||
#[\Doctrine\ORM\Mapping\Table] | ||
final class ManyToMany | ||
{ | ||
#[\Doctrine\ORM\Mapping\ManyToMany(targetEntity: \App\SomeTargetEntity::class, inversedBy: 'theirObjects')] | ||
#[\Doctrine\ORM\Mapping\JoinTable(name: 'owned_objects')] | ||
#[\Doctrine\ORM\Mapping\JoinColumn(name: 'owner_id', referencedColumnName: 'some_id', onDelete: 'cascade')] | ||
#[\Doctrine\ORM\Mapping\InverseJoinColumn(name: 'object_id', referencedColumnName: 'some_id', onDelete: 'cascade')] | ||
public $myObjects; | ||
|
||
#[\Doctrine\ORM\Mapping\ManyToMany(targetEntity: \App\SomeTargetEntity::class, mappedBy: 'myObjects')] | ||
public $theirObjects; | ||
} | ||
|
||
?> |
19 changes: 19 additions & 0 deletions
19
.../Rector/Class_/YamlToAttributeDoctrineMappingRector/config/yaml_mapping/many_to_many.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Rector\Doctrine\Tests\CodeQuality\Rector\Class_\YamlToAttributeDoctrineMappingRector\Fixture\ManyToMany: | ||
manyToMany: | ||
myObjects: | ||
targetEntity: App\SomeTargetEntity | ||
inversedBy: theirObjects | ||
joinTable: | ||
name: owned_objects | ||
joinColumns: | ||
owner_id: | ||
referencedColumnName: some_id | ||
onDelete: cascade | ||
inverseJoinColumns: | ||
object_id: | ||
referencedColumnName: some_id | ||
onDelete: cascade | ||
|
||
theirObjects: | ||
targetEntity: App\SomeTargetEntity | ||
mappedBy: myObjects |
55 changes: 55 additions & 0 deletions
55
...tributeTransformer/PropertyAttributeTransformer/InverseJoinColumnAttributeTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\CodeQuality\AttributeTransformer\PropertyAttributeTransformer; | ||
|
||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\Property; | ||
use Rector\Doctrine\CodeQuality\Contract\PropertyAttributeTransformerInterface; | ||
use Rector\Doctrine\CodeQuality\NodeFactory\AttributeFactory; | ||
use Rector\Doctrine\CodeQuality\ValueObject\EntityMapping; | ||
use Rector\Doctrine\Enum\MappingClass; | ||
use Rector\PhpParser\Node\NodeFactory; | ||
|
||
final readonly class InverseJoinColumnAttributeTransformer implements PropertyAttributeTransformerInterface | ||
{ | ||
public function __construct( | ||
private NodeFactory $nodeFactory | ||
) { | ||
} | ||
|
||
public function transform(EntityMapping $entityMapping, Property|Param $property): void | ||
{ | ||
$joinTableMapping = $entityMapping->matchManyToManyPropertyMapping($property)['joinTable'] ?? null; | ||
if (! is_array($joinTableMapping)) { | ||
return; | ||
} | ||
|
||
$joinColumns = $joinTableMapping['inverseJoinColumns'] ?? null; | ||
if (! is_array($joinColumns)) { | ||
return; | ||
} | ||
|
||
foreach ($joinColumns as $columnName => $joinColumn) { | ||
$property->attrGroups[] = $this->createInverseJoinColumnAttrGroup($columnName, $joinColumn); | ||
} | ||
} | ||
|
||
public function getClassName(): string | ||
{ | ||
return MappingClass::INVERSE_JOIN_COLUMN; | ||
} | ||
|
||
private function createInverseJoinColumnAttrGroup(int|string $columnName, mixed $joinColumn): AttributeGroup | ||
{ | ||
$joinColumn = array_merge([ | ||
'name' => $columnName, | ||
], $joinColumn); | ||
|
||
$args = $this->nodeFactory->createArgs($joinColumn); | ||
|
||
return AttributeFactory::createGroup($this->getClassName(), $args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ality/AttributeTransformer/PropertyAttributeTransformer/JoinTableAttributeTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\CodeQuality\AttributeTransformer\PropertyAttributeTransformer; | ||
|
||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\Property; | ||
use Rector\Doctrine\CodeQuality\Contract\PropertyAttributeTransformerInterface; | ||
use Rector\Doctrine\CodeQuality\Enum\EntityMappingKey; | ||
use Rector\Doctrine\CodeQuality\Helper\NodeValueNormalizer; | ||
use Rector\Doctrine\CodeQuality\NodeFactory\AttributeFactory; | ||
use Rector\Doctrine\CodeQuality\ValueObject\EntityMapping; | ||
use Rector\Doctrine\Enum\MappingClass; | ||
use Rector\PhpParser\Node\NodeFactory; | ||
|
||
final readonly class JoinTableAttributeTransformer implements PropertyAttributeTransformerInterface | ||
{ | ||
public function __construct( | ||
private NodeFactory $nodeFactory | ||
) { | ||
} | ||
|
||
public function transform(EntityMapping $entityMapping, Property|Param $property): void | ||
{ | ||
$joinTableMapping = $entityMapping->matchManyToManyPropertyMapping($property)['joinTable'] ?? null; | ||
if (! is_array($joinTableMapping)) { | ||
return; | ||
} | ||
|
||
// handled by another mapper | ||
unset($joinTableMapping['joinColumns'], $joinTableMapping['inverseJoinColumns']); | ||
|
||
$args = $this->nodeFactory->createArgs($joinTableMapping); | ||
$property->attrGroups[] = AttributeFactory::createGroup($this->getClassName(), $args); | ||
|
||
NodeValueNormalizer::ensureKeyIsClassConstFetch($args, EntityMappingKey::TARGET_ENTITY); | ||
} | ||
|
||
public function getClassName(): string | ||
{ | ||
return MappingClass::JOIN_TABLE; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...lity/AttributeTransformer/PropertyAttributeTransformer/ManyToManyAttributeTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Doctrine\CodeQuality\AttributeTransformer\PropertyAttributeTransformer; | ||
|
||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\Property; | ||
use Rector\Doctrine\CodeQuality\Contract\PropertyAttributeTransformerInterface; | ||
use Rector\Doctrine\CodeQuality\Enum\EntityMappingKey; | ||
use Rector\Doctrine\CodeQuality\Helper\NodeValueNormalizer; | ||
use Rector\Doctrine\CodeQuality\NodeFactory\AttributeFactory; | ||
use Rector\Doctrine\CodeQuality\ValueObject\EntityMapping; | ||
use Rector\Doctrine\Enum\MappingClass; | ||
use Rector\PhpParser\Node\NodeFactory; | ||
|
||
final readonly class ManyToManyAttributeTransformer implements PropertyAttributeTransformerInterface | ||
{ | ||
public function __construct( | ||
private NodeFactory $nodeFactory | ||
) { | ||
} | ||
|
||
public function transform(EntityMapping $entityMapping, Property|Param $property): void | ||
{ | ||
$manyToManyMapping = $entityMapping->matchManyToManyPropertyMapping($property); | ||
if (! is_array($manyToManyMapping)) { | ||
return; | ||
} | ||
|
||
// handled by another mapper | ||
unset($manyToManyMapping['joinTable']); | ||
|
||
$args = $this->nodeFactory->createArgs($manyToManyMapping); | ||
$property->attrGroups[] = AttributeFactory::createGroup($this->getClassName(), $args); | ||
|
||
NodeValueNormalizer::ensureKeyIsClassConstFetch($args, EntityMappingKey::TARGET_ENTITY); | ||
} | ||
|
||
public function getClassName(): string | ||
{ | ||
return MappingClass::MANY_TO_MANY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters