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

Update ConstructableFromArrayTrait.php #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

plehanov
Copy link

@plehanov plehanov commented Mar 5, 2020

Eliminates problems that arise when classes appear instead of simple parameter types

Eliminates problems that arise when classes appear instead of simple parameter types
Copy link
Owner

@hgraca hgraca left a comment

Choose a reason for hiding this comment

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

Tkx for the PR.

However, I see some issues in your proposed changes.

Also, the idea is that the array contains the class arguments, ready to be injected. So if a parameter is a class, the array should contain the instantiated class already. Otherwise we might end up creating a DIC because not always the class can be instantiated with a single argument.

Nevertheless, if you feel this is a valid use case, can you please make a test for it?
I understand this trait is not tested explicitly, but it is tested through other use cases. Since your proposal is handling an edge case that does not exist in this codebase, we need an explicit test for it.

:)

if ($reflectionParameter->isVariadic() && \is_array($argument)) {
$class = $reflectionParameter->getClass();
if ($class && ($className = $class->getName()) && class_exists($className)) {
$argumentList[] = new $className($argument);
Copy link
Owner

Choose a reason for hiding this comment

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

It seems like, at this point, $argument does not exist, right?

You removed line 48, which gets the argument.
Also, what happens if the argument is already an instance of the specified class? It looks like your code will try to instantiate the class with and instance of that same class as the argument.
And what happens if that class needs to be instantiated with several arguments?
It feels like we would be going in the direction of creating a DIC...

Copy link
Author

Choose a reason for hiding this comment

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

I share your thoughts. Apparently, without DIС, unfortunately can not do

Copy link
Author

Choose a reason for hiding this comment

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

On the other hand, these are models. They cannot have initiation requiring a DIC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants