Completed
Pull Request — master (#2079)
by Christian
11:52 queued 01:53
created

JMSSerializerAdapter::convertContext()   C

Complexity

Conditions 10
Paths 256

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 17.8732

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 12
cts 21
cp 0.5714
rs 6.1333
c 0
b 0
f 0
cc 10
nc 256
nop 2
crap 17.8732

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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