From 27331e24ded809c730024e06e85bed7bdd0ac266 Mon Sep 17 00:00:00 2001 From: Roger Batista Date: Wed, 28 Aug 2024 15:45:29 +0200 Subject: [PATCH] Fixed trait manipulator to avoid duplicate interface calls --- .../Doctrine/EntityTrait/Adder.php | 8 +++-- .../Doctrine/EntityTrait/Remover.php | 8 +++-- .../Doctrine/EntityTrait/Replacer.php | 8 +++-- .../Doctrine/EntityTrait/TraitManipulator.php | 29 +++++++++++++++++-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php b/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php index b905527..249ae0a 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php @@ -15,6 +15,8 @@ class Adder implements CodeGeneratorUnitInterface protected $classMetadata; protected $visibility; + protected $returnHintCallable; + public function __construct( string $propertyName, string $type, @@ -22,7 +24,8 @@ public function __construct( $classMetadata, array $commentLines = [], array $columnOptions = [], - string $visibility = 'protected' + string $visibility = 'protected', + callable $returnHintCallable ) { $this->propertyName = $propertyName; $this->type = $type; @@ -31,6 +34,7 @@ public function __construct( $this->columnOptions = $columnOptions; $this->classMetadata = $classMetadata; $this->visibility = $visibility; + $this->returnHintCallable = $returnHintCallable; } public function toString(string $nlLeftPad = ''): string @@ -41,7 +45,7 @@ public function toString(string $nlLeftPad = ''): string $methodName = 'add' . ucfirst($singularProperty); $fqdnSegments = explode('\\', $this->classMetadata->name); - $returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface'; + $returnHint = ($this->returnHintCallable)($fqdnSegments[count($fqdnSegments) -2] . 'Interface'); $response = []; $response[] = sprintf( diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php b/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php index 6cf0d60..dfe8d7c 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php @@ -15,6 +15,8 @@ class Remover implements CodeGeneratorUnitInterface protected $classMetadata; protected $visibility; + protected $returnHintCallable; + public function __construct( string $propertyName, string $type, @@ -22,7 +24,8 @@ public function __construct( $classMetadata, array $commentLines = [], array $columnOptions = [], - string $visibility = 'protected' + string $visibility = 'protected', + callable $returnHintCallable ) { $this->propertyName = $propertyName; $this->type = $type; @@ -31,6 +34,7 @@ public function __construct( $this->columnOptions = $columnOptions; $this->classMetadata = $classMetadata; $this->visibility = $visibility; + $this->returnHintCallable = $returnHintCallable; } public function toString(string $nlLeftPad = ''): string @@ -40,7 +44,7 @@ public function toString(string $nlLeftPad = ''): string $methodName = 'remove' . ucfirst($singularProperty); $fqdnSegments = explode('\\', $this->classMetadata->name); - $returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface'; + $returnHint = ($this->returnHintCallable)($fqdnSegments[count($fqdnSegments) -2] . 'Interface'); $response = []; $response[] = sprintf( diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php b/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php index 70f88f1..9075c6e 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php @@ -15,6 +15,8 @@ class Replacer implements CodeGeneratorUnitInterface protected $classMetadata; protected $visibility; + protected $returnHintCallable; + public function __construct( string $propertyName, string $type, @@ -22,7 +24,8 @@ public function __construct( $classMetadata, array $commentLines = [], array $columnOptions = [], - string $visibility = 'protected' + string $visibility = 'protected', + callable $returnHintCallable ) { $this->propertyName = $propertyName; $this->type = $type; @@ -31,6 +34,7 @@ public function __construct( $this->columnOptions = $columnOptions; $this->classMetadata = $classMetadata; $this->visibility = $visibility; + $this->returnHintCallable = $returnHintCallable; } public function toString(string $nlLeftPad = ''): string @@ -45,7 +49,7 @@ public function toString(string $nlLeftPad = ''): string $methodName = 'replace' . $camelCaseProperty; $fqdnSegments = explode('\\', $this->classMetadata->name); - $returnHint = $fqdnSegments[count($fqdnSegments) -2] . 'Interface'; + $returnHint = ($this->returnHintCallable)($fqdnSegments[count($fqdnSegments) -2] . 'Interface'); $response = []; $response[] = '/**'; diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php b/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php index 5bc5aeb..4253617 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php @@ -239,6 +239,26 @@ public function addGetter(string $propertyName, $returnType, bool $isReturnTypeN ); } + public function getReturnHint(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; + } + public function addSetter( string $propertyName, $type, @@ -255,7 +275,8 @@ public function addSetter( $classMetadata, $commentLines, $columnOptions, - $visibility + $visibility, + [$this, 'getReturnHint'] ); $this->methods[] = new Remover( @@ -265,7 +286,8 @@ public function addSetter( $classMetadata, $commentLines, $columnOptions, - $visibility + $visibility, + [$this, 'getReturnHint'] ); $this->methods[] = new Replacer( @@ -275,7 +297,8 @@ public function addSetter( $classMetadata, $commentLines, $columnOptions, - $visibility + $visibility, + [$this, 'getReturnHint'] ); }