JMSSerializerAdapter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 82
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A serialize() 0 6 1
A deserialize() 0 6 1
C convertContext() 0 35 10
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Serializer;
13
14
use FOS\RestBundle\Context\Context;
15
use JMS\Serializer\Context as JMSContext;
16
use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface;
17
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
18
use JMS\Serializer\DeserializationContext;
19
use JMS\Serializer\SerializationContext;
20
use JMS\Serializer\SerializerInterface;
21
22
/**
23
 * Adapter to plug the JMS serializer into the FOSRestBundle Serializer API.
24
 *
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
final class JMSSerializerAdapter implements Serializer
28
{
29
    /**
30
     * @internal
31
     */
32
    const SERIALIZATION = 0;
33
34
    /**
35
     * @internal
36
     */
37
    const DESERIALIZATION = 1;
38
39
    private $serializer;
40
    private $serializationContextFactory;
41
    private $deserializationContextFactory;
42
43 12
    public function __construct(
44
        SerializerInterface $serializer,
45
        SerializationContextFactoryInterface $serializationContextFactory = null,
46
        DeserializationContextFactoryInterface $deserializationContextFactory = null
47
    ) {
48 12
        $this->serializer = $serializer;
49 12
        $this->serializationContextFactory = $serializationContextFactory;
50 12
        $this->deserializationContextFactory = $deserializationContextFactory;
51 12
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 10
    public function serialize($data, string $format, Context $context): string
57
    {
58 10
        $context = $this->convertContext($context, self::SERIALIZATION);
59
60 10
        return $this->serializer->serialize($data, $format, $context);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function deserialize(string $data, string $type, string $format, Context $context)
67
    {
68 2
        $context = $this->convertContext($context, self::DESERIALIZATION);
69
70 2
        return $this->serializer->deserialize($data, $type, $format, $context);
71
    }
72
73 12
    private function convertContext(Context $context, int $direction): JMSContext
74
    {
75 12
        if (self::SERIALIZATION === $direction) {
76 10
            $jmsContext = $this->serializationContextFactory
77 9
                ? $this->serializationContextFactory->createSerializationContext()
78 10
                : SerializationContext::create();
79
        } else {
80 2
            $jmsContext = $this->deserializationContextFactory
81 1
                ? $this->deserializationContextFactory->createDeserializationContext()
82 2
                : DeserializationContext::create();
83
        }
84
85 12
        foreach ($context->getAttributes() as $key => $value) {
86 7
            $jmsContext->setAttribute($key, $value);
87
        }
88
89 12
        if (null !== $context->getVersion()) {
90 1
            $jmsContext->setVersion($context->getVersion());
91
        }
92 12
        if (null !== $context->getGroups()) {
93 1
            $jmsContext->setGroups($context->getGroups());
94
        }
95 12
        if (true === $context->isMaxDepthEnabled()) {
96 1
            $jmsContext->enableMaxDepthChecks();
97
        }
98 12
        if (null !== $context->getSerializeNull()) {
99 3
            $jmsContext->setSerializeNull($context->getSerializeNull());
0 ignored issues
show
Bug introduced by
The method setSerializeNull does only exist in JMS\Serializer\SerializationContext, but not in JMS\Serializer\DeserializationContext.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
        }
101
102 12
        foreach ($context->getExclusionStrategies() as $strategy) {
103 1
            $jmsContext->addExclusionStrategy($strategy);
104
        }
105
106 12
        return $jmsContext;
107
    }
108
}
109