diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php b/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php index b905527..ec33eb9 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php @@ -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; @@ -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 @@ -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( diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/GetReturnHint.php b/EntityGeneratorBundle/Doctrine/EntityTrait/GetReturnHint.php new file mode 100644 index 0000000..9abfc94 --- /dev/null +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/GetReturnHint.php @@ -0,0 +1,36 @@ +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; + } +} \ No newline at end of file diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php b/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php index 6cf0d60..d4ba441 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php @@ -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; @@ -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 @@ -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( diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php b/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php index 70f88f1..28cff1a 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php @@ -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' @@ -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 @@ -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[] = '/**'; diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php b/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php index 5bc5aeb..675e76b 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php @@ -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 @@ -263,6 +265,7 @@ public function addSetter( $type, $isNullable, $classMetadata, + $getReturnHint, $commentLines, $columnOptions, $visibility @@ -273,6 +276,7 @@ public function addSetter( $type, $isNullable, $classMetadata, + $getReturnHint, $commentLines, $columnOptions, $visibility