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 28, 2024
1 parent 9a9f237 commit 585600a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
8 changes: 6 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 $returnHintCallable;

public function __construct(
string $propertyName,
string $type,
bool $isNullable,
$classMetadata,
array $commentLines = [],
array $columnOptions = [],
string $visibility = 'protected'
string $visibility = 'protected',
callable $returnHintCallable

Check failure on line 28 in EntityGeneratorBundle/Doctrine/EntityTrait/Adder.php

View workflow job for this annotation

GitHub Actions / build

Deprecated in PHP 8.0: Required parameter $returnHintCallable follows optional parameter $visibility.
) {
$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->returnHintCallable = $returnHintCallable;
}

public function toString(string $nlLeftPad = ''): string
Expand All @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ class Remover implements CodeGeneratorUnitInterface
protected $classMetadata;
protected $visibility;

protected $returnHintCallable;

public function __construct(
string $propertyName,
string $type,
bool $isNullable,
$classMetadata,
array $commentLines = [],
array $columnOptions = [],
string $visibility = 'protected'
string $visibility = 'protected',
callable $returnHintCallable

Check failure on line 28 in EntityGeneratorBundle/Doctrine/EntityTrait/Remover.php

View workflow job for this annotation

GitHub Actions / build

Deprecated in PHP 8.0: Required parameter $returnHintCallable follows optional parameter $visibility.
) {
$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->returnHintCallable = $returnHintCallable;
}

public function toString(string $nlLeftPad = ''): string
Expand All @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ class Replacer implements CodeGeneratorUnitInterface
protected $classMetadata;
protected $visibility;

protected $returnHintCallable;

public function __construct(
string $propertyName,
string $type,
bool $isNullable,
$classMetadata,
array $commentLines = [],
array $columnOptions = [],
string $visibility = 'protected'
string $visibility = 'protected',
callable $returnHintCallable

Check failure on line 28 in EntityGeneratorBundle/Doctrine/EntityTrait/Replacer.php

View workflow job for this annotation

GitHub Actions / build

Deprecated in PHP 8.0: Required parameter $returnHintCallable follows optional parameter $visibility.
) {
$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->returnHintCallable = $returnHintCallable;
}

public function toString(string $nlLeftPad = ''): string
Expand All @@ -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[] = '/**';
Expand Down
35 changes: 29 additions & 6 deletions EntityGeneratorBundle/Doctrine/EntityTrait/TraitManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -255,7 +275,8 @@ public function addSetter(
$classMetadata,
$commentLines,
$columnOptions,
$visibility
$visibility,
[$this, 'getReturnHint']
);
$this->methods[] = new Remover(
Expand All @@ -265,7 +286,8 @@ public function addSetter(
$classMetadata,
$commentLines,
$columnOptions,
$visibility
$visibility,
[$this, 'getReturnHint']
);
$this->methods[] = new Replacer(
Expand All @@ -275,7 +297,8 @@ public function addSetter(
$classMetadata,
$commentLines,
$columnOptions,
$visibility
$visibility,
[$this, 'getReturnHint']
);
}
Expand All @@ -285,7 +308,7 @@ public function addProperty(
string $columnName,
string $fkFqdn,
array $comments = [],
$defaultValue = null,
$defaultValue = null,
bool $required = false,
bool $isCollection = true
) {
Expand Down

0 comments on commit 585600a

Please sign in to comment.