@@ -110,6 +110,18 @@ class BeanDescriptor implements BeanDescriptorInterface
110
110
* @var BeanRegistry
111
111
*/
112
112
private $ registry ;
113
+ /**
114
+ * @var int[]
115
+ */
116
+ private $ descriptorsByMethodName = [];
117
+ /**
118
+ * @var DirectForeignKeyMethodDescriptor[]|null
119
+ */
120
+ private $ directForeignKeysDescriptors = null ;
121
+ /**
122
+ * @var PivotTableMethodsDescriptor[]|null
123
+ */
124
+ private $ pivotTableDescriptors = null ;
113
125
114
126
public function __construct (
115
127
Table $ table ,
@@ -144,6 +156,18 @@ public function __construct(
144
156
public function initBeanPropertyDescriptors (): void
145
157
{
146
158
$ this ->beanPropertyDescriptors = $ this ->getProperties ($ this ->table );
159
+
160
+ //init the list of method names with regular properties names
161
+ foreach ($ this ->beanPropertyDescriptors as $ beanPropertyDescriptor ) {
162
+ $ name = $ beanPropertyDescriptor ->getGetterName ();
163
+ if (!isset ($ this ->descriptorsByMethodName [$ name ])) {
164
+ $ this ->descriptorsByMethodName [$ name ] = 0 ;
165
+ }
166
+ $ this ->descriptorsByMethodName [$ name ] ++;
167
+ if ($ this ->descriptorsByMethodName [$ name ] > 1 ) {
168
+ $ beanPropertyDescriptor ->useAlternativeName ();
169
+ }
170
+ }
147
171
}
148
172
149
173
/**
@@ -373,38 +397,68 @@ private function generateBeanConstructor() : MethodGenerator
373
397
*/
374
398
private function getDirectForeignKeysDescriptors (): array
375
399
{
400
+ if ($ this ->directForeignKeysDescriptors !== null ) {
401
+ return $ this ->directForeignKeysDescriptors ;
402
+ }
376
403
$ fks = $ this ->tdbmSchemaAnalyzer ->getIncomingForeignKeys ($ this ->table ->getName ());
377
404
378
405
$ descriptors = [];
379
406
380
407
foreach ($ fks as $ fk ) {
381
- $ descriptors [] = new DirectForeignKeyMethodDescriptor ($ fk , $ this ->table , $ this ->namingStrategy , $ this ->annotationParser , $ this ->beanNamespace );
408
+ /** @var DirectForeignKeyMethodDescriptor $desc */
409
+ $ desc = $ this ->checkForDuplicate (new DirectForeignKeyMethodDescriptor ($ fk , $ this ->table , $ this ->namingStrategy , $ this ->annotationParser , $ this ->beanNamespace ));
410
+ $ descriptors [] = $ desc ;
382
411
}
383
412
384
- return $ descriptors ;
413
+ $ this ->directForeignKeysDescriptors = $ descriptors ;
414
+ return $ this ->directForeignKeysDescriptors ;
385
415
}
386
416
387
417
/**
388
418
* @return PivotTableMethodsDescriptor[]
389
419
*/
390
420
private function getPivotTableDescriptors (): array
391
421
{
422
+ if ($ this ->pivotTableDescriptors !== null ) {
423
+ return $ this ->pivotTableDescriptors ;
424
+ }
392
425
$ descs = [];
393
426
foreach ($ this ->schemaAnalyzer ->detectJunctionTables (true ) as $ table ) {
394
427
// There are exactly 2 FKs since this is a pivot table.
395
428
$ fks = array_values ($ table ->getForeignKeys ());
396
429
397
430
if ($ fks [0 ]->getForeignTableName () === $ this ->table ->getName ()) {
398
431
list ($ localFk , $ remoteFk ) = $ fks ;
399
- $ descs [] = new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser );
432
+ /** @var PivotTableMethodsDescriptor $desc */
433
+ $ desc = $ this ->checkForDuplicate (new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser ));
434
+ $ descs [] = $ desc ;
400
435
}
401
436
if ($ fks [1 ]->getForeignTableName () === $ this ->table ->getName ()) {
402
437
list ($ remoteFk , $ localFk ) = $ fks ;
403
- $ descs [] = new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser );
438
+ /** @var PivotTableMethodsDescriptor $desc */
439
+ $ desc = $ this ->checkForDuplicate (new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser ));
440
+ $ descs [] = $ desc ;
404
441
}
405
442
}
406
443
407
- return $ descs ;
444
+ $ this ->pivotTableDescriptors = $ descs ;
445
+ return $ this ->pivotTableDescriptors ;
446
+ }
447
+
448
+ /**
449
+ * Check the method name isn't already used and flag the $descriptor to use its alternative name if it is the case
450
+ */
451
+ public function checkForDuplicate (MethodDescriptorInterface $ descriptor ): MethodDescriptorInterface
452
+ {
453
+ $ name = $ descriptor ->getName ();
454
+ if (!isset ($ this ->descriptorsByMethodName [$ name ])) {
455
+ $ this ->descriptorsByMethodName [$ name ] = 0 ;
456
+ }
457
+ $ this ->descriptorsByMethodName [$ name ] ++;
458
+ if ($ this ->descriptorsByMethodName [$ name ] > 1 ) {
459
+ $ descriptor ->useAlternativeName ();
460
+ }
461
+ return $ descriptor ;
408
462
}
409
463
410
464
/**
@@ -417,24 +471,7 @@ public function getMethodDescriptors(): array
417
471
$ directForeignKeyDescriptors = $ this ->getDirectForeignKeysDescriptors ();
418
472
$ pivotTableDescriptors = $ this ->getPivotTableDescriptors ();
419
473
420
- $ descriptors = array_merge ($ directForeignKeyDescriptors , $ pivotTableDescriptors );
421
-
422
- // Descriptors by method names
423
- $ descriptorsByMethodName = [];
424
-
425
- foreach ($ descriptors as $ descriptor ) {
426
- $ descriptorsByMethodName [$ descriptor ->getName ()][] = $ descriptor ;
427
- }
428
-
429
- foreach ($ descriptorsByMethodName as $ descriptorsForMethodName ) {
430
- if (count ($ descriptorsForMethodName ) > 1 ) {
431
- foreach ($ descriptorsForMethodName as $ descriptor ) {
432
- $ descriptor ->useAlternativeName ();
433
- }
434
- }
435
- }
436
-
437
- return $ descriptors ;
474
+ return array_merge ($ directForeignKeyDescriptors , $ pivotTableDescriptors );
438
475
}
439
476
440
477
public function generateJsonSerialize (): MethodGenerator
0 commit comments