Skip to content
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

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

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 this if statement by moving addUserStatement...() outside and after `if ('array' === $typeHint) is called.

We're calling the add use statement method regardless if array === $typeHint correct?

Copy link
Contributor Author

@Fan2Shrek Fan2Shrek Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're calling the add use statement method regardless if array === $typeHint correct?

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.


$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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 if conditional would accomplish the same thing with less code if I'm not mistaken.

Suggested change
} else {
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);
}
}
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will override $typeHint with the enum type even if we use an array, which was the initial problem.
We don't want to use the returned type of addUseStatementIfNecessary if we specify an array.

} 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);
}),
];
Comment on lines +739 to +760
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 */
Expand Down
Loading