-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Argument objects #19277
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7bd4ac5
Test
theofidry e99a90b
Add tests
theofidry e64e999
Address comments
theofidry 4884a2e
f1
theofidry f361e52
fix tests
theofidry f46a176
Apply patch
theofidry 93608dc
Add deprecation message
theofidry d4cdb00
fix CS
theofidry 5556fa5
fix
theofidry 3fe9802
revert CS
theofidry e437e04
fix reflection type
theofidry 7b5d55d
Prevent BC in instantiateObject
dunglas 98bcb91
Merge pull request #1 from dunglas/theofidry-feature/param-object
theofidry 988eba1
fix tests
theofidry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
revert CS
- Loading branch information
commit 3fe9802eadde75e6252859275e39832451dc56c6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,11 +330,7 @@ protected function instantiateComplexObject(array &$data, $class, array &$contex | |
|
||
$params = array_merge($params, $data[$paramName]); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { | ||
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { | ||
$parameterData = $data[$key]; | ||
if (null !== $constructorParameter->getClass()) { | ||
$parameterData = $this->serializer->deserialize($parameterData, $constructorParameter->getClass()->getName(), $format, $context); | ||
|
@@ -343,30 +339,24 @@ protected function instantiateComplexObject(array &$data, $class, array &$contex | |
// Don't run set for a parameter passed to the constructor | ||
$params[] = $parameterData; | ||
unset($data[$key]); | ||
|
||
continue; | ||
} | ||
|
||
if ($constructorParameter->isDefaultValueAvailable()) { | ||
} elseif ($constructorParameter->isDefaultValueAvailable()) { | ||
$params[] = $constructorParameter->getDefaultValue(); | ||
|
||
continue; | ||
} else { | ||
throw new RuntimeException( | ||
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. Why removing the unset? 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. bad copy/paste, should be kept |
||
sprintf( | ||
'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', | ||
$class, | ||
$constructorParameter->name | ||
) | ||
); | ||
} | ||
|
||
throw new RuntimeException( | ||
sprintf( | ||
'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', | ||
$class, | ||
$constructorParameter->name | ||
) | ||
); | ||
} | ||
|
||
if ($constructor->isConstructor()) { | ||
return $reflectionClass->newInstanceArgs($params); | ||
} else { | ||
return $constructor->invokeArgs(null, $params); | ||
} | ||
|
||
return $constructor->invokeArgs(null, $params); | ||
} | ||
|
||
return new $class(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
ReflectionParameter::getType()
should be used when available imo and exceptions catched in case the type hinted class doesn't exist.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 personally find it easier to check check if the class is null or not in this case
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 agree but the type hint may be on a class not loadable. In this case, the exception thrown should be catched and the default value applied.
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.
could you elaborate? On which scenario may it happen? Not that I'm not trusting you, but if that's the case I would like to add a test for it :)
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.
In case you use a type hint with a class from a third party library which is not loaded because you don't need it for example.
And the advantage of getType() is that it doesn't need to load the class, so would work anyway.
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.
Ah I see, fair enough will change that then