Completed
Push — master ( b1054d...eba5f2 )
by Christian
07:28 queued 11s
created

JMSSerializerAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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 since 2.8
28
 */
29
class JMSSerializerAdapter implements Serializer
30
{
31
    /**
32
     * @internal
33
     */
34
    const SERIALIZATION = 0;
35
36
    /**
37
     * @internal
38
     */
39
    const DESERIALIZATION = 1;
40
41
    private $serializer;
42
    private $serializationContextFactory;
43
    private $deserializationContextFactory;
44
45 10
    public function __construct(
46
        SerializerInterface $serializer,
47
        SerializationContextFactoryInterface $serializationContextFactory = null,
48
        DeserializationContextFactoryInterface $deserializationContextFactory = null
49
    ) {
50 10
        $this->serializer = $serializer;
51 10
        $this->serializationContextFactory = $serializationContextFactory;
52 10
        $this->deserializationContextFactory = $deserializationContextFactory;
53 10
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 8
    public function serialize($data, $format, Context $context)
59
    {
60 8
        $context = $this->convertContext($context, self::SERIALIZATION);
61
62 8
        return $this->serializer->serialize($data, $format, $context);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    public function deserialize($data, $type, $format, Context $context)
69
    {
70 2
        $context = $this->convertContext($context, self::DESERIALIZATION);
71
72 2
        return $this->serializer->deserialize($data, $type, $format, $context);
73
    }
74
75
    /**
76
     * @param Context $context
77
     * @param int     $direction {@see self} constants
78
     *
79
     * @return JMSContext
80
     */
81 10
    private function convertContext(Context $context, $direction)
82
    {
83 10
        if (self::SERIALIZATION === $direction) {
84 8
            $jmsContext = $this->serializationContextFactory
85 7
                ? $this->serializationContextFactory->createSerializationContext()
86 8
                : SerializationContext::create();
87
        } else {
88 2
            $jmsContext = $this->deserializationContextFactory
89 1
                ? $this->deserializationContextFactory->createDeserializationContext()
90 2
                : DeserializationContext::create();
91
        }
92
93 10
        foreach ($context->getAttributes() as $key => $value) {
94 6
            $jmsContext->setAttribute($key, $value);
95
        }
96
97 10
        if (null !== $context->getVersion()) {
98 1
            $jmsContext->setVersion($context->getVersion());
99
        }
100 10
        if (null !== $context->getGroups()) {
101 1
            $jmsContext->setGroups($context->getGroups());
102
        }
103 10
        if (null !== $context->isMaxDepthEnabled()) {
104 1
            $jmsContext->enableMaxDepthChecks();
105
        }
106 10
        if (null !== $context->getSerializeNull()) {
107 6
            $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...
108
        }
109
110 10
        foreach ($context->getExclusionStrategies() as $strategy) {
111 1
            $jmsContext->addExclusionStrategy($strategy);
112
        }
113
114 10
        return $jmsContext;
115
    }
116
}
117