Skip to content

Commit

Permalink
feat: generate only typed properties and public properties for entity
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Feb 22, 2023
1 parent 6c20721 commit 6e8e005
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 43 deletions.
10 changes: 1 addition & 9 deletions src/Component/Generator/Definition/CollectionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,9 @@ public function generate(Definition $loaderResult): void
}

$phpDoc = '/**
* @method void add(%className% $entity)
* @method void set(string $key, %className% $entity)
* @method %className%[] getIterator()
* @method %className%[] getElements()
* @method %className%|null get(string $key)
* @method %className%|null first()
* @method %className%|null last()
* @extends EntityCollection<%className%>
*/';



$node = $builder
->namespace($loaderResult->namespace)
->addStmt($builder->use(EntityCollection::class))
Expand Down
43 changes: 9 additions & 34 deletions src/Component/Generator/Definition/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,47 +66,22 @@ public function generate(Definition $definition): void
continue;
}

$type = TypeMapping::mapToPhpType($field, true, $definition);

$class->stmts[] = $builder->property($field->getPropertyName())->makeProtected()->setDocComment('/** @var ' . $type . ' */')->getNode();
}

/** @var Field $field */
foreach ($definition->fields as $field) {
if ($field->name === IdField::class) {
continue;
}

$setterMethodName = 'set' . ucfirst($field->getPropertyName());
$getterMethodName = 'get' . ucfirst($field->getPropertyName());

if ($this->hasMethod($classMethods, $setterMethodName) || $this->hasMethod($classMethods, $getterMethodName)) {
continue;
}

$type = TypeMapping::mapToPhpType($field, false, $definition);

if ($field->isNullable()) {
$type = '?' . $type;
}

$method = new ClassMethod(new Identifier($setterMethodName));
$method->flags = Class_::MODIFIER_PUBLIC;
$method->returnType = new Identifier('void');
$method->params = [new Param(new Name('$value'), null, new Name($type))];

$var = new PropertyFetch(new Variable('this'), new Name($field->getPropertyName()));
$method->stmts[] = new Expression(new Assign($var, new Variable('value')));
$node = $builder
->property($field->getPropertyName())
->setType($type)
->makePublic();

$class->stmts[] = $method;

$method = new ClassMethod(new Identifier($getterMethodName));
$method->flags = Class_::MODIFIER_PUBLIC;
$method->returnType = new Identifier($type);

$method->stmts[] = new Return_($var);
if ($field->isNullable()) {
$node = $node->setDefault(null);
}

$class->stmts[] = $method;
$class->stmts[] = $node
->getNode();
}

$printer = new Standard();
Expand Down

0 comments on commit 6e8e005

Please sign in to comment.