-
-
Notifications
You must be signed in to change notification settings - Fork 412
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
[make:entity] fix multiple and nullable enums #1584
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+136
to
138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity to my suggestion above, removing that use statement call and moving this one outside of the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will override |
||||||||||||||
} 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)); | ||||||||||||||
} | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
}), | ||
]; | ||
Comment on lines
+739
to
+760
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if this test is relevant |
||
} | ||
|
||
/** @param array<string, mixed> $data */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can eliminate the
else
conditional in thisif
statement by movingaddUserStatement...()
outside and after `if ('array' === $typeHint) is called.We're calling the add use statement method regardless if
array
===$typeHint
correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're calling it in both cases, but if
array === $typeHint
, we don't want the returned string; it's just to automate the use statement.