Skip to content

Commit f48d955

Browse files
committed
Fix 1580 - Mulitiple and Nullable enum
1 parent e7ba0ec commit f48d955

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/Util/ClassSourceManipulator.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,23 @@ public function addEntityField(ClassProperty $mapping): void
121121

122122
$defaultValue = null;
123123
$commentLines = [];
124-
if ('array' === $typeHint && !$nullable) {
125-
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
126-
if (null !== $mapping->enumType) {
124+
125+
if (null !== $mapping->enumType) {
126+
if ('array' === $typeHint) {
127+
// still need to add the use statement
128+
$this->addUseStatementIfNecessary($mapping->enumType);
129+
127130
$commentLines = [\sprintf('@return %s[]', Str::getShortClassName($mapping->enumType))];
131+
if ($nullable) {
132+
$commentLines[0] .= '|null';
133+
} else {
134+
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
135+
}
136+
} else {
137+
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);
128138
}
129-
} elseif (null !== $mapping->enumType) {
130-
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);
139+
} elseif ('array' === $typeHint && !$nullable) {
140+
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
131141
} elseif ($typeHint && '\\' === $typeHint[0] && false !== strpos($typeHint, '\\', 1)) {
132142
$typeHint = $this->addUseStatementIfNecessary(substr($typeHint, 1));
133143
}

tests/Maker/MakeEntityTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,29 @@ public function getTestDetails(): \Generator
735735
$this->runEntityTest($runner);
736736
}),
737737
];
738+
739+
yield 'it_creates_a_new_class_with_enum_field_multiple_and_nullable' => [$this->createMakeEntityTest()
740+
->run(function (MakerTestRunner $runner) {
741+
$this->copyEntity($runner, 'Enum/Role-basic.php');
742+
743+
$runner->runMaker([
744+
// entity class name
745+
'User',
746+
// add additional field
747+
'role',
748+
'enum',
749+
'App\\Entity\\Enum\\Role',
750+
// multiple
751+
'y',
752+
// nullable
753+
'y',
754+
// finish adding fields
755+
'',
756+
]);
757+
758+
$this->runEntityTest($runner);
759+
}),
760+
];
738761
}
739762

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

0 commit comments

Comments
 (0)