Skip to content

Commit

Permalink
bug #1584 [make:entity] fix mulitiple and nullable enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Fan2Shrek authored Aug 29, 2024
1 parent a64783e commit 207ed9c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,23 @@ public function addEntityField(ClassProperty $mapping): void

$defaultValue = null;
$commentLines = [];
if ('array' === $typeHint && !$nullable) {
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
if (null !== $mapping->enumType) {

if (null !== $mapping->enumType) {
if ('array' === $typeHint) {
// still need to add the use statement
$this->addUseStatementIfNecessary($mapping->enumType);

$commentLines = [\sprintf('@return %s[]', Str::getShortClassName($mapping->enumType))];
if ($nullable) {
$commentLines[0] = \sprintf('%s|null', $commentLines[0]);
} else {
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
}
} else {
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);
}
} elseif (null !== $mapping->enumType) {
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);
} elseif ('array' === $typeHint && !$nullable) {
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
} elseif ($typeHint && '\\' === $typeHint[0] && false !== strpos($typeHint, '\\', 1)) {
$typeHint = $this->addUseStatementIfNecessary(substr($typeHint, 1));
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,29 @@ public function getTestDetails(): \Generator
$this->runEntityTest($runner);
}),
];

yield 'it_creates_a_new_class_with_enum_field_multiple_and_nullable' => [$this->createMakeEntityTest()
->run(function (MakerTestRunner $runner) {
$this->copyEntity($runner, 'Enum/Role-basic.php');

$runner->runMaker([
// entity class name
'User',
// add additional field
'role',
'enum',
'App\\Entity\\Enum\\Role',
// multiple
'y',
// nullable
'y',
// finish adding fields
'',
]);

$this->runEntityTest($runner);
}),
];
}

/** @param array<string, mixed> $data */
Expand Down

0 comments on commit 207ed9c

Please # to comment.