From 585600ac0be1e0a4a8bf8ac75e10482f213eec55 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 | 35 +++++++++++++++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php b/EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php index b905527..3120f5f 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..20d856d 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..c9d2666 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..6157bf3 100644 --- a/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php +++ b/EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php @@ -239,11 +239,31 @@ 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, + $type, bool $isNullable, - $classMetadata, + $classMetadata, array $commentLines = [], array $columnOptions = [], string $visibility = 'protected' @@ -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'] ); } @@ -285,7 +308,7 @@ public function addProperty( string $columnName, string $fkFqdn, array $comments = [], - $defaultValue = null, + $defaultValue = null, bool $required = false, bool $isCollection = true ) {