1818use Doctrine \Inflector \Inflector ;
1919use Doctrine \Inflector \InflectorFactory ;
2020use Doctrine \ORM \EntityManagerInterface ;
21- use Doctrine \ORM \Mapping \ClassMetadataInfo ;
21+ use Doctrine \ORM \Mapping \AssociationMapping ;
22+ use Doctrine \ORM \Mapping \ClassMetadata ;
23+ use Doctrine \ORM \Mapping \EmbeddedClassMapping ;
24+ use Doctrine \ORM \Mapping \FieldMapping ;
25+ use Doctrine \ORM \Mapping \InverseSideMapping ;
26+ use Doctrine \ORM \Mapping \OwningSideMapping ;
2227use Doctrine \Persistence \ManagerRegistry ;
2328use Ecommit \DoctrineEntitiesGeneratorBundle \Attribute \GenerateEntityTemplate ;
2429use Ecommit \DoctrineEntitiesGeneratorBundle \Entity \EntityInitializerInterface ;
3136use Twig \Environment ;
3237
3338/**
34- * @phpstan-import-type FieldMapping from ClassMetadataInfo
35- * @phpstan-import-type EmbeddedClassMapping from ClassMetadataInfo
36- * @phpstan-import-type AssociationMapping from ClassMetadataInfo
3739 * @phpstan-import-type FileParts from GenerateEntityRequest
3840 */
3941class EntityGenerator implements EntityGeneratorInterface
@@ -43,37 +45,10 @@ class EntityGenerator implements EntityGeneratorInterface
4345 public const TYPE_ADD = 'add ' ;
4446 public const TYPE_REMOVE = 'remove ' ;
4547
46- /**
47- * @var EntitySearcherInterface
48- */
49- protected $ searcher ;
48+ protected Inflector $ inflector ;
5049
51- /**
52- * @var ManagerRegistry
53- */
54- protected $ registry ;
55-
56- /**
57- * @var Environment
58- */
59- protected $ twig ;
60-
61- /**
62- * @var Inflector
63- */
64- protected $ inflector ;
65-
66- /**
67- * @var string
68- */
69- protected $ template ;
70-
71- public function __construct (EntitySearcherInterface $ searcher , ManagerRegistry $ registry , Environment $ twig , string $ template )
50+ public function __construct (protected EntitySearcherInterface $ searcher , protected ManagerRegistry $ registry , protected Environment $ twig , protected string $ template )
7251 {
73- $ this ->searcher = $ searcher ;
74- $ this ->registry = $ registry ;
75- $ this ->twig = $ twig ;
76- $ this ->template = $ template ;
7752 $ this ->inflector = InflectorFactory::create ()->build ();
7853 }
7954
@@ -85,9 +60,6 @@ public function generate(string $className): void
8560 throw new ClassNotManagedException (\sprintf ('Class "%s" not managed ' , $ className ));
8661 }
8762 $ metadata = $ entityManager ->getClassMetadata ($ reflectionClass ->getName ());
88- if (!$ metadata instanceof ClassMetadataInfo) {
89- throw new ClassNotManagedException (\sprintf ('Class "%s" cannot be generated (Metatada not found) ' , $ className ));
90- }
9163
9264 if (!$ this ->searcher ->classCanBeGenerated ($ metadata )) {
9365 throw new ClassNotManagedException (\sprintf ('Class "%s" cannot be generated (Is IgnoreGenerateEntity attribute used ?) ' , $ className ));
@@ -268,12 +240,9 @@ protected function methodIsDefinedOutsideBlock(GenerateEntityRequest $request, s
268240 return false ;
269241 }
270242
271- /**
272- * @param FieldMapping $fieldMapping
273- */
274- protected function addField (GenerateEntityRequest $ request , array $ fieldMapping ): void
243+ protected function addField (GenerateEntityRequest $ request , FieldMapping $ fieldMapping ): void
275244 {
276- $ fieldName = $ fieldMapping[ ' fieldName ' ] ;
245+ $ fieldName = $ fieldMapping-> fieldName ;
277246 $ types = $ request ->doctrineExtractor ->getTypes ($ request ->reflectionClass ->getName (), $ fieldName );
278247 $ phpType = (new \ReflectionProperty ($ request ->reflectionClass ->getName (), $ fieldName ))->getType ();
279248
@@ -305,12 +274,9 @@ protected function addField(GenerateEntityRequest $request, array $fieldMapping)
305274 }
306275 }
307276
308- /**
309- * @param EmbeddedClassMapping $embeddedMapping
310- */
311- protected function addEmbedded (GenerateEntityRequest $ request , string $ fieldName , array $ embeddedMapping ): void
277+ protected function addEmbedded (GenerateEntityRequest $ request , string $ fieldName , EmbeddedClassMapping $ embeddedMapping ): void
312278 {
313- $ targetClass = $ embeddedMapping[ ' class ' ] ;
279+ $ targetClass = $ embeddedMapping-> class ;
314280 $ phpType = (new \ReflectionProperty ($ request ->reflectionClass ->getName (), $ fieldName ))->getType ();
315281
316282 $ targetClassAlias = $ request ->useStatementManipulator ->addUseStatementIfNecessary ($ targetClass );
@@ -346,58 +312,55 @@ protected function addEmbedded(GenerateEntityRequest $request, string $fieldName
346312 }
347313 }
348314
349- /**
350- * @param AssociationMapping $associationMapping
351- */
352- protected function addAssociation (GenerateEntityRequest $ request , array $ associationMapping ): void
315+ protected function addAssociation (GenerateEntityRequest $ request , AssociationMapping $ associationMapping ): void
353316 {
354- if ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::TO_ONE && $ associationMapping[ ' mappedBy ' ] ) {
317+ if ($ associationMapping-> type () & ClassMetadata ::TO_ONE && $ associationMapping instanceof InverseSideMapping ) {
355318 $ this ->addAssociationToOne (
356319 $ request ,
357320 $ associationMapping ,
358321 'assocation_one_to_one_reverse ' ,
359- $ this ->buildMethodName (self ::TYPE_SET , $ associationMapping[ ' mappedBy ' ] )
322+ $ this ->buildMethodName (self ::TYPE_SET , $ associationMapping-> mappedBy )
360323 );
361- } elseif ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::TO_ONE && $ associationMapping[ ' inversedBy ' ] ) {
324+ } elseif ($ associationMapping-> type () & ClassMetadata ::TO_ONE && $ associationMapping instanceof OwningSideMapping ) {
362325 $ this ->addAssociationToOne (
363326 $ request ,
364327 $ associationMapping ,
365328 'assocation_one_to_one_owning ' ,
366329 null
367330 );
368- } elseif ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::TO_ONE ) {
331+ } elseif ($ associationMapping-> type () & ClassMetadata ::TO_ONE ) {
369332 $ this ->addAssociationToOne (
370333 $ request ,
371334 $ associationMapping ,
372335 'assocation_one_to_one_unidirectional ' ,
373336 null
374337 );
375- } elseif ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::TO_MANY ) {
376- if ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::ONE_TO_MANY && $ associationMapping[ ' mappedBy ' ] ) {
338+ } elseif ($ associationMapping-> type () & ClassMetadata ::TO_MANY ) {
339+ if ($ associationMapping-> type () & ClassMetadata ::ONE_TO_MANY && $ associationMapping instanceof InverseSideMapping ) {
377340 $ this ->addAssociationToMany (
378341 $ request ,
379342 $ associationMapping ,
380343 'assocation_one_to_many_reverse ' ,
381- $ this ->buildMethodName (self ::TYPE_SET , $ associationMapping[ ' mappedBy ' ] ),
382- $ this ->buildMethodName (self ::TYPE_SET , $ associationMapping[ ' mappedBy ' ] )
344+ $ this ->buildMethodName (self ::TYPE_SET , $ associationMapping-> mappedBy ),
345+ $ this ->buildMethodName (self ::TYPE_SET , $ associationMapping-> mappedBy )
383346 );
384- } elseif ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::MANY_TO_MANY && $ associationMapping[ ' mappedBy ' ] ) {
347+ } elseif ($ associationMapping-> type () & ClassMetadata ::MANY_TO_MANY && $ associationMapping instanceof InverseSideMapping ) {
385348 $ this ->addAssociationToMany (
386349 $ request ,
387350 $ associationMapping ,
388351 'assocation_many_to_many_reverse ' ,
389- $ this ->buildMethodName (self ::TYPE_ADD , $ associationMapping[ ' mappedBy ' ] ),
390- $ this ->buildMethodName (self ::TYPE_REMOVE , $ associationMapping[ ' mappedBy ' ] )
352+ $ this ->buildMethodName (self ::TYPE_ADD , $ associationMapping-> mappedBy ),
353+ $ this ->buildMethodName (self ::TYPE_REMOVE , $ associationMapping-> mappedBy )
391354 );
392- } elseif ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::MANY_TO_MANY && $ associationMapping[ ' inversedBy ' ] ) {
355+ } elseif ($ associationMapping-> type () & ClassMetadata ::MANY_TO_MANY && $ associationMapping instanceof OwningSideMapping ) {
393356 $ this ->addAssociationToMany (
394357 $ request ,
395358 $ associationMapping ,
396359 'assocation_many_to_many_owning ' ,
397360 null ,
398361 null
399362 );
400- } elseif ($ associationMapping[ ' type ' ] & ClassMetadataInfo ::MANY_TO_MANY ) {
363+ } elseif ($ associationMapping-> type () & ClassMetadata ::MANY_TO_MANY ) {
401364 $ this ->addAssociationToMany (
402365 $ request ,
403366 $ associationMapping ,
@@ -409,13 +372,10 @@ protected function addAssociation(GenerateEntityRequest $request, array $associa
409372 }
410373 }
411374
412- /**
413- * @param AssociationMapping $associationMapping
414- */
415- protected function addAssociationToOne (GenerateEntityRequest $ request , array $ associationMapping , string $ block , ?string $ foreignMethodNameSet ): void
375+ protected function addAssociationToOne (GenerateEntityRequest $ request , AssociationMapping $ associationMapping , string $ block , ?string $ foreignMethodNameSet ): void
416376 {
417- $ fieldName = $ associationMapping[ ' fieldName ' ] ;
418- $ targetEntity = $ associationMapping[ ' targetEntity ' ] ;
377+ $ fieldName = $ associationMapping-> fieldName ;
378+ $ targetEntity = $ associationMapping-> targetEntity ;
419379
420380 $ targetEntityAlias = $ request ->useStatementManipulator ->addUseStatementIfNecessary ($ targetEntity );
421381 if ($ request ->reflectionClass ->getName () === $ targetEntity ) {
@@ -449,13 +409,10 @@ protected function addAssociationToOne(GenerateEntityRequest $request, array $as
449409 }
450410 }
451411
452- /**
453- * @param AssociationMapping $associationMapping
454- */
455- protected function addAssociationToMany (GenerateEntityRequest $ request , array $ associationMapping , string $ block , ?string $ foreignMethodNameAdd , ?string $ foreignMethodNameRemove ): void
412+ protected function addAssociationToMany (GenerateEntityRequest $ request , AssociationMapping $ associationMapping , string $ block , ?string $ foreignMethodNameAdd , ?string $ foreignMethodNameRemove ): void
456413 {
457- $ fieldName = $ associationMapping[ ' fieldName ' ] ;
458- $ targetEntity = $ associationMapping[ ' targetEntity ' ] ;
414+ $ fieldName = $ associationMapping-> fieldName ;
415+ $ targetEntity = $ associationMapping-> targetEntity ;
459416
460417 $ targetEntityAlias = $ request ->useStatementManipulator ->addUseStatementIfNecessary ($ targetEntity );
461418 if ($ request ->reflectionClass ->getName () === $ targetEntity ) {
0 commit comments