From cbc2be8ba867277e3b3b1afaa1300211f27cc6a1 Mon Sep 17 00:00:00 2001 From: Rhodri Pugh Date: Fri, 21 Sep 2018 14:11:40 +0200 Subject: [PATCH] [Serializer] deprecated normalizers and encoders who dont implement the base interfaces --- src/Symfony/Component/Serializer/CHANGELOG.md | 6 +++++ .../Component/Serializer/Serializer.php | 14 ++++++++++++ .../Serializer/Tests/SerializerTest.php | 22 +++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 2760900907..65f413625e 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -13,6 +13,12 @@ CHANGELOG the format and the context in a name converter * the `AbstractNormalizer::handleCircularReference()` method will have two new `$format` and `$context` arguments in version 5.0, not defining them is deprecated + * deprecated creating a `Serializer` with normalizers which do not implement + either `NormalizerInterface` or `DenormalizerInterface` + * deprecated creating a `Serializer` with normalizers which do not implement + either `NormalizerInterface` or `DenormalizerInterface` + * deprecated creating a `Serializer` with encoders which do not implement + either `EncoderInterface` or `DecoderInterface` 4.1.0 ----- diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index 27167fc6d6..f7daa6f73f 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -64,6 +64,10 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface private $denormalizerCache = array(); private $normalizerCache = array(); + /** + * @param (NormalizerInterface|DenormalizerInterface)[] $normalizers + * @param (EncoderInterface|DecoderInterface)[] $encoders + */ public function __construct(array $normalizers = array(), array $encoders = array()) { foreach ($normalizers as $normalizer) { @@ -78,6 +82,11 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface if ($normalizer instanceof NormalizerAwareInterface) { $normalizer->setNormalizer($this); } + + if (!($normalizer instanceof NormalizerInterface || $normalizer instanceof DenormalizerInterface)) { + @trigger_error(\sprintf('Passing normalizers ("%s") which do not implement either "%s" or "%s" has been deprecated since Symfony 4.2.', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class), E_USER_DEPRECATED); + // throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class)); + } } $this->normalizers = $normalizers; @@ -93,6 +102,11 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface if ($encoder instanceof EncoderInterface) { $realEncoders[] = $encoder; } + + if (!($encoder instanceof EncoderInterface || $encoder instanceof DecoderInterface)) { + @trigger_error(\sprintf('Passing encoders ("%s") which do not implement either "%s" or "%s" has been deprecated since Symfony 4.2.', \get_class($encoder), EncoderInterface::class, DecoderInterface::class), E_USER_DEPRECATED); + // throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), EncoderInterface::class, DecoderInterface::class)); + } } $this->encoder = new ChainEncoder($realEncoders); $this->decoder = new ChainDecoder($decoders); diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 6d2159aad3..72344cf72a 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -55,6 +55,24 @@ class SerializerTest extends TestCase $this->assertInstanceOf('Symfony\Component\Serializer\Encoder\DecoderInterface', $serializer); } + /** + * @expectedDeprecation Passing normalizers ("stdClass") which do not implement either "Symfony\Component\Serializer\Normalizer\NormalizerInterface" or "Symfony\Component\Serializer\Normalizer\DenormalizerInterface" has been deprecated since Symfony 4.2. + * @group legacy + */ + public function testDeprecationErrorOnInvalidNormalizer() + { + new Serializer(array(new \stdClass())); + } + + /** + * @expectedDeprecation Passing encoders ("stdClass") which do not implement either "Symfony\Component\Serializer\Encoder\EncoderInterface" or "Symfony\Component\Serializer\Encoder\DecoderInterface" has been deprecated since Symfony 4.2. + * @group legacy + */ + public function testDeprecationErrorOnInvalidEncoder() + { + new Serializer(array(), array(new \stdClass())); + } + /** * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException */ @@ -334,7 +352,7 @@ class SerializerTest extends TestCase public function testNormalizerAware() { - $normalizerAware = $this->getMockBuilder(NormalizerAwareInterface::class)->getMock(); + $normalizerAware = $this->getMockBuilder(array(NormalizerAwareInterface::class, NormalizerInterface::class))->getMock(); $normalizerAware->expects($this->once()) ->method('setNormalizer') ->with($this->isInstanceOf(NormalizerInterface::class)); @@ -344,7 +362,7 @@ class SerializerTest extends TestCase public function testDenormalizerAware() { - $denormalizerAware = $this->getMockBuilder(DenormalizerAwareInterface::class)->getMock(); + $denormalizerAware = $this->getMockBuilder(array(DenormalizerAwareInterface::class, DenormalizerInterface::class))->getMock(); $denormalizerAware->expects($this->once()) ->method('setDenormalizer') ->with($this->isInstanceOf(DenormalizerInterface::class));