JMSSerializerAdapter::convertContext()   F
last analyzed

Complexity

Conditions 13
Paths 448

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 26.3058

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 12
cts 21
cp 0.5714
rs 3.2166
c 0
b 0
f 0
cc 13
nc 448
nop 2
crap 26.3058

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
            $maxDepth = $context->getMaxDepth(false);
0 ignored issues
show
Deprecated Code introduced by Christian Flothmann
The method FOS\RestBundle\Context\Context::getMaxDepth() has been deprecated with message: since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90 5
            if (null !== $maxDepth) {
91
                for ($i = 0; $i < $maxDepth; ++$i) {
92
                    $jmsContext->increaseDepth();
93 5
                }
94
            }
95
        }
96 5
97 5
        foreach ($context->getAttributes() as $key => $value) {
98 5
            $jmsContext->setAttribute($key, $value);
99
        }
100 5
101
        if (null !== $context->getVersion()) {
102 5
            $jmsContext->setVersion($context->getVersion());
103
        }
104 5
        if (null !== $context->getGroups()) {
105
            $jmsContext->setGroups($context->getGroups());
106
        }
107
        if (null !== $context->getMaxDepth(false) || null !== $context->isMaxDepthEnabled()) {
0 ignored issues
show
Deprecated Code introduced by Ener-Getick
The method FOS\RestBundle\Context\Context::getMaxDepth() has been deprecated with message: since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
108
            $jmsContext->enableMaxDepthChecks();
109
        }
110
        if (null !== $context->getSerializeNull()) {
111
            $jmsContext->setSerializeNull($context->getSerializeNull());
0 ignored issues
show
Bug introduced by Ener-Getick
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...
112
        }
113
114
        foreach ($context->getExclusionStrategies() as $strategy) {
115
            $jmsContext->addExclusionStrategy($strategy);
116
        }
117
118
        return $jmsContext;
119
    }
120
}
121