Skip to content

Commit 97cfa5b

Browse files
committed
improved dulicate names detection in bean descriptor
1 parent 7afa37a commit 97cfa5b

File tree

2 files changed

+61
-24
lines changed

2 files changed

+61
-24
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ class BeanDescriptor implements BeanDescriptorInterface
110110
* @var BeanRegistry
111111
*/
112112
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;
113125

114126
public function __construct(
115127
Table $table,
@@ -144,6 +156,18 @@ public function __construct(
144156
public function initBeanPropertyDescriptors(): void
145157
{
146158
$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+
}
147171
}
148172

149173
/**
@@ -373,38 +397,68 @@ private function generateBeanConstructor() : MethodGenerator
373397
*/
374398
private function getDirectForeignKeysDescriptors(): array
375399
{
400+
if ($this->directForeignKeysDescriptors !== null) {
401+
return $this->directForeignKeysDescriptors;
402+
}
376403
$fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName());
377404

378405
$descriptors = [];
379406

380407
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;
382411
}
383412

384-
return $descriptors;
413+
$this->directForeignKeysDescriptors = $descriptors;
414+
return $this->directForeignKeysDescriptors;
385415
}
386416

387417
/**
388418
* @return PivotTableMethodsDescriptor[]
389419
*/
390420
private function getPivotTableDescriptors(): array
391421
{
422+
if ($this->pivotTableDescriptors !== null) {
423+
return $this->pivotTableDescriptors;
424+
}
392425
$descs = [];
393426
foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) {
394427
// There are exactly 2 FKs since this is a pivot table.
395428
$fks = array_values($table->getForeignKeys());
396429

397430
if ($fks[0]->getForeignTableName() === $this->table->getName()) {
398431
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;
400435
}
401436
if ($fks[1]->getForeignTableName() === $this->table->getName()) {
402437
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;
404441
}
405442
}
406443

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;
408462
}
409463

410464
/**
@@ -417,24 +471,7 @@ public function getMethodDescriptors(): array
417471
$directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors();
418472
$pivotTableDescriptors = $this->getPivotTableDescriptors();
419473

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);
438475
}
439476

440477
public function generateJsonSerialize(): MethodGenerator

tests/TDBMDaoGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2008,7 +2008,7 @@ public function testFloydHasOneChild(): void
20082008
{
20092009
$artists = new ArtistDao($this->tdbmService);
20102010
$pinkFloyd = $artists->getById(1);
2011-
$children = $pinkFloyd->getChildren();
2011+
$children = $pinkFloyd->getChildrenByArtistsRelations();
20122012

20132013
$this->assertEquals(1, count($children));
20142014
$this->assertEquals(2, $children[0]->getId());

0 commit comments

Comments
 (0)