Skip to content

[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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @template TData
*/
interface NormalizerInterface
{
/**
* Normalizes data into a set of arrays/scalars.
*
* @param mixed $data Data to normalize
* @param TData $data Data to normalize
* @param string|null $format Format the normalization result will be encoded as
* @param array $context Context options for the normalizer
*
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

why keep mixed?

Copy link
Contributor

@uuf6429 uuf6429 Jan 7, 2025

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).

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 guess you could argue that mixed already contains TData as well, so we might as well just keep mixed as a type.

* @param string|null $format The format being (de-)serialized from or into
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool;

Expand Down
Loading