Skip to content

Commit 4e52397

Browse files
author
Roger Batista
committed
Fixed trait manipulator to avoid duplicate interface calls
1 parent 9a9f237 commit 4e52397

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ class Adder implements CodeGeneratorUnitInterface
1515
protected $classMetadata;
1616
protected $visibility;
1717

18+
protected GetReturnHint $getReturnHint;
19+
1820
public function __construct(
1921
string $propertyName,
2022
string $type,
2123
bool $isNullable,
2224
$classMetadata,
25+
GetReturnHint $getReturnHint,
2326
array $commentLines = [],
2427
array $columnOptions = [],
25-
string $visibility = 'protected'
28+
string $visibility = 'protected',
2629
) {
2730
$this->propertyName = $propertyName;
2831
$this->type = $type;
@@ -31,6 +34,7 @@ public function __construct(
3134
$this->columnOptions = $columnOptions;
3235
$this->classMetadata = $classMetadata;
3336
$this->visibility = $visibility;
37+
$this->getReturnHint = $getReturnHint;
3438
}
3539

3640
public function toString(string $nlLeftPad = ''): string
@@ -41,7 +45,9 @@ public function toString(string $nlLeftPad = ''): string
4145
$methodName = 'add' . ucfirst($singularProperty);
4246

4347
$fqdnSegments = explode('\\', $this->classMetadata->name);
44-
$returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface';
48+
$returnHint = $this->getReturnHint->execute(
49+
$fqdnSegments[count($fqdnSegments) -2] . 'Interface'
50+
);
4551

4652
$response = [];
4753
$response[] = sprintf(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace IvozDevTools\EntityGeneratorBundle\Doctrine\EntityTrait;
4+
5+
class GetReturnHint
6+
{
7+
private array $useStatements;
8+
9+
public function __construct(
10+
array $useStatements
11+
)
12+
{
13+
$this->useStatements = $useStatements;
14+
}
15+
16+
public function execute(string $returnHint): string
17+
{
18+
$similarInterfaces = false;
19+
20+
foreach (array_keys($this->useStatements) as $fqdn) {
21+
$parts = explode('\\', $fqdn);
22+
$lastValue = end($parts);
23+
24+
if ($lastValue === $returnHint) {
25+
$similarInterfaces = true;
26+
break;
27+
}
28+
}
29+
30+
if ($similarInterfaces) {
31+
$returnHint = 'static';
32+
}
33+
34+
return $returnHint;
35+
}
36+
}

EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ class Remover implements CodeGeneratorUnitInterface
1515
protected $classMetadata;
1616
protected $visibility;
1717

18+
protected $returnHintCallable;
19+
protected GetReturnHint $getReturnHint;
20+
1821
public function __construct(
1922
string $propertyName,
2023
string $type,
2124
bool $isNullable,
2225
$classMetadata,
26+
GetReturnHint $getReturnHint,
2327
array $commentLines = [],
2428
array $columnOptions = [],
25-
string $visibility = 'protected'
29+
string $visibility = 'protected',
30+
?callable $returnHintCallable = null
2631
) {
2732
$this->propertyName = $propertyName;
2833
$this->type = $type;
@@ -31,6 +36,8 @@ public function __construct(
3136
$this->columnOptions = $columnOptions;
3237
$this->classMetadata = $classMetadata;
3338
$this->visibility = $visibility;
39+
$this->returnHintCallable = $returnHintCallable;
40+
$this->getReturnHint = $getReturnHint;
3441
}
3542

3643
public function toString(string $nlLeftPad = ''): string
@@ -40,7 +47,9 @@ public function toString(string $nlLeftPad = ''): string
4047

4148
$methodName = 'remove' . ucfirst($singularProperty);
4249
$fqdnSegments = explode('\\', $this->classMetadata->name);
43-
$returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface';
50+
$returnHint = $this->getReturnHint->execute(
51+
$fqdnSegments[count($fqdnSegments) -2] . 'Interface'
52+
);
4453

4554
$response = [];
4655
$response[] = sprintf(

EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ class Replacer implements CodeGeneratorUnitInterface
1515
protected $classMetadata;
1616
protected $visibility;
1717

18+
protected GetReturnHint $getReturnHint;
19+
1820
public function __construct(
1921
string $propertyName,
2022
string $type,
2123
bool $isNullable,
2224
$classMetadata,
25+
GetReturnHint $getReturnHint,
2326
array $commentLines = [],
2427
array $columnOptions = [],
2528
string $visibility = 'protected'
@@ -31,6 +34,7 @@ public function __construct(
3134
$this->columnOptions = $columnOptions;
3235
$this->classMetadata = $classMetadata;
3336
$this->visibility = $visibility;
37+
$this->getReturnHint = $getReturnHint;
3438
}
3539

3640
public function toString(string $nlLeftPad = ''): string
@@ -45,7 +49,9 @@ public function toString(string $nlLeftPad = ''): string
4549

4650
$methodName = 'replace' . $camelCaseProperty;
4751
$fqdnSegments = explode('\\', $this->classMetadata->name);
48-
$returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface';
52+
$returnHint = $this->getReturnHint->execute(
53+
$fqdnSegments[count($fqdnSegments) -2] . 'Interface'
54+
);
4955

5056
$response = [];
5157
$response[] = '/**';

EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,13 @@ public function addSetter(
248248
array $columnOptions = [],
249249
string $visibility = 'protected'
250250
) {
251+
$getReturnHint = new GetReturnHint($this->useStatements);
251252
$this->methods[] = new Adder(
252253
$propertyName,
253254
$type,
254255
$isNullable,
255256
$classMetadata,
257+
$getReturnHint,
256258
$commentLines,
257259
$columnOptions,
258260
$visibility
@@ -263,6 +265,7 @@ public function addSetter(
263265
$type,
264266
$isNullable,
265267
$classMetadata,
268+
$getReturnHint,
266269
$commentLines,
267270
$columnOptions,
268271
$visibility
@@ -273,6 +276,7 @@ public function addSetter(
273276
$type,
274277
$isNullable,
275278
$classMetadata,
279+
$getReturnHint,
276280
$commentLines,
277281
$columnOptions,
278282
$visibility

0 commit comments

Comments
 (0)