Skip to content

Commit

Permalink
Fixed trait manipulator to avoid duplicate interface calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rbatistadev committed Aug 30, 2024
1 parent 9a9f237 commit 4e52397
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
10 changes: 8 additions & 2 deletions EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ class Adder implements CodeGeneratorUnitInterface
protected $classMetadata;
protected $visibility;

protected GetReturnHint $getReturnHint;

public function __construct(
string $propertyName,
string $type,
bool $isNullable,
$classMetadata,
GetReturnHint $getReturnHint,
array $commentLines = [],
array $columnOptions = [],
string $visibility = 'protected'
string $visibility = 'protected',
) {
$this->propertyName = $propertyName;
$this->type = $type;
Expand All @@ -31,6 +34,7 @@ public function __construct(
$this->columnOptions = $columnOptions;
$this->classMetadata = $classMetadata;
$this->visibility = $visibility;
$this->getReturnHint = $getReturnHint;
}

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

$fqdnSegments = explode('\\', $this->classMetadata->name);
$returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface';
$returnHint = $this->getReturnHint->execute(
$fqdnSegments[count($fqdnSegments) -2] . 'Interface'
);

$response = [];
$response[] = sprintf(
Expand Down
36 changes: 36 additions & 0 deletions EntityGeneratorBundle/Doctrine/EntityTrait/GetReturnHint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace IvozDevTools\EntityGeneratorBundle\Doctrine\EntityTrait;

class GetReturnHint
{
private array $useStatements;

public function __construct(
array $useStatements
)
{
$this->useStatements = $useStatements;
}

public function execute(string $returnHint): string
{
$similarInterfaces = false;

foreach (array_keys($this->useStatements) as $fqdn) {
$parts = explode('\\', $fqdn);
$lastValue = end($parts);

if ($lastValue === $returnHint) {
$similarInterfaces = true;
break;
}
}

if ($similarInterfaces) {
$returnHint = 'static';
}

return $returnHint;
}
}
13 changes: 11 additions & 2 deletions EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ class Remover implements CodeGeneratorUnitInterface
protected $classMetadata;
protected $visibility;

protected $returnHintCallable;
protected GetReturnHint $getReturnHint;

public function __construct(
string $propertyName,
string $type,
bool $isNullable,
$classMetadata,
GetReturnHint $getReturnHint,
array $commentLines = [],
array $columnOptions = [],
string $visibility = 'protected'
string $visibility = 'protected',
?callable $returnHintCallable = null
) {
$this->propertyName = $propertyName;
$this->type = $type;
Expand All @@ -31,6 +36,8 @@ public function __construct(
$this->columnOptions = $columnOptions;
$this->classMetadata = $classMetadata;
$this->visibility = $visibility;
$this->returnHintCallable = $returnHintCallable;
$this->getReturnHint = $getReturnHint;
}

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

$methodName = 'remove' . ucfirst($singularProperty);
$fqdnSegments = explode('\\', $this->classMetadata->name);
$returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface';
$returnHint = $this->getReturnHint->execute(
$fqdnSegments[count($fqdnSegments) -2] . 'Interface'
);

$response = [];
$response[] = sprintf(
Expand Down
8 changes: 7 additions & 1 deletion EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ class Replacer implements CodeGeneratorUnitInterface
protected $classMetadata;
protected $visibility;

protected GetReturnHint $getReturnHint;

public function __construct(
string $propertyName,
string $type,
bool $isNullable,
$classMetadata,
GetReturnHint $getReturnHint,
array $commentLines = [],
array $columnOptions = [],
string $visibility = 'protected'
Expand All @@ -31,6 +34,7 @@ public function __construct(
$this->columnOptions = $columnOptions;
$this->classMetadata = $classMetadata;
$this->visibility = $visibility;
$this->getReturnHint = $getReturnHint;
}

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

$methodName = 'replace' . $camelCaseProperty;
$fqdnSegments = explode('\\', $this->classMetadata->name);
$returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface';
$returnHint = $this->getReturnHint->execute(
$fqdnSegments[count($fqdnSegments) -2] . 'Interface'
);

$response = [];
$response[] = '/**';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,13 @@ public function addSetter(
array $columnOptions = [],
string $visibility = 'protected'
) {
$getReturnHint = new GetReturnHint($this->useStatements);
$this->methods[] = new Adder(
$propertyName,
$type,
$isNullable,
$classMetadata,
$getReturnHint,
$commentLines,
$columnOptions,
$visibility
Expand All @@ -263,6 +265,7 @@ public function addSetter(
$type,
$isNullable,
$classMetadata,
$getReturnHint,
$commentLines,
$columnOptions,
$visibility
Expand All @@ -273,6 +276,7 @@ public function addSetter(
$type,
$isNullable,
$classMetadata,
$getReturnHint,
$commentLines,
$columnOptions,
$visibility
Expand Down

0 comments on commit 4e52397

Please sign in to comment.