Skip to content

[Serializer] Allow to use easily static constructors #19137

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 1 commit into from
Jun 29, 2016

Conversation

GuilhemN
Copy link
Contributor

Q A
Branch? "master"
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #19027 (comment)
License MIT
Doc PR -

This PR allows to simply use static constructors to instantiate objects with the serializer by extending the default normalizers.

*
* @return \ReflectionMethod|null
*/
protected function getConstructor($class, \ReflectionClass $reflectionClass)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should we pass to this method all the arguments passed to instantiateObject ?

Copy link
Member

Choose a reason for hiding this comment

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

I think that makes sense in case you want to support different factory methods depending on the input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed, updated :)


public function testObjectWithStaticConstructor()
{
$normalizer = $this->getMockBuilder(ObjectNormalizer::class)
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why we would need too use a mock here. Why not using the ObjectNormalizer directly?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, what I mean is create a custom normalizer to be used in the test that properly implements getConstructor().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A mock allows to test the abstract class but i have no strong opinion about it and a fixture normalizer is fine for me too.

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer a fixture normalizer too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated :)

@@ -318,7 +335,11 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
}
}

return $reflectionClass->newInstanceArgs($params);
if ($constructor->isConstructor()) {
Copy link
Contributor

@ro0NL ro0NL Jun 26, 2016

Choose a reason for hiding this comment

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

This is breaking and should also be called if $constructor is null. However that throws an exception if the class has no constructor, so what about calling newInstanceWithoutConstructor then?, being the absolute fallback.

Copy link
Member

Choose a reason for hiding this comment

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

if $constructor is null, this code wouldn't be reached because of the if statement on line 303.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hehe. Totally overlooked indenting.

@dunglas
Copy link
Member

dunglas commented Jun 27, 2016

👍

1 similar comment
@xabbuh
Copy link
Member

xabbuh commented Jun 27, 2016

👍

return $reflectionClass->newInstanceArgs($params);
if ($constructor->isConstructor()) {
return $reflectionClass->newInstanceArgs($params);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

There isn't any sense for this else.

Copy link
Contributor Author

@GuilhemN GuilhemN Jun 28, 2016

Choose a reason for hiding this comment

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

IMO it's harder to understand this 2 lines without it.

@fabpot
Copy link
Member

fabpot commented Jun 29, 2016

Thank you @Ener-Getick.

@fabpot fabpot merged commit 9be6484 into symfony:master Jun 29, 2016
fabpot added a commit that referenced this pull request Jun 29, 2016
…Ener-Getick)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[Serializer] Allow to use easily static constructors

| Q             | A
| ------------- | ---
| Branch?       | "master"
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19027 (comment)
| License       | MIT
| Doc PR        | -

This PR allows to simply use static constructors to instantiate objects with the serializer by extending the default normalizers.

Commits
-------

9be6484 [Serializer] Allow to use easily static constructors
@GuilhemN GuilhemN deleted the SERIALIZER branch June 29, 2016 07:15
@fabpot fabpot mentioned this pull request Oct 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants