Skip to content

Commit

Permalink
enhanced entity generator hints
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadariaga committed Nov 30, 2023
1 parent ccbee70 commit 8b9e9b6
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 75 deletions.
30 changes: 14 additions & 16 deletions EntityGeneratorBundle/Doctrine/Dto/DtoManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function addEntityField(
$embedded
);
if ($nullable) {
if ($nullable || $isPk) {
$paramDoc = '@param ' . $typeHint . '|null $' . $propertyName;
} else {
$paramDoc = '@param ' . $typeHint . ' $' . $propertyName;
Expand Down Expand Up @@ -269,16 +269,15 @@ public function addSetter(
$propertyName,
$type,
$isNullable,
$classMetadata,
$commentLines,
$columnOptions,
$visibility
);
}
public function addIdSetter(
string $propertyName,
$type,
$targetClass,
$hint,
bool $isNullable,
$classMetadata,
array $commentLines = [],
Expand All @@ -287,11 +286,8 @@ public function addIdSetter(
) {
$this->methods[] = new FkSetter(
$propertyName,
$type,
$isNullable,
$classMetadata,
$commentLines,
$columnOptions,
$targetClass,
$hint,
$visibility
);
}
Expand Down Expand Up @@ -414,12 +410,6 @@ private function addSingularRelation(BaseSingleRelation $relation, $classMetadat
);
////
$this->addIdSetter(
$relation->getPropertyName(),
$typeHint,
true,
$classMetadata,
);
$targetClassName = substr(
$relation->getTargetClassName(),
Expand All @@ -437,6 +427,14 @@ private function addSingularRelation(BaseSingleRelation $relation, $classMetadat
$targetPkField["type"]
);
$this->addIdSetter(
$relation->getPropertyName(),
$typeHint,
$targetPkHint,
true,
$classMetadata,
);
$this->addIdGetter(
$relation->getPropertyName(),
$relation->getCustomReturnType() ?: $targetPkHint,
Expand Down Expand Up @@ -474,7 +472,7 @@ private function addCollectionRelation(BaseCollectionRelation $relation, $classM
);
// Setter
$setterHint = '@param ' . $typeHint . '[] | null';
$setterHint = '@param ' . $typeHint . '[] | null $' . $relation->getPropertyName();
$setterComments = [
$setterHint,
Expand Down
7 changes: 7 additions & 0 deletions EntityGeneratorBundle/Doctrine/Dto/DtoRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IvozDevTools\EntityGeneratorBundle\Doctrine\Dto;

use Doctrine\ORM\Mapping\ClassMetadata;
use IvozDevTools\EntityGeneratorBundle\Doctrine\EntityTypeTrait;
use IvozDevTools\EntityGeneratorBundle\Doctrine\ManipulatorInterface;
use IvozDevTools\EntityGeneratorBundle\Generator;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
Expand All @@ -18,6 +19,8 @@
*/
final class DtoRegenerator
{
use EntityTypeTrait;

private $fileManager;
private $generator;
private $doctrineHelper;
Expand Down Expand Up @@ -146,6 +149,10 @@ private function getClassTemplate(
);
}

$variables['pk_type_hint'] = $this->getEntityTypeHintByMetadata(
$metadata
);

return [
$variables['relative_path'],
$this->fileManager->parseTemplate(
Expand Down
39 changes: 15 additions & 24 deletions EntityGeneratorBundle/Doctrine/Dto/DtoSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,27 @@

class DtoSetter implements CodeGeneratorUnitInterface
{
protected $propertyName;
protected $type;
protected $isNullable;
protected $comments = [];
protected $columnOptions = [];
protected $classMetadata;
protected $visibility;

public function __construct(
string $propertyName,
?string $type,
bool $isNullable,
$classMetadata,
array $commentLines = [],
array $columnOptions = [],
string $visibility = 'protected'
private string $propertyName,
private ?string $type,
private bool $isNullable,
private array $comments = [],
private string $visibility = 'protected'
) {
$this->propertyName = $propertyName;
$this->type = $type;
$this->isNullable = $isNullable;
$this->comments = $commentLines;
$this->columnOptions = $columnOptions;
$this->classMetadata = $classMetadata;
$this->visibility = $visibility;
}

public function toString(string $nlLeftPad = ''): string
{
$response = [];
$showComments = is_null($this->type) || $this->type === 'array';
if (count($this->comments) && $showComments) {
$response = ['/**'];
foreach ($this->comments as $comment) {
$response[] = ' * ' . $comment;
}
$response[] = ' */';
}

if ($this->isNullable && $this->type) {
$typeHint = strpos($this->type, '|') !== false
? 'null|' . $this->type
Expand All @@ -48,8 +41,6 @@ public function toString(string $nlLeftPad = ''): string
}

$methodName = 'set' . Str::asCamelCase($this->propertyName);

$response = [];
$response[] = sprintf(
'%s function %s(%s%s): %s',
$this->visibility,
Expand Down
15 changes: 12 additions & 3 deletions EntityGeneratorBundle/Doctrine/Dto/FkSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@
use IvozDevTools\EntityGeneratorBundle\Doctrine\Setter;
use Symfony\Bundle\MakerBundle\Str;

class FkSetter extends Setter
class FkSetter
{
public function __construct(
private string $propertyName,
private ?string $targetClass,
private string $typeHint,
private string $visibility = 'protected'
) {
}

public function toString(string $nlLeftPad = ''): string
{
$methodName = 'set' . Str::asCamelCase($this->propertyName);

$response = [];
$response[] = sprintf(
'%s function %sId($id): %s',
'%s function %sId(?%s $id): %s',
$this->visibility,
$methodName,
$this->typeHint,
'static'
);

$response[] = '{';
$response[] = ' $value = !is_null($id)';
$response[] = " ? new " . $this->type . '($id)';
$response[] = " ? new " . $this->targetClass . '($id)';
$response[] = ' : null;';
$response[] = '';
$response[] = ' return $this->' . $methodName . '($value);';
Expand Down
38 changes: 13 additions & 25 deletions EntityGeneratorBundle/Doctrine/Entity/EntityRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IvozDevTools\EntityGeneratorBundle\Doctrine\Entity;

use Doctrine\ORM\Mapping\ClassMetadata;
use IvozDevTools\EntityGeneratorBundle\Doctrine\EntityTypeTrait;
use IvozDevTools\EntityGeneratorBundle\Doctrine\ManipulatorInterface;
use IvozDevTools\EntityGeneratorBundle\Generator;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
Expand All @@ -15,6 +16,8 @@
*/
final class EntityRegenerator
{
use EntityTypeTrait;

private $fileManager;
private $generator;
private $doctrineHelper;
Expand All @@ -29,11 +32,12 @@ public function __construct(
$this->doctrineHelper = $doctrineHelper;
}

public function makeAbstractEntity($classMetadata)
public function makeAbstractEntity($classMetadata, $parentMetadata)
{
[$classPath, $content] = $this->getClassTemplate(
$classMetadata,
'doctrine/AbstractEntity.tpl.php'
'doctrine/AbstractEntity.tpl.php',
$parentMetadata,
);

$manipulator = $this->createClassManipulator(
Expand All @@ -52,28 +56,6 @@ public function makeAbstractEntity($classMetadata)
);
}

public function makeEmptyInterface($classMetadata)
{
if (class_exists($classMetadata->name)) {
return;
}

[$classPath, $content] = $this->getClassTemplate(
$classMetadata,
'doctrine/EntityInterface.tpl.php'
);

$manipulator = $this->createClassManipulator(
$classPath,
$content
);

$this->dumpFile(
$classPath,
$manipulator
);
}

public function makeEntity($classMetadata)
{
try {
Expand Down Expand Up @@ -108,7 +90,8 @@ private function dumpFile(string $classPath, ManipulatorInterface $manipulator):

private function getClassTemplate(
ClassMetadata $metadata,
$templateName
$templateName,
ClassMetadata $parentMetadata = null,
): array
{
[$path, $variables] = $this->generator->generateClassContentVariables(
Expand All @@ -127,6 +110,11 @@ private function getClassTemplate(
);
}

$pkTypeHint = $this->getEntityTypeHintByMetadata(
$parentMetadata ?? $metadata
);
$variables['pk_type_hint'] = $pkTypeHint ?? 'int';

return [
$variables['relative_path'],
$this->fileManager->parseTemplate(
Expand Down
18 changes: 18 additions & 0 deletions EntityGeneratorBundle/Doctrine/EntityTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@

namespace IvozDevTools\EntityGeneratorBundle\Doctrine;

use Doctrine\ORM\Mapping\ClassMetadata;

trait EntityTypeTrait
{
private function getEntityTypeHintByMetadata(ClassMetadata $metadata)
{
$identifier = $metadata->getIdentifier();
if (empty($identifier)) {
return null;
}

$pkField = $metadata->getFieldMapping(
current($identifier)
);

return $this->getEntityTypeHint(
$pkField["type"]
);
}

private function getEntityTypeHint($doctrineType)
{
switch ($doctrineType) {
Expand Down
7 changes: 6 additions & 1 deletion EntityGeneratorBundle/Doctrine/Regenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public function regenerateEntities(string $classOrNamespace)
$classMetadata
);
} elseif ($isMappedSuperclass) {
$this->entityRegenerator->makeAbstractEntity($classMetadata);

try {
$parentMetadata = $this->getMetadata(substr($classOrNamespace, 0, strlen('Abstract') * -1));
} catch (\Exception $e) {}

$this->entityRegenerator->makeAbstractEntity($classMetadata, $parentMetadata ?? null);
$this->dtoRegenerator->makeAbstractDto($classMetadata);
} else {
$this->interfaceRegenerator->makeEmptyInterface($classMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ abstract class <?= $class_name." implements DataTransferObjectInterface\n" ?>

/*__dto_attributes*/

/**
* @param string|int|null $id
*/
public function __construct($id = null)
public function __construct(?<?= $pk_type_hint ?> $id = null)
{
$this->setId($id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ protected function sanitizeValues(): void
{
}

public static function createDto(string|int|null $id = null): <?= $parent_class_name ?>Dto
/**
* @param <?= $pk_type_hint ?> | null $id
*/
public static function createDto($id = null): <?= $parent_class_name ?>Dto
{
return new <?= $parent_class_name ?>Dto($id);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"doctrine/doctrine-bundle": "^2.0",
"symfony/console": "^5.1",
"symfony/maker-bundle": "<=1.38",
"irontec/ivoz-core": "^4.17.0"
"irontec/ivoz-core": "^4.18.0"
},
"require-dev": {
"phpstan/phpstan": "^1.0"
Expand Down

0 comments on commit 8b9e9b6

Please sign in to comment.