Skip to content

Commit 58bdc11

Browse files
committed
DAN feedback
1 parent 97cfa5b commit 58bdc11

6 files changed

+77
-56
lines changed

src/Utils/AbstractBeanPropertyDescriptor.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* This class represent a property in a bean (a property has a getter, a setter, etc...).
1212
*/
13-
abstract class AbstractBeanPropertyDescriptor
13+
abstract class AbstractBeanPropertyDescriptor implements MethodDescriptorInterface
1414
{
1515
/**
1616
* @var Table
@@ -80,6 +80,14 @@ public function getSetterName(): string
8080
return $this->namingStrategy->getSetterName($this);
8181
}
8282

83+
/**
84+
* Alias of the method getGetterName(). Used to validate MethodDescriptorInterface
85+
*/
86+
public function getName(): string
87+
{
88+
return $this->getGetterName();
89+
}
90+
8391
public function getGetterName(): string
8492
{
8593
return $this->namingStrategy->getGetterName($this);

src/Utils/BeanDescriptor.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class BeanDescriptor implements BeanDescriptorInterface
111111
*/
112112
private $registry;
113113
/**
114-
* @var int[]
114+
* @var MethodDescriptorInterface[][]
115115
*/
116116
private $descriptorsByMethodName = [];
117117
/**
@@ -159,14 +159,7 @@ public function initBeanPropertyDescriptors(): void
159159

160160
//init the list of method names with regular properties names
161161
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-
}
162+
$this->checkForDuplicate($beanPropertyDescriptor);
170163
}
171164
}
172165

@@ -405,8 +398,8 @@ private function getDirectForeignKeysDescriptors(): array
405398
$descriptors = [];
406399

407400
foreach ($fks as $fk) {
408-
/** @var DirectForeignKeyMethodDescriptor $desc */
409-
$desc = $this->checkForDuplicate(new DirectForeignKeyMethodDescriptor($fk, $this->table, $this->namingStrategy, $this->annotationParser, $this->beanNamespace));
401+
$desc = new DirectForeignKeyMethodDescriptor($fk, $this->table, $this->namingStrategy, $this->annotationParser, $this->beanNamespace);
402+
$this->checkForDuplicate($desc);
410403
$descriptors[] = $desc;
411404
}
412405

@@ -429,14 +422,14 @@ private function getPivotTableDescriptors(): array
429422

430423
if ($fks[0]->getForeignTableName() === $this->table->getName()) {
431424
list($localFk, $remoteFk) = $fks;
432-
/** @var PivotTableMethodsDescriptor $desc */
433-
$desc = $this->checkForDuplicate(new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser));
425+
$desc = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser);
426+
$this->checkForDuplicate($desc);
434427
$descs[] = $desc;
435428
}
436429
if ($fks[1]->getForeignTableName() === $this->table->getName()) {
437430
list($remoteFk, $localFk) = $fks;
438-
/** @var PivotTableMethodsDescriptor $desc */
439-
$desc = $this->checkForDuplicate(new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser));
431+
$desc = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser);
432+
$this->checkForDuplicate($desc);
440433
$descs[] = $desc;
441434
}
442435
}
@@ -446,25 +439,26 @@ private function getPivotTableDescriptors(): array
446439
}
447440

448441
/**
449-
* Check the method name isn't already used and flag the $descriptor to use its alternative name if it is the case
442+
* Check the method name isn't already used and flag the associated descriptors to use their alternative names if it is the case
450443
*/
451-
public function checkForDuplicate(MethodDescriptorInterface $descriptor): MethodDescriptorInterface
444+
private function checkForDuplicate(MethodDescriptorInterface $descriptor): void
452445
{
453446
$name = $descriptor->getName();
454447
if (!isset($this->descriptorsByMethodName[$name])) {
455-
$this->descriptorsByMethodName[$name] = 0;
448+
$this->descriptorsByMethodName[$name] = [];
456449
}
457-
$this->descriptorsByMethodName[$name] ++;
458-
if ($this->descriptorsByMethodName[$name] > 1) {
459-
$descriptor->useAlternativeName();
450+
$this->descriptorsByMethodName[$name][] = $descriptor;
451+
if (count($this->descriptorsByMethodName[$name]) > 1) {
452+
foreach ($this->descriptorsByMethodName[$name] as $duplicateDescriptor) {
453+
$duplicateDescriptor->useAlternativeName();
454+
}
460455
}
461-
return $descriptor;
462456
}
463457

464458
/**
465459
* Returns the list of method descriptors (and applies the alternative name if needed).
466460
*
467-
* @return MethodDescriptorInterface[]
461+
* @return RelationshipMethodDescriptorInterface[]
468462
*/
469463
public function getMethodDescriptors(): array
470464
{

src/Utils/DirectForeignKeyMethodDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Represents a method to get a list of beans from a direct foreign key pointing to our bean.
2020
*/
21-
class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface
21+
class DirectForeignKeyMethodDescriptor implements RelationshipMethodDescriptorInterface
2222
{
2323
use ForeignKeyAnalyzerTrait;
2424

src/Utils/MethodDescriptorInterface.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace TheCodingMachine\TDBM\Utils;
55

6-
use Zend\Code\Generator\MethodGenerator;
7-
86
interface MethodDescriptorInterface
97
{
108
/**
@@ -14,36 +12,8 @@ interface MethodDescriptorInterface
1412
*/
1513
public function getName() : string;
1614

17-
/**
18-
* Returns the name of the class that will be returned by the getter (short name).
19-
*
20-
* @return string
21-
*/
22-
public function getBeanClassName(): string;
23-
2415
/**
2516
* Requests the use of an alternative name for this method.
2617
*/
2718
public function useAlternativeName(): void;
28-
29-
/**
30-
* Returns the code of the method.
31-
*
32-
* @return MethodGenerator[]
33-
*/
34-
public function getCode() : array;
35-
36-
/**
37-
* Returns an array of classes that needs a "use" for this method.
38-
*
39-
* @return string[]
40-
*/
41-
public function getUsedClasses() : array;
42-
43-
/**
44-
* Returns the code to past in jsonSerialize.
45-
*
46-
* @return string
47-
*/
48-
public function getJsonSerializeCode() : string;
4919
}

src/Utils/PivotTableMethodsDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Zend\Code\Generator\ParameterGenerator;
1818
use function var_export;
1919

20-
class PivotTableMethodsDescriptor implements MethodDescriptorInterface
20+
class PivotTableMethodsDescriptor implements RelationshipMethodDescriptorInterface
2121
{
2222
/**
2323
* @var Table
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TheCodingMachine\TDBM\Utils;
5+
6+
use Zend\Code\Generator\MethodGenerator;
7+
8+
interface RelationshipMethodDescriptorInterface extends MethodDescriptorInterface
9+
{
10+
/**
11+
* Returns the name of the method to be generated.
12+
*
13+
* @return string
14+
*/
15+
public function getName() : string;
16+
17+
/**
18+
* Returns the name of the class that will be returned by the getter (short name).
19+
*
20+
* @return string
21+
*/
22+
public function getBeanClassName(): string;
23+
24+
/**
25+
* Requests the use of an alternative name for this method.
26+
*/
27+
public function useAlternativeName(): void;
28+
29+
/**
30+
* Returns the code of the method.
31+
*
32+
* @return MethodGenerator[]
33+
*/
34+
public function getCode() : array;
35+
36+
/**
37+
* Returns an array of classes that needs a "use" for this method.
38+
*
39+
* @return string[]
40+
*/
41+
public function getUsedClasses() : array;
42+
43+
/**
44+
* Returns the code to past in jsonSerialize.
45+
*
46+
* @return string
47+
*/
48+
public function getJsonSerializeCode() : string;
49+
}

0 commit comments

Comments
 (0)