-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add template parameter to avoid necessity to assert data in normalize #59200
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
base: 7.3
Are you sure you want to change the base?
Conversation
Hey! Thanks for your PR. You are targeting branch "7.3" but it seems your PR description refers to branch "TBH I am not sure about this one 😅". Cheers! Carsonbot |
I already saw that this causes some issues... But instead of fixing all of that now and occupying github action pipelines I'll wait for your feedback to see if you would accept this PR. |
@@ -41,8 +43,8 @@ public function normalize(mixed $data, ?string $format = null, array $context = | ||
/** | ||
* Checks whether the given class is supported for normalization by this normalizer. | ||
* | ||
* @param mixed $data Data to normalize | ||
* @param string|null $format The format being (de-)serialized from or into | ||
* @param TData|mixed $data Data to normalize |
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.
why keep mixed?
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.
For supportsNormalization()
, it might presumably receive all sorts of data (since it's testing for serialization support).
The idea is that (for example) NormalizerInterface<string>
will only normalize()
strings, but still might receive other types to test for serialization (otherwise (NormalizerInterface<string>)->supportsNormalization(123, ...)
will cause a static analysis tool warning, which is undesirable and unintended).
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 guess you could argue that mixed
already contains TData
as well, so we might as well just keep mixed
as a type.
Looking at the normalizers shipped in Symfony, they all verify the passed data to So to me, it sounds like it is a good practice to verify the type in the method itself rather than relying on the return value of |
@wouterj I just checked two normalizers randomly and that doesn't seem to be the case (unless I misunderstood your point):
I don't know how/if psalm/phpstan is set up, but in either case I would have expected a warning that the usage of With this PR (and some small adjustments), static analysis tools should figure out People should still be able to opt-out by using |
Interesting, so we are inconsistent about this in Symfony. See e.g. symfony/src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php Lines 46 to 50 in 23fd2eb
|
I have to admit that this is kind of an edge case. It is not absolutely safe to just assume the type passed to I.e. this is probably how Symfony is calling this under the hood: $valueObjectNormalizer = new ValueObjectNormalizer();
if ($valueObjectNormalizer->supportsNormalization($value)) {
$valueObjectNormalizer->normalize($value);
} But nothing stops a developer from instantiating that manually and call it in the "wrong" way: $valueObjectNormalizer = new ValueObjectNormalizer();
$valueObjectNormalizer->normalize($value); Nevertheless, I would say this is an edge case, and at least for me the current PR would still be a nice addition, since it is closer to how this is actually working under the hood. |
In a project I am working on we are using quite some value objects, and I also want to normalize some of them. Instead of repeating the serialization part for every object, I like to create a separate
ValueObjectNormalizer
(whereby I useValueObject
as a placeholder here and in the below code).We are also using PHPStan with its highest level, and then there are some "issues" with that. The problem is that I always have to add a call to
assert
in thenormalize
function, since the$data
variable is typed as mixed, and whatever I am doing with the passed data PHPStan won't like. This looks something like this:With the change being introduced in this PR, it would be possible to make use of generics in order to avoid this
assert
call, which I think is a bit nicer.What do you think about this? I am also not sure in which branch such a change would have to go, and I am also not sure if you consider this a breaking change.