Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ivoz dev tools regenerator trait #31

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading