From e70cc832a1aabfff9cdb3de39262de1987e6e081 Mon Sep 17 00:00:00 2001 From: Jesse Rushlow Date: Thu, 19 May 2022 09:58:08 -0400 Subject: [PATCH] drop annotation support with entities --- composer.json | 4 +- .../DoctrineAttributesCheckPass.php | 26 -- .../SetDoctrineAnnotatedPrefixesPass.php | 2 +- src/Doctrine/BaseCollectionRelation.php | 2 - src/Doctrine/BaseRelation.php | 80 ++---- src/Doctrine/BaseSingleRelation.php | 36 --- src/Doctrine/DoctrineHelper.php | 61 ++--- src/Doctrine/EntityClassGenerator.php | 1 - src/Doctrine/EntityRegenerator.php | 57 ++--- src/Doctrine/EntityRelation.php | 88 +++---- src/Doctrine/RelationManyToMany.php | 19 -- src/Doctrine/RelationManyToOne.php | 6 +- src/Doctrine/RelationOneToMany.php | 19 -- src/Doctrine/RelationOneToOne.php | 16 +- src/Maker/MakeEntity.php | 53 +--- src/Maker/MakeRegistrationForm.php | 4 +- src/Maker/MakeResetPassword.php | 16 +- src/MakerBundle.php | 4 +- src/Resources/config/services.xml | 2 - .../skeleton/doctrine/Entity.tpl.php | 18 +- src/Util/ClassSourceManipulator.php | 4 +- tests/Doctrine/EntityRegeneratorTest.php | 6 +- .../src/Entity/BaseClient.php | 24 +- .../src/Entity/Client.php | 14 +- .../src/Entity/Embed.php | 8 +- .../expected_no_overwrite/src/Entity/Tag.php | 12 +- .../expected_no_overwrite/src/Entity/User.php | 24 +- .../src/Entity/UserAvatar.php | 18 +- .../src/Entity/UserProfile.php | 18 +- .../src/Entity/BaseClient.php | 24 +- .../expected_overwrite/src/Entity/Client.php | 14 +- .../expected_overwrite/src/Entity/Embed.php | 8 +- .../expected_overwrite/src/Entity/Tag.php | 12 +- .../expected_overwrite/src/Entity/User.php | 24 +- .../src/Entity/UserAvatar.php | 18 +- .../src/Entity/UserProfile.php | 18 +- .../source_project/src/Entity/BaseClient.php | 24 +- .../source_project/src/Entity/Client.php | 14 +- .../source_project/src/Entity/Embed.php | 8 +- .../source_project/src/Entity/Tag.php | 12 +- .../source_project/src/Entity/TeamTrait.php | 4 +- .../src/Entity/TimestampableTrait.php | 8 +- .../source_project/src/Entity/User.php | 24 +- .../source_project/src/Entity/UserAvatar.php | 18 +- .../source_project/src/Entity/UserProfile.php | 18 +- tests/Maker/MakeCrudTest.php | 2 - tests/Maker/MakeEntityTest.php | 19 +- tests/Maker/TestHelpersTrait.php | 30 --- tests/Util/ClassSourceManipulatorTest.php | 238 ++++++++++-------- 49 files changed, 393 insertions(+), 786 deletions(-) delete mode 100644 src/DependencyInjection/CompilerPass/DoctrineAttributesCheckPass.php delete mode 100644 src/Doctrine/BaseSingleRelation.php delete mode 100644 tests/Maker/TestHelpersTrait.php diff --git a/composer.json b/composer.json index 5c378a8ac..ef5dea418 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,9 @@ "sort-packages": true }, "conflict": { - "doctrine/orm": "<2.10" + "doctrine/orm": "<2.10", + "doctrine/doctrine-bundle": "<2.4", + "symfony/doctrine-bridge": "<5.4" }, "autoload": { "psr-4": { "Symfony\\Bundle\\MakerBundle\\": "src/" } diff --git a/src/DependencyInjection/CompilerPass/DoctrineAttributesCheckPass.php b/src/DependencyInjection/CompilerPass/DoctrineAttributesCheckPass.php deleted file mode 100644 index 37980b64f..000000000 --- a/src/DependencyInjection/CompilerPass/DoctrineAttributesCheckPass.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass; - -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\ContainerBuilder; - -class DoctrineAttributesCheckPass implements CompilerPassInterface -{ - public function process(ContainerBuilder $container): void - { - $container->setParameter( - 'maker.compatible_check.doctrine.supports_attributes', - $container->hasParameter('doctrine.orm.metadata.attribute.class') - ); - } -} diff --git a/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php b/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php index 5829f4a53..f486c3976 100644 --- a/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php +++ b/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php @@ -63,7 +63,7 @@ public function process(ContainerBuilder $container): void } if (null !== $annotatedPrefixes) { - $container->getDefinition('maker.doctrine_helper')->setArgument(4, $annotatedPrefixes); + $container->getDefinition('maker.doctrine_helper')->setArgument(2, $annotatedPrefixes); } } } diff --git a/src/Doctrine/BaseCollectionRelation.php b/src/Doctrine/BaseCollectionRelation.php index 022dcb06f..9b9e9f4b8 100644 --- a/src/Doctrine/BaseCollectionRelation.php +++ b/src/Doctrine/BaseCollectionRelation.php @@ -18,8 +18,6 @@ */ abstract class BaseCollectionRelation extends BaseRelation { - abstract public function getOrphanRemoval(): bool; - abstract public function getTargetSetterMethodName(): string; public function getAdderMethodName(): string diff --git a/src/Doctrine/BaseRelation.php b/src/Doctrine/BaseRelation.php index b45d8fbd0..5ab9a513f 100644 --- a/src/Doctrine/BaseRelation.php +++ b/src/Doctrine/BaseRelation.php @@ -16,88 +16,51 @@ */ abstract class BaseRelation { - private $propertyName; - private $targetClassName; - private $targetPropertyName; - private $customReturnType; - private $isSelfReferencing = false; - private $mapInverseRelation = true; - private $avoidSetter = false; - - abstract public function isOwning(): bool; + public function __construct( + private string $propertyName, + private string $targetClassName, + private ?string $targetPropertyName = null, + private bool $isSelfReferencing = false, + private bool $mapInverseRelation = true, + private bool $avoidSetter = false, + private bool $isCustomReturnTypeNullable = false, + private ?string $customReturnType = null, + private bool $isOwning = false, + private bool $orphanRemoval = false, + private bool $isNullable = false, + ) { + } public function getPropertyName(): string { return $this->propertyName; } - public function setPropertyName(string $propertyName): self - { - $this->propertyName = $propertyName; - - return $this; - } - public function getTargetClassName(): string { return $this->targetClassName; } - public function setTargetClassName(string $targetClassName): self - { - $this->targetClassName = $targetClassName; - - return $this; - } - public function getTargetPropertyName(): ?string { return $this->targetPropertyName; } - public function setTargetPropertyName(?string $targetPropertyName): self - { - $this->targetPropertyName = $targetPropertyName; - - return $this; - } - public function isSelfReferencing(): bool { return $this->isSelfReferencing; } - public function setIsSelfReferencing(bool $isSelfReferencing): self - { - $this->isSelfReferencing = $isSelfReferencing; - - return $this; - } - public function getMapInverseRelation(): bool { return $this->mapInverseRelation; } - public function setMapInverseRelation(bool $mapInverseRelation): self - { - $this->mapInverseRelation = $mapInverseRelation; - - return $this; - } - public function shouldAvoidSetter(): bool { return $this->avoidSetter; } - public function avoidSetter(bool $avoidSetter = true): self - { - $this->avoidSetter = $avoidSetter; - - return $this; - } - public function getCustomReturnType(): ?string { return $this->customReturnType; @@ -108,11 +71,18 @@ public function isCustomReturnTypeNullable(): bool return $this->isCustomReturnTypeNullable; } - public function setCustomReturnType(string $customReturnType, bool $isNullable) + public function isOwning(): bool { - $this->customReturnType = $customReturnType; - $this->isCustomReturnTypeNullable = $isNullable; + return $this->isOwning; + } - return $this; + public function getOrphanRemoval(): bool + { + return $this->orphanRemoval; + } + + public function isNullable(): bool + { + return $this->isNullable; } } diff --git a/src/Doctrine/BaseSingleRelation.php b/src/Doctrine/BaseSingleRelation.php deleted file mode 100644 index 222b8ac71..000000000 --- a/src/Doctrine/BaseSingleRelation.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\MakerBundle\Doctrine; - -/** - * @internal - */ -abstract class BaseSingleRelation extends BaseRelation -{ - private $isNullable; - - public function isNullable(): bool - { - if ($this->isNullable) { - return $this->isNullable; - } - - return false; - } - - public function setIsNullable(bool $isNullable): self - { - $this->isNullable = $isNullable; - - return $this; - } -} diff --git a/src/Doctrine/DoctrineHelper.php b/src/Doctrine/DoctrineHelper.php index ff54aa3da..551053f79 100644 --- a/src/Doctrine/DoctrineHelper.php +++ b/src/Doctrine/DoctrineHelper.php @@ -13,7 +13,6 @@ use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Mapping\Driver\AnnotationDriver; use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\ORM\Mapping\MappingException as ORMMappingException; use Doctrine\ORM\Mapping\NamingStrategy; @@ -25,7 +24,6 @@ use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Doctrine\Persistence\Mapping\MappingException as PersistenceMappingException; use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; -use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil; /** * @author Fabien Potencier @@ -36,27 +34,12 @@ */ final class DoctrineHelper { - /** - * @var string - */ - private $entityNamespace; - private $phpCompatUtil; - private $registry; - - /** - * @var array|null - */ - private $mappingDriversByPrefix; - - private $attributeMappingSupport; - - public function __construct(string $entityNamespace, PhpCompatUtil $phpCompatUtil, ManagerRegistry $registry = null, bool $attributeMappingSupport = false, array $annotatedPrefixes = null) - { + public function __construct( + private string $entityNamespace, + private ?ManagerRegistry $registry = null, + private ?array $mappingDriversByPrefix = null, + ) { $this->entityNamespace = trim($entityNamespace, '\\'); - $this->phpCompatUtil = $phpCompatUtil; - $this->registry = $registry; - $this->attributeMappingSupport = $attributeMappingSupport; - $this->mappingDriversByPrefix = $annotatedPrefixes; } public function getRegistry(): ManagerRegistry @@ -114,7 +97,7 @@ public function doesClassUseDriver(string $className, string $driverClass): bool } foreach ($metadataDriver->getDrivers() as $namespace => $driver) { - if (0 === strpos($className, $namespace)) { + if (str_starts_with($className, $namespace)) { return $this->isInstanceOf($driver, $driverClass); } } @@ -125,7 +108,7 @@ public function doesClassUseDriver(string $className, string $driverClass): bool $managerName = array_search($em, $this->getRegistry()->getManagers(), true); foreach ($this->mappingDriversByPrefix[$managerName] as [$prefix, $prefixDriver]) { - if (0 === strpos($className, $prefix)) { + if (str_starts_with($className, $prefix)) { return $this->isInstanceOf($prefixDriver, $driverClass); } } @@ -133,11 +116,6 @@ public function doesClassUseDriver(string $className, string $driverClass): bool return false; } - public function isClassAnnotated(string $className): bool - { - return $this->doesClassUseDriver($className, AnnotationDriver::class); - } - public function doesClassUsesAttributes(string $className): bool { return $this->doesClassUseDriver($className, AttributeDriver::class); @@ -145,7 +123,7 @@ public function doesClassUsesAttributes(string $className): bool public function isDoctrineSupportingAttributes(): bool { - return $this->isDoctrineInstalled() && $this->attributeMappingSupport; + return $this->isDoctrineInstalled(); } public function getEntitiesForAutocomplete(): array @@ -168,18 +146,14 @@ public function getEntitiesForAutocomplete(): array public function getMetadata(string $classOrNamespace = null, bool $disconnected = false): array|ClassMetadata { - // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes + // Invalidating the cached AttributeDriver::$classNames to find new Entity classes foreach ($this->mappingDriversByPrefix ?? [] as $managerName => $prefixes) { - foreach ($prefixes as [$prefix, $annotationDriver]) { - if (null !== $annotationDriver) { - if ($annotationDriver instanceof AnnotationDriver) { - $classNames = (new \ReflectionClass(AnnotationDriver::class))->getProperty('classNames'); - } else { - $classNames = (new \ReflectionClass(AttributeDriver::class))->getProperty('classNames'); - } + foreach ($prefixes as [$prefix, $attributeDriver]) { + if ($attributeDriver instanceof AttributeDriver) { + $classNames = (new \ReflectionClass(AttributeDriver::class))->getProperty('classNames'); $classNames->setAccessible(true); - $classNames->setValue($annotationDriver, null); + $classNames->setValue($attributeDriver, null); } } } @@ -205,14 +179,11 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected } if (null === $this->mappingDriversByPrefix) { - // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes + // Invalidating the cached AttributeDriver::$classNames to find new Entity classes $metadataDriver = $em->getConfiguration()->getMetadataDriverImpl(); + if ($this->isInstanceOf($metadataDriver, MappingDriverChain::class)) { foreach ($metadataDriver->getDrivers() as $driver) { - if ($this->isInstanceOf($driver, AnnotationDriver::class)) { - $classNames->setValue($driver, null); - } - if ($this->isInstanceOf($driver, AttributeDriver::class)) { $classNames->setValue($driver, null); } @@ -229,7 +200,7 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected return $m; } - if (0 === strpos($m->getName(), $classOrNamespace)) { + if (str_starts_with($m->getName(), $classOrNamespace)) { $metadata[$m->getName()] = $m; } } diff --git a/src/Doctrine/EntityClassGenerator.php b/src/Doctrine/EntityClassGenerator.php index 09821e267..e576c499d 100644 --- a/src/Doctrine/EntityClassGenerator.php +++ b/src/Doctrine/EntityClassGenerator.php @@ -70,7 +70,6 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $ 'broadcast' => $broadcast, 'should_escape_table_name' => $this->doctrineHelper->isKeyword($tableName), 'table_name' => $tableName, - 'doctrine_use_attributes' => $this->doctrineHelper->isDoctrineSupportingAttributes() && $this->doctrineHelper->doesClassUsesAttributes($entityClassDetails->getFullName()), ] ); diff --git a/src/Doctrine/EntityRegenerator.php b/src/Doctrine/EntityRegenerator.php index eec54cc7e..e8f5e88b4 100644 --- a/src/Doctrine/EntityRegenerator.php +++ b/src/Doctrine/EntityRegenerator.php @@ -122,49 +122,50 @@ public function regenerateEntities(string $classOrNamespace): void switch ($mapping['type']) { case ClassMetadata::MANY_TO_ONE: - $relation = (new RelationManyToOne()) - ->setPropertyName($mapping['fieldName']) - ->setIsNullable($getIsNullable($mapping)) - ->setTargetClassName($mapping['targetEntity']) - ->setTargetPropertyName($mapping['inversedBy']) - ->setMapInverseRelation(null !== $mapping['inversedBy']) - ; + $relation = (new RelationManyToOne( + propertyName: $mapping['fieldName'], + targetClassName: $mapping['targetEntity'], + targetPropertyName: $mapping['inversedBy'], + mapInverseRelation: null !== $mapping['inversedBy'], + isOwning: true, + isNullable: $getIsNullable($mapping), + )); $manipulator->addManyToOneRelation($relation); break; case ClassMetadata::ONE_TO_MANY: - $relation = (new RelationOneToMany()) - ->setPropertyName($mapping['fieldName']) - ->setTargetClassName($mapping['targetEntity']) - ->setTargetPropertyName($mapping['mappedBy']) - ->setOrphanRemoval($mapping['orphanRemoval']) - ; + $relation = (new RelationOneToMany( + propertyName: $mapping['fieldName'], + targetClassName: $mapping['targetEntity'], + targetPropertyName: $mapping['mappedBy'], + orphanRemoval: $mapping['orphanRemoval'], + )); $manipulator->addOneToManyRelation($relation); break; case ClassMetadata::MANY_TO_MANY: - $relation = (new RelationManyToMany()) - ->setPropertyName($mapping['fieldName']) - ->setTargetClassName($mapping['targetEntity']) - ->setTargetPropertyName($mapping['mappedBy']) - ->setIsOwning($mapping['isOwningSide']) - ->setMapInverseRelation($mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true) - ; + $relation = (new RelationManyToMany( + propertyName: $mapping['fieldName'], + targetClassName: $mapping['targetEntity'], + targetPropertyName: $mapping['mappedBy'], + mapInverseRelation: $mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true, + isOwning: $mapping['isOwningSide'], + )); $manipulator->addManyToManyRelation($relation); break; case ClassMetadata::ONE_TO_ONE: - $relation = (new RelationOneToOne()) - ->setPropertyName($mapping['fieldName']) - ->setTargetClassName($mapping['targetEntity']) - ->setTargetPropertyName($mapping['isOwningSide'] ? $mapping['inversedBy'] : $mapping['mappedBy']) - ->setIsOwning($mapping['isOwningSide']) - ->setMapInverseRelation($mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true) - ->setIsNullable($getIsNullable($mapping)) - ; + $relation = (new RelationOneToOne( + propertyName: $mapping['fieldName'], + targetClassName: $mapping['targetEntity'], + targetPropertyName: $mapping['isOwningSide'] ? $mapping['inversedBy'] : $mapping['mappedBy'], + mapInverseRelation: $mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true, + isOwning: $mapping['isOwningSide'], + isNullable: $getIsNullable($mapping), + )); $manipulator->addOneToOneRelation($relation); diff --git a/src/Doctrine/EntityRelation.php b/src/Doctrine/EntityRelation.php index e5ef19ebb..db41f8052 100644 --- a/src/Doctrine/EntityRelation.php +++ b/src/Doctrine/EntityRelation.php @@ -81,29 +81,32 @@ public static function getValidRelationTypes(): array public function getOwningRelation(): RelationManyToMany|RelationOneToOne|RelationManyToOne { return match ($this->getType()) { - self::MANY_TO_ONE => (new RelationManyToOne()) - ->setPropertyName($this->owningProperty) - ->setTargetClassName($this->inverseClass) - ->setTargetPropertyName($this->inverseProperty) - ->setIsNullable($this->isNullable) - ->setIsSelfReferencing($this->isSelfReferencing) - ->setMapInverseRelation($this->mapInverseRelation), - self::MANY_TO_MANY => (new RelationManyToMany()) - ->setPropertyName($this->owningProperty) - ->setTargetClassName($this->inverseClass) - ->setTargetPropertyName($this->inverseProperty) - ->setIsOwning(true)->setMapInverseRelation( - $this->mapInverseRelation - ) - ->setIsSelfReferencing($this->isSelfReferencing), - self::ONE_TO_ONE => (new RelationOneToOne()) - ->setPropertyName($this->owningProperty) - ->setTargetClassName($this->inverseClass) - ->setTargetPropertyName($this->inverseProperty) - ->setIsNullable($this->isNullable) - ->setIsOwning(true) - ->setIsSelfReferencing($this->isSelfReferencing) - ->setMapInverseRelation($this->mapInverseRelation), + self::MANY_TO_ONE => (new RelationManyToOne( + propertyName: $this->owningProperty, + targetClassName: $this->inverseClass, + targetPropertyName: $this->inverseProperty, + isSelfReferencing: $this->isSelfReferencing, + mapInverseRelation: $this->mapInverseRelation, + isOwning: true, + isNullable: $this->isNullable, + )), + self::MANY_TO_MANY => (new RelationManyToMany( + propertyName: $this->owningProperty, + targetClassName: $this->inverseClass, + targetPropertyName: $this->inverseProperty, + isSelfReferencing: $this->isSelfReferencing, + mapInverseRelation: $this->mapInverseRelation, + isOwning: true, + )), + self::ONE_TO_ONE => (new RelationOneToOne( + propertyName: $this->owningProperty, + targetClassName: $this->inverseClass, + targetPropertyName: $this->inverseProperty, + isSelfReferencing: $this->isSelfReferencing, + mapInverseRelation: $this->mapInverseRelation, + isOwning: true, + isNullable: $this->isNullable, + )), default => throw new \InvalidArgumentException('Invalid type'), }; } @@ -111,25 +114,26 @@ public function getOwningRelation(): RelationManyToMany|RelationOneToOne|Relatio public function getInverseRelation(): RelationManyToMany|RelationOneToOne|RelationOneToMany { return match ($this->getType()) { - self::MANY_TO_ONE => (new RelationOneToMany()) - ->setPropertyName($this->inverseProperty) - ->setTargetClassName($this->owningClass) - ->setTargetPropertyName($this->owningProperty) - ->setOrphanRemoval($this->orphanRemoval) - ->setIsSelfReferencing($this->isSelfReferencing), - self::MANY_TO_MANY => (new RelationManyToMany()) - ->setPropertyName($this->inverseProperty) - ->setTargetClassName($this->owningClass) - ->setTargetPropertyName($this->owningProperty) - ->setIsOwning(false) - ->setIsSelfReferencing($this->isSelfReferencing), - self::ONE_TO_ONE => (new RelationOneToOne()) - ->setPropertyName($this->inverseProperty) - ->setTargetClassName($this->owningClass) - ->setTargetPropertyName($this->owningProperty) - ->setIsNullable($this->isNullable) - ->setIsOwning(false) - ->setIsSelfReferencing($this->isSelfReferencing), + self::MANY_TO_ONE => (new RelationOneToMany( + propertyName: $this->inverseProperty, + targetClassName: $this->owningClass, + targetPropertyName: $this->owningProperty, + isSelfReferencing: $this->isSelfReferencing, + orphanRemoval: $this->orphanRemoval, + )), + self::MANY_TO_MANY => (new RelationManyToMany( + propertyName: $this->inverseProperty, + targetClassName: $this->owningClass, + targetPropertyName: $this->owningProperty, + isSelfReferencing: $this->isSelfReferencing + )), + self::ONE_TO_ONE => (new RelationOneToOne( + propertyName: $this->inverseProperty, + targetClassName: $this->owningClass, + targetPropertyName: $this->owningProperty, + isSelfReferencing: $this->isSelfReferencing, + isNullable: $this->isNullable, + )), default => throw new \InvalidArgumentException('Invalid type'), }; } diff --git a/src/Doctrine/RelationManyToMany.php b/src/Doctrine/RelationManyToMany.php index be19a8ba7..eaf0599d1 100644 --- a/src/Doctrine/RelationManyToMany.php +++ b/src/Doctrine/RelationManyToMany.php @@ -18,25 +18,6 @@ */ final class RelationManyToMany extends BaseCollectionRelation { - private $isOwning; - - public function isOwning(): bool - { - return $this->isOwning; - } - - public function setIsOwning($isOwning): self - { - $this->isOwning = $isOwning; - - return $this; - } - - public function getOrphanRemoval(): bool - { - return false; - } - public function getTargetSetterMethodName(): string { return 'add'.Str::asCamelCase(Str::pluralCamelCaseToSingular($this->getTargetPropertyName())); diff --git a/src/Doctrine/RelationManyToOne.php b/src/Doctrine/RelationManyToOne.php index 0cd0a523d..97c26c619 100644 --- a/src/Doctrine/RelationManyToOne.php +++ b/src/Doctrine/RelationManyToOne.php @@ -14,10 +14,6 @@ /** * @internal */ -final class RelationManyToOne extends BaseSingleRelation +final class RelationManyToOne extends BaseRelation { - public function isOwning(): bool - { - return true; - } } diff --git a/src/Doctrine/RelationOneToMany.php b/src/Doctrine/RelationOneToMany.php index 865f47ad8..1fa43dd30 100644 --- a/src/Doctrine/RelationOneToMany.php +++ b/src/Doctrine/RelationOneToMany.php @@ -18,20 +18,6 @@ */ final class RelationOneToMany extends BaseCollectionRelation { - private $orphanRemoval; - - public function getOrphanRemoval(): bool - { - return $this->orphanRemoval; - } - - public function setOrphanRemoval($orphanRemoval): self - { - $this->orphanRemoval = $orphanRemoval; - - return $this; - } - public function getTargetGetterMethodName(): string { return 'get'.Str::asCamelCase($this->getTargetPropertyName()); @@ -42,11 +28,6 @@ public function getTargetSetterMethodName(): string return 'set'.Str::asCamelCase($this->getTargetPropertyName()); } - public function isOwning(): bool - { - return false; - } - public function isMapInverseRelation(): bool { throw new \Exception('OneToMany IS the inverse side!'); diff --git a/src/Doctrine/RelationOneToOne.php b/src/Doctrine/RelationOneToOne.php index 785fcd11a..8b970a9bd 100644 --- a/src/Doctrine/RelationOneToOne.php +++ b/src/Doctrine/RelationOneToOne.php @@ -16,22 +16,8 @@ /** * @internal */ -final class RelationOneToOne extends BaseSingleRelation +final class RelationOneToOne extends BaseRelation { - private $isOwning; - - public function isOwning(): bool - { - return $this->isOwning; - } - - public function setIsOwning($isOwning): self - { - $this->isOwning = $isOwning; - - return $this; - } - public function getTargetGetterMethodName(): string { return 'get'.Str::asCamelCase($this->getTargetPropertyName()); diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index 585d5967d..63116832d 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -173,10 +173,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen 'Entity\\' ); - if (!$this->doctrineHelper->isDoctrineSupportingAttributes() && $this->doctrineHelper->doesClassUsesAttributes($entityClassDetails->getFullName())) { - throw new RuntimeCommandException('To use Doctrine entity attributes you\'ll need PHP 8, doctrine/orm 2.9, doctrine/doctrine-bundle 2.4 and symfony/framework-bundle 5.2.'); - } - $classExists = class_exists($entityClassDetails->getFullName()); if (!$classExists) { $broadcast = $input->getOption('broadcast'); @@ -203,11 +199,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $generator->writeChanges(); } - if ( - !$this->doesEntityUseAnnotationMapping($entityClassDetails->getFullName()) - && !$this->doesEntityUseAttributeMapping($entityClassDetails->getFullName()) - ) { - throw new RuntimeCommandException(sprintf('Only annotation or attribute mapping is supported by make:entity, but the %s class uses a different format. If you would like this command to generate the properties & getter/setter methods, add your mapping configuration, and then re-run this command with the --regenerate flag.', $entityClassDetails->getFullName())); + if (!$this->doesEntityUseAttributeMapping($entityClassDetails->getFullName())) { + throw new RuntimeCommandException(sprintf('Only attribute mapping is supported by make:entity, but the %s class uses a different format. If you would like this command to generate the properties & getter/setter methods, add your mapping configuration, and then re-run this command with the --regenerate flag.', $entityClassDetails->getFullName())); } if ($classExists) { @@ -224,7 +217,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen } $currentFields = $this->getPropertyNames($entityClassDetails->getFullName()); - $manipulator = $this->createClassManipulator($entityPath, $io, $overwrite, $entityClassDetails->getFullName()); + $manipulator = $this->createClassManipulator($entityPath, $io, $overwrite); $isFirstField = true; while (true) { @@ -252,7 +245,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $otherManipulator = $manipulator; } else { $otherManipulatorFilename = $this->getPathOfClass($newField->getInverseClass()); - $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite, $entityClassDetails->getFullName()); + $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite); } switch ($newField->getType()) { case EntityRelation::MANY_TO_ONE: @@ -267,7 +260,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen // the new field being added to THIS entity is the inverse $newFieldName = $newField->getInverseProperty(); $otherManipulatorFilename = $this->getPathOfClass($newField->getOwningClass()); - $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite, $entityClassDetails->getFullName()); + $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite); // The *other* class will receive the ManyToOne $otherManipulator->addManyToOneRelation($newField->getOwningRelation()); @@ -340,7 +333,7 @@ public function configureDependencies(DependencyBuilder $dependencies, InputInte ORMDependencyBuilder::buildDependencies($dependencies); } - private function askForNextField(ConsoleStyle $io, array $fields, string $entityClass, bool $isFirstField) + private function askForNextField(ConsoleStyle $io, array $fields, string $entityClass, bool $isFirstField): EntityRelation|array|null { $io->writeln(''); @@ -478,7 +471,7 @@ private function printAvailableTypes(ConsoleStyle $io): void ], ]; - $printSection = function (array $sectionTypes) use ($io, &$allTypes) { + $printSection = static function (array $sectionTypes) use ($io, &$allTypes) { foreach ($sectionTypes as $mainType => $subTypes) { unset($allTypes[$mainType]); $line = sprintf(' * %s', $mainType); @@ -530,7 +523,7 @@ private function createEntityClassQuestion(string $questionText): Question return $question; } - private function askRelationDetails(ConsoleStyle $io, string $generatedEntityClass, string $type, string $newFieldName) + private function askRelationDetails(ConsoleStyle $io, string $generatedEntityClass, string $type, string $newFieldName): EntityRelation { // ask the targetEntity $targetEntityClass = null; @@ -576,7 +569,7 @@ function ($name) use ($targetClass) { ); }; - $askIsNullable = function (string $propertyName, string $targetClass) use ($io) { + $askIsNullable = static function (string $propertyName, string $targetClass) use ($io) { return $io->confirm(sprintf( 'Is the %s.%s property allowed to be null (nullable)?', Str::getShortClassName($targetClass), @@ -584,7 +577,7 @@ function ($name) use ($targetClass) { )); }; - $askOrphanRemoval = function (string $owningClass, string $inverseClass) use ($io) { + $askOrphanRemoval = static function (string $owningClass, string $inverseClass) use ($io) { $io->text([ 'Do you want to activate orphanRemoval on your relationship?', sprintf( @@ -809,12 +802,9 @@ private function askRelationType(ConsoleStyle $io, string $entityClass, string $ return $io->askQuestion($question); } - private function createClassManipulator(string $path, ConsoleStyle $io, bool $overwrite, string $className): ClassSourceManipulator + private function createClassManipulator(string $path, ConsoleStyle $io, bool $overwrite): ClassSourceManipulator { - $useAttributes = $this->doctrineHelper->doesClassUsesAttributes($className) && $this->doctrineHelper->isDoctrineSupportingAttributes(); - $useAnnotations = $this->doctrineHelper->isClassAnnotated($className) || !$useAttributes; - - $manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite, $useAnnotations, true, $useAttributes); + $manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite, false, true, true); $manipulator->setIo($io); @@ -847,28 +837,11 @@ private function getPropertyNames(string $class): array $reflClass = new \ReflectionClass($class); - return array_map(function (\ReflectionProperty $prop) { + return array_map(static function (\ReflectionProperty $prop) { return $prop->getName(); }, $reflClass->getProperties()); } - /** @legacy Drop when Annotations are no longer supported */ - private function doesEntityUseAnnotationMapping(string $className): bool - { - if (!class_exists($className)) { - $otherClassMetadatas = $this->doctrineHelper->getMetadata(Str::getNamespace($className).'\\', true); - - // if we have no metadata, we should assume this is the first class being mapped - if (empty($otherClassMetadatas)) { - return false; - } - - $className = reset($otherClassMetadatas)->getName(); - } - - return $this->doctrineHelper->isClassAnnotated($className); - } - /** @legacy Drop when Annotations are no longer supported */ private function doesEntityUseAttributeMapping(string $className): bool { diff --git a/src/Maker/MakeRegistrationForm.php b/src/Maker/MakeRegistrationForm.php index 539f9a75e..7d1e8c2b6 100644 --- a/src/Maker/MakeRegistrationForm.php +++ b/src/Maker/MakeRegistrationForm.php @@ -399,9 +399,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $userManipulator = new ClassSourceManipulator( file_get_contents($classDetails->getPath()), false, - $this->doctrineHelper->isClassAnnotated($this->userClass), + false, true, - $this->doctrineHelper->doesClassUsesAttributes($this->userClass) + true ); $userManipulator->setIo($io); diff --git a/src/Maker/MakeResetPassword.php b/src/Maker/MakeResetPassword.php index 265bba2e0..9363d9ff8 100644 --- a/src/Maker/MakeResetPassword.php +++ b/src/Maker/MakeResetPassword.php @@ -427,13 +427,15 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r CODE ); - $manipulator->addManyToOneRelation((new RelationManyToOne()) - ->setPropertyName('user') - ->setTargetClassName($this->userClass) - ->setMapInverseRelation(false) - ->setCustomReturnType('object', false) - ->avoidSetter() - ); + $manipulator->addManyToOneRelation((new RelationManyToOne( + propertyName: 'user', + targetClassName: $this->userClass, + mapInverseRelation: false, + avoidSetter: true, + isCustomReturnTypeNullable: false, + customReturnType: 'object', + isOwning: true, + ))); $this->fileManager->dumpFile($requestEntityPath, $manipulator->getSourceCode()); diff --git a/src/MakerBundle.php b/src/MakerBundle.php index 7d946b111..427e2103c 100644 --- a/src/MakerBundle.php +++ b/src/MakerBundle.php @@ -11,7 +11,6 @@ namespace Symfony\Bundle\MakerBundle; -use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\DoctrineAttributesCheckPass; use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\MakeCommandRegistrationPass; use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\RemoveMissingParametersPass; use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\SetDoctrineAnnotatedPrefixesPass; @@ -25,10 +24,9 @@ */ class MakerBundle extends Bundle { - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { // add a priority so we run before the core command pass - $container->addCompilerPass(new DoctrineAttributesCheckPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11); $container->addCompilerPass(new MakeCommandRegistrationPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10); $container->addCompilerPass(new RemoveMissingParametersPass()); $container->addCompilerPass(new SetDoctrineAnnotatedPrefixesPass()); diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index ed3438e8e..5728e4aa1 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -37,9 +37,7 @@ - - %maker.compatible_check.doctrine.supports_attributes% diff --git a/src/Resources/skeleton/doctrine/Entity.tpl.php b/src/Resources/skeleton/doctrine/Entity.tpl.php index 0209f2eac..f6a98fcdb 100644 --- a/src/Resources/skeleton/doctrine/Entity.tpl.php +++ b/src/Resources/skeleton/doctrine/Entity.tpl.php @@ -4,18 +4,9 @@ - -/** - * @ORM\Entity(repositoryClass=::class) - * @ORM\Table(name="``") - - */ - - #[ORM\Entity(repositoryClass: ::class)] #[ORM\Table(name: '``')] - #[ApiResource] @@ -24,15 +15,10 @@ class { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ - #[ORM\Id] + #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] - private $id; + private int $id; public function getId(): ?int { diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 8bb2b545b..c6ec9a335 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -579,10 +579,10 @@ private function addSingularRelation(BaseRelation $relation): void $this->addGetter( $relation->getPropertyName(), - $relation->getCustomReturnType() ?: $typeHint, + $relation->getCustomReturnType() ?? $typeHint, // getter methods always have nullable return values // unless this has been customized explicitly - $relation->getCustomReturnType() ? $relation->isCustomReturnTypeNullable() : true + !$relation->getCustomReturnType() || $relation->isCustomReturnTypeNullable() ); if ($relation->shouldAvoidSetter()) { diff --git a/tests/Doctrine/EntityRegeneratorTest.php b/tests/Doctrine/EntityRegeneratorTest.php index 275862ed9..55cee6a24 100644 --- a/tests/Doctrine/EntityRegeneratorTest.php +++ b/tests/Doctrine/EntityRegeneratorTest.php @@ -31,9 +31,6 @@ use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Routing\RouteCollectionBuilder; -/** - * @requires PHP 7.1 - */ class EntityRegeneratorTest extends TestCase { /** @@ -101,7 +98,7 @@ private function doTestRegeneration(string $sourceDir, Kernel $kernel, string $n }); $fileManager = new FileManager($fs, $autoloaderUtil, new MakerFileLinkFormatter(null), $tmpDir); - $doctrineHelper = new DoctrineHelper('App\\Entity', new PhpCompatUtil($fileManager), $container->get('doctrine')); + $doctrineHelper = new DoctrineHelper('App\\Entity', $container->get('doctrine')); $phpCompatUtil = new PhpCompatUtil($fileManager); $generator = new Generator($fileManager, 'App\\', $phpCompatUtil); $entityClassGenerator = new EntityClassGenerator($generator, $doctrineHelper); @@ -179,7 +176,6 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load 'mappings' => [ 'EntityRegenerator' => [ 'is_bundle' => false, - 'type' => 'annotation', 'dir' => '%kernel.project_dir%/src/Entity', 'prefix' => 'Symfony\Bundle\MakerBundle\Tests\tmp\current_project\src\Entity', 'alias' => 'EntityRegeneratorApp', diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/BaseClient.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/BaseClient.php index 6a1b09208..6777d117f 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/BaseClient.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/BaseClient.php @@ -4,33 +4,23 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\MappedSuperclass() - */ +#[ORM\MappedSuperclass] class BaseClient { use TeamTrait; - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string") - */ + #[ORM\Column(type: 'string')] private $name; - /** - * @ORM\ManyToOne(targetEntity=User::class) - */ + #[ORM\ManyToOne(targetEntity: User::class)] private $creator; - /** - * @ORM\Column(type="integer") - */ + #[ORM\Column(type: 'integer')] private $magic; public function __construct() diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php index d2b31908a..c4fbdca36 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php @@ -6,27 +6,21 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Client extends BaseClient { use TimestampableTrait; /** - * @ORM\Column(type="string") * @var string */ + #[ORM\Column(type: 'string')] private $apiKey; - /** - * @ORM\ManyToMany(targetEntity=Tag::class) - */ + #[ORM\ManyToMany(targetEntity: Tag::class)] private $tags; - /** - * @ORM\Embedded(class=Embed::class) - */ + #[ORM\Embedded(class: Embed::class)] private $embed; public function __construct() diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Embed.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Embed.php index ea4aa8043..754a2ad65 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Embed.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Embed.php @@ -4,14 +4,10 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Embeddable() - */ +#[ORM\Embeddable] class Embed { - /** - * @ORM\Column(type="integer") - */ + #[ORM\Column(type: 'integer')] private $val; public function getVal(): ?int diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Tag.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Tag.php index 63552be48..d7a6da390 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Tag.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Tag.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Tag { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId(): ?int diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php index fb6f61ba3..c957851a6 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php @@ -6,31 +6,21 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToMany(targetEntity=UserAvatar::class, mappedBy="user") - */ + #[ORM\OneToMany(targetEntity: UserAvatar::class, mappedBy: 'user')] private $avatars; - /** - * @ORM\OneToOne(targetEntity=UserProfile::class, mappedBy="user") - */ + #[ORM\OneToOne(targetEntity: UserProfile::class, mappedBy: 'user')] private $userProfile; - /** - * @ORM\ManyToMany(targetEntity=Tag::class) - */ + #[ORM\ManyToMany(targetEntity: Tag::class)] private $tags; public function __construct() diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserAvatar.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserAvatar.php index 237b46ab3..77dc05a34 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserAvatar.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserAvatar.php @@ -4,22 +4,16 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserAvatar { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=User::class, inversedBy="avatars", cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'avatars', cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $user; public function getId(): ?int diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserProfile.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserProfile.php index 97be4c254..b07a84f71 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserProfile.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/UserProfile.php @@ -4,22 +4,16 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserProfile { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=User::class, inversedBy="userProfile", cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\OneToOne(targetEntity: User::class, inversedBy: 'userProfile', cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $user; public function getId(): ?int diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/BaseClient.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/BaseClient.php index b81cc38dd..029ed8481 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/BaseClient.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/BaseClient.php @@ -4,33 +4,23 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\MappedSuperclass() - */ +#[ORM\MappedSuperclass] class BaseClient { use TeamTrait; - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string") - */ + #[ORM\Column(type: 'string')] private $name; - /** - * @ORM\ManyToOne(targetEntity=User::class) - */ + #[ORM\ManyToOne(targetEntity: User::class)] private $creator; - /** - * @ORM\Column(type="integer") - */ + #[ORM\Column(type: 'integer')] private $magic; public function __construct() diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php index d2b31908a..c4fbdca36 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php @@ -6,27 +6,21 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Client extends BaseClient { use TimestampableTrait; /** - * @ORM\Column(type="string") * @var string */ + #[ORM\Column(type: 'string')] private $apiKey; - /** - * @ORM\ManyToMany(targetEntity=Tag::class) - */ + #[ORM\ManyToMany(targetEntity: Tag::class)] private $tags; - /** - * @ORM\Embedded(class=Embed::class) - */ + #[ORM\Embedded(class: Embed::class)] private $embed; public function __construct() diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Embed.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Embed.php index ea4aa8043..754a2ad65 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Embed.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Embed.php @@ -4,14 +4,10 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Embeddable() - */ +#[ORM\Embeddable] class Embed { - /** - * @ORM\Column(type="integer") - */ + #[ORM\Column(type: 'integer')] private $val; public function getVal(): ?int diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Tag.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Tag.php index 63552be48..d7a6da390 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Tag.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Tag.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Tag { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId(): ?int diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php index a199df132..fb5483f86 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php @@ -6,31 +6,21 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToMany(targetEntity=UserAvatar::class, mappedBy="user") - */ + #[ORM\OneToMany(targetEntity: UserAvatar::class, mappedBy: 'user')] private $avatars; - /** - * @ORM\OneToOne(targetEntity=UserProfile::class, mappedBy="user") - */ + #[ORM\OneToOne(targetEntity: UserProfile::class, mappedBy: 'user')] private $userProfile; - /** - * @ORM\ManyToMany(targetEntity=Tag::class) - */ + #[ORM\ManyToMany(targetEntity: Tag::class)] private $tags; public function __construct() diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserAvatar.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserAvatar.php index 237b46ab3..77dc05a34 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserAvatar.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserAvatar.php @@ -4,22 +4,16 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserAvatar { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=User::class, inversedBy="avatars", cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'avatars', cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $user; public function getId(): ?int diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserProfile.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserProfile.php index 97be4c254..b07a84f71 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserProfile.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/UserProfile.php @@ -4,22 +4,16 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserProfile { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=User::class, inversedBy="userProfile", cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\OneToOne(targetEntity: User::class, inversedBy: 'userProfile', cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $user; public function getId(): ?int diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/BaseClient.php b/tests/Doctrine/fixtures/source_project/src/Entity/BaseClient.php index 4d769892f..122fd11e1 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/BaseClient.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/BaseClient.php @@ -4,33 +4,23 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\MappedSuperclass() - */ +#[ORM\MappedSuperclass] class BaseClient { use TeamTrait; - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string") - */ + #[ORM\Column(type: 'string')] private $name; - /** - * @ORM\ManyToOne(targetEntity=User::class) - */ + #[ORM\ManyToOne(targetEntity: User::class)] private $creator; - /** - * @ORM\Column(type="integer") - */ + #[ORM\Column(type: 'integer')] private $magic; public function __construct() diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/Client.php b/tests/Doctrine/fixtures/source_project/src/Entity/Client.php index 146bcb346..5d1fe1cc4 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/Client.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/Client.php @@ -4,26 +4,20 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Client extends BaseClient { use TimestampableTrait; /** - * @ORM\Column(type="string") * @var string */ + #[ORM\Column(type: 'string')] private $apiKey; - /** - * @ORM\ManyToMany(targetEntity=Tag::class) - */ + #[ORM\ManyToMany(targetEntity: Tag::class)] private $tags; - /** - * @ORM\Embedded(class=Embed::class) - */ + #[ORM\Embedded(class: Embed::class)] private $embed; } diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/Embed.php b/tests/Doctrine/fixtures/source_project/src/Entity/Embed.php index 8d38dd9ef..0b0ab86df 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/Embed.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/Embed.php @@ -4,13 +4,9 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Embeddable() - */ +#[ORM\Embeddable] class Embed { - /** - * @ORM\Column(type="integer") - */ + #[ORM\Column(type: 'integer')] private $val; } diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/Tag.php b/tests/Doctrine/fixtures/source_project/src/Entity/Tag.php index 63552be48..d7a6da390 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/Tag.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/Tag.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Tag { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId(): ?int diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/TeamTrait.php b/tests/Doctrine/fixtures/source_project/src/Entity/TeamTrait.php index f2965fa96..0de807680 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/TeamTrait.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/TeamTrait.php @@ -6,8 +6,6 @@ trait TeamTrait { - /** - * @ORM\ManyToMany(targetEntity=User::class) - */ + #[ORM\ManyToMany(targetEntity: User::class)] private $members; } diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/TimestampableTrait.php b/tests/Doctrine/fixtures/source_project/src/Entity/TimestampableTrait.php index 265d43001..1a835931a 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/TimestampableTrait.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/TimestampableTrait.php @@ -6,14 +6,10 @@ trait TimestampableTrait { - /** - * @ORM\Column(type="datetime") - */ + #[ORM\Column(type: 'datetime')] private $createdAt; - /** - * @ORM\Column(type="datetime") - */ + #[ORM\Column(type: 'datetime')] private $updatedAt; /** diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/User.php b/tests/Doctrine/fixtures/source_project/src/Entity/User.php index 552acf9c1..2b79247c0 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/User.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/User.php @@ -5,31 +5,21 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToMany(targetEntity=UserAvatar::class, mappedBy="user") - */ + #[ORM\OneToMany(targetEntity: UserAvatar::class, mappedBy: 'user')] private $avatars; - /** - * @ORM\OneToOne(targetEntity=UserProfile::class, mappedBy="user") - */ + #[ORM\OneToOne(targetEntity: UserProfile::class, mappedBy: 'user')] private $userProfile; - /** - * @ORM\ManyToMany(targetEntity=Tag::class) - */ + #[ORM\ManyToMany(targetEntity: Tag::class)] private $tags; public function __construct() diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/UserAvatar.php b/tests/Doctrine/fixtures/source_project/src/Entity/UserAvatar.php index 26a5884d5..c26a2d24b 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/UserAvatar.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/UserAvatar.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserAvatar { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=User::class, inversedBy="avatars", cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'avatars', cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $user; } diff --git a/tests/Doctrine/fixtures/source_project/src/Entity/UserProfile.php b/tests/Doctrine/fixtures/source_project/src/Entity/UserProfile.php index 10045ea0a..732618b77 100644 --- a/tests/Doctrine/fixtures/source_project/src/Entity/UserProfile.php +++ b/tests/Doctrine/fixtures/source_project/src/Entity/UserProfile.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserProfile { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=User::class, inversedBy="userProfile", cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\OneToOne(targetEntity: User::class, inversedBy: 'userProfile', cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $user; } diff --git a/tests/Maker/MakeCrudTest.php b/tests/Maker/MakeCrudTest.php index 9e6081740..45beafb7d 100644 --- a/tests/Maker/MakeCrudTest.php +++ b/tests/Maker/MakeCrudTest.php @@ -18,8 +18,6 @@ class MakeCrudTest extends MakerTestCase { - use TestHelpersTrait; - protected function getMakerClass(): string { return MakeCrud::class; diff --git a/tests/Maker/MakeEntityTest.php b/tests/Maker/MakeEntityTest.php index 9ab814f5f..6bd7ad943 100644 --- a/tests/Maker/MakeEntityTest.php +++ b/tests/Maker/MakeEntityTest.php @@ -20,8 +20,6 @@ class MakeEntityTest extends MakerTestCase { - use TestHelpersTrait; - protected function getMakerClass(): string { return MakeEntity::class; @@ -31,19 +29,6 @@ private function createMakeEntityTest(bool $withDatabase = true): MakerTestDetai { return $this->createMakerTest() ->preRun(function (MakerTestRunner $runner) use ($withDatabase) { - $config = $runner->readYaml('config/packages/doctrine.yaml'); - - /* @legacy Refactor when annotations are no longer supported. */ - if (isset($config['doctrine']['orm']['mappings']['App']) && !$this->useAttributes($runner)) { - // Attributes are only supported w/ PHP 8, FrameworkBundle >=5.2, - // ORM >=2.9, & DoctrineBundle >=2.4 - $runner->modifyYamlFile('config/packages/doctrine.yaml', function (array $data) { - $data['doctrine']['orm']['mappings']['App']['type'] = 'annotation'; - - return $data; - }); - } - if ($withDatabase) { $runner->configureDatabase(); } @@ -504,7 +489,7 @@ public function getTestDetails(): \Generator '', ], '', true /* allow failure */); - $this->assertStringContainsString('Only annotation or attribute mapping is supported', $output); + $this->assertStringContainsString('Only attribute mapping is supported', $output); }), ]; @@ -524,7 +509,7 @@ public function getTestDetails(): \Generator '', ], '', true /* allow failure */); - $this->assertStringContainsString('Only annotation or attribute mapping is supported', $output); + $this->assertStringContainsString('Only attribute mapping is supported', $output); }), ]; diff --git a/tests/Maker/TestHelpersTrait.php b/tests/Maker/TestHelpersTrait.php deleted file mode 100644 index eac266d03..000000000 --- a/tests/Maker/TestHelpersTrait.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\MakerBundle\Tests\Maker; - -use Doctrine\ORM\Mapping\Driver\AttributeReader; -use Symfony\Bundle\MakerBundle\Test\MakerTestRunner; - -/** - * @author Jesse Rushlow - * - * @internal - */ -trait TestHelpersTrait -{ - // @legacy - remove when annotations are no longer supported - protected function useAttributes(MakerTestRunner $runner): bool - { - return \PHP_VERSION_ID >= 80000 - && $runner->doesClassExist(AttributeReader::class); - } -} diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index bad1b45bc..3d5ffe15e 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -377,62 +377,73 @@ public function getAddManyToOneRelationTests(): \Generator yield 'many_to_one_not_nullable' => [ 'User_simple.php', 'User_simple_not_nullable.php', - (new RelationManyToOne()) - ->setPropertyName('category') - ->setTargetClassName('App\Entity\Category') - ->setTargetPropertyName('foods') - ->setIsNullable(false), + (new RelationManyToOne( + propertyName: 'category', + targetClassName: \App\Entity\Category::class, + targetPropertyName: 'foods', + isOwning: true, + )), ]; yield 'many_to_one_nullable' => [ 'User_simple.php', 'User_simple_nullable.php', - (new RelationManyToOne()) - ->setPropertyName('category') - ->setTargetClassName('App\Entity\Category') - ->setTargetPropertyName('foods') - ->setIsNullable(true), + (new RelationManyToOne( + propertyName: 'category', + targetClassName: \App\Entity\Category::class, + targetPropertyName: 'foods', + isOwning: true, + isNullable: true, + )), ]; yield 'many_to_one_other_namespace' => [ 'User_simple.php', 'User_simple_other_namespace.php', - (new RelationManyToOne()) - ->setPropertyName('category') - ->setTargetClassName('Foo\Entity\Category') - ->setTargetPropertyName('foods') - ->setIsNullable(true), + (new RelationManyToOne( + propertyName: 'category', + targetClassName: \Foo\Entity\Category::class, + targetPropertyName: 'foods', + isOwning: true, + isNullable: true, + )), ]; yield 'many_to_one_empty_other_namespace' => [ 'User_empty.php', 'User_empty_other_namespace.php', - (new RelationManyToOne()) - ->setPropertyName('category') - ->setTargetClassName('Foo\Entity\Category') - ->setTargetPropertyName('foods') - ->setIsNullable(true), + (new RelationManyToOne( + propertyName: 'category', + targetClassName: \Foo\Entity\Category::class, + targetPropertyName: 'foods', + isOwning: true, + isNullable: true, + )), ]; yield 'many_to_one_same_and_other_namespaces' => [ 'User_with_relation.php', 'User_with_relation_same_and_other_namespaces.php', - (new RelationManyToOne()) - ->setPropertyName('subCategory') - ->setTargetClassName('App\Entity\SubDirectory\Category') - ->setTargetPropertyName('foods') - ->setIsNullable(true), + (new RelationManyToOne( + propertyName: 'subCategory', + targetClassName: \App\Entity\SubDirectory\Category::class, + targetPropertyName: 'foods', + isOwning: true, + isNullable: true, + )), ]; yield 'many_to_one_no_inverse' => [ 'User_simple.php', 'User_simple_no_inverse.php', - (new RelationManyToOne()) - ->setPropertyName('category') - ->setTargetClassName('App\Entity\Category') - ->setTargetPropertyName('foods') - ->setIsNullable(true) - ->setMapInverseRelation(false), + (new RelationManyToOne( + propertyName: 'category', + targetClassName: \App\Entity\Category::class, + targetPropertyName: 'foods', + mapInverseRelation: false, + isOwning: true, + isNullable: true, + )), ]; } @@ -476,11 +487,11 @@ public function getAddOneToManyRelationTests(): \Generator yield 'one_to_many_simple' => [ 'User_simple.php', 'User_simple.php', - (new RelationOneToMany()) - ->setPropertyName('avatarPhotos') - ->setTargetClassName('App\Entity\UserAvatarPhoto') - ->setTargetPropertyName('user') - ->setOrphanRemoval(false), + (new RelationOneToMany( + propertyName: 'avatarPhotos', + targetClassName: \App\Entity\UserAvatarPhoto::class, + targetPropertyName: 'user', + )), ]; // interesting also because the source file has its @@ -488,21 +499,22 @@ public function getAddOneToManyRelationTests(): \Generator yield 'one_to_many_simple_no_duplicate_use' => [ 'User_with_use_statements.php', 'User_with_use_statements.php', - (new RelationOneToMany()) - ->setPropertyName('avatarPhotos') - ->setTargetClassName('App\Entity\UserAvatarPhoto') - ->setTargetPropertyName('user') - ->setOrphanRemoval(false), + (new RelationOneToMany( + propertyName: 'avatarPhotos', + targetClassName: \App\Entity\UserAvatarPhoto::class, + targetPropertyName: 'user', + )), ]; yield 'one_to_many_orphan_removal' => [ 'User_simple.php', 'User_simple_orphan_removal.php', - (new RelationOneToMany()) - ->setPropertyName('avatarPhotos') - ->setTargetClassName('App\Entity\UserAvatarPhoto') - ->setTargetPropertyName('user') - ->setOrphanRemoval(true), + (new RelationOneToMany( + propertyName: 'avatarPhotos', + targetClassName: \App\Entity\UserAvatarPhoto::class, + targetPropertyName: 'user', + orphanRemoval: true, + )), ]; // todo test existing constructor @@ -548,32 +560,34 @@ public function getAddManyToManyRelationTests(): \Generator yield 'many_to_many_owning' => [ 'User_simple.php', 'User_simple_owning.php', - (new RelationManyToMany()) - ->setPropertyName('recipes') - ->setTargetClassName('App\Entity\Recipe') - ->setTargetPropertyName('foods') - ->setIsOwning(true), + (new RelationManyToMany( + propertyName: 'recipes', + targetClassName: \App\Entity\Recipe::class, + targetPropertyName: 'foods', + isOwning: true, + )), ]; yield 'many_to_many_inverse' => [ 'User_simple.php', 'User_simple_inverse.php', - (new RelationManyToMany()) - ->setPropertyName('recipes') - ->setTargetClassName('App\Entity\Recipe') - ->setTargetPropertyName('foods') - ->setIsOwning(false), + (new RelationManyToMany( + propertyName: 'recipes', + targetClassName: \App\Entity\Recipe::class, + targetPropertyName: 'foods', + )), ]; yield 'many_to_many_owning_inverse' => [ 'User_simple.php', 'User_simple_no_inverse.php', - (new RelationManyToMany()) - ->setPropertyName('recipes') - ->setTargetClassName('App\Entity\Recipe') - ->setTargetPropertyName('foods') - ->setIsOwning(true) - ->setMapInverseRelation(false), + (new RelationManyToMany( + propertyName: 'recipes', + targetClassName: \App\Entity\Recipe::class, + targetPropertyName: 'foods', + mapInverseRelation: false, + isOwning: true, + )), ]; } @@ -617,90 +631,94 @@ public function getAddOneToOneRelationTests(): \Generator yield 'one_to_one_owning' => [ 'User_simple.php', 'User_simple_owning.php', - (new RelationOneToOne()) - ->setPropertyName('userProfile') - ->setTargetClassName('App\Entity\UserProfile') - ->setTargetPropertyName('user') - ->setIsNullable(true) - ->setIsOwning(true), + (new RelationOneToOne( + propertyName: 'userProfile', + targetClassName: \App\Entity\UserProfile::class, + targetPropertyName: 'user', + isOwning: true, + isNullable: true, + )), ]; // a relationship to yourself - return type is self yield 'one_to_one_owning_self' => [ 'User_simple.php', 'User_simple_self.php', - (new RelationOneToOne()) - ->setPropertyName('embeddedUser') - ->setTargetClassName('App\Entity\User') - ->setTargetPropertyName('user') - ->setIsNullable(true) - ->setIsOwning(true), + (new RelationOneToOne( + propertyName: 'embeddedUser', + targetClassName: \App\Entity\User::class, + targetPropertyName: 'user', + isOwning: true, + isNullable: true, + )), ]; yield 'one_to_one_inverse' => [ 'UserProfile_simple.php', 'UserProfile_simple_inverse.php', - (new RelationOneToOne()) - ->setPropertyName('user') - ->setTargetClassName('App\Entity\User') - ->setTargetPropertyName('userProfile') - ->setIsNullable(true) - ->setIsOwning(false), + (new RelationOneToOne( + propertyName: 'user', + targetClassName: \App\Entity\User::class, + targetPropertyName: 'userProfile', + isNullable: true, + )), ]; yield 'one_to_one_inverse_not_nullable' => [ 'UserProfile_simple.php', 'UserProfile_simple_inverse_not_nullable.php', - (new RelationOneToOne()) - ->setPropertyName('user') - ->setTargetClassName('App\Entity\User') - ->setTargetPropertyName('userProfile') - ->setIsNullable(false) - ->setIsOwning(false), + (new RelationOneToOne( + propertyName: 'user', + targetClassName: \App\Entity\User::class, + targetPropertyName: 'userProfile', + )), ]; yield 'one_to_one_no_inverse' => [ 'User_simple.php', 'User_simple_no_inverse.php', - (new RelationOneToOne()) - ->setPropertyName('userProfile') - ->setTargetClassName('App\Entity\UserProfile') - ->setIsNullable(true) - ->setIsOwning(true) - ->setMapInverseRelation(false), + (new RelationOneToOne( + propertyName: 'userProfile', + targetClassName: \App\Entity\UserProfile::class, + mapInverseRelation: false, + isOwning: true, + isNullable: true, + )), ]; yield 'one_to_one_no_inverse_not_nullable' => [ 'User_simple.php', 'User_simple_no_inverse_not_nullable.php', - (new RelationOneToOne()) - ->setPropertyName('userProfile') - ->setTargetClassName('App\Entity\UserProfile') - ->setIsNullable(false) - ->setIsOwning(true) - ->setMapInverseRelation(false), + (new RelationOneToOne( + propertyName: 'userProfile', + targetClassName: \App\Entity\UserProfile::class, + mapInverseRelation: false, + isOwning: true, + )), ]; yield 'avoid_duplicate_use_statement' => [ 'User_with_use_statements.php', 'User_with_use_statements_avoid_duplicate_use.php', - (new RelationOneToOne()) - ->setPropertyName('userProfile') - ->setTargetClassName('App\OtherEntity\UserProfile') - ->setTargetPropertyName('user') - ->setIsNullable(true) - ->setIsOwning(true), + (new RelationOneToOne( + propertyName: 'userProfile', + targetClassName: \App\OtherEntity\UserProfile::class, + targetPropertyName: 'user', + isOwning: true, + isNullable: true, + )), ]; yield 'avoid_duplicate_use_statement_with_alias' => [ 'User_with_use_statements.php', 'User_with_use_statements_avoid_duplicate_use_alias.php', - (new RelationOneToOne()) - ->setPropertyName('category') - ->setTargetClassName('App\OtherEntity\Category') - ->setTargetPropertyName('user') - ->setIsNullable(true) - ->setIsOwning(true), + (new RelationOneToOne( + propertyName: 'category', + targetClassName: \App\OtherEntity\Category::class, + targetPropertyName: 'user', + isOwning: true, + isNullable: true, + )), ]; }