Callback   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 78
Duplicated Lines 20.51 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 16
loc 78
ccs 28
cts 32
cp 0.875
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setSerializer() 0 7 2
A setGroups() 8 8 4
A setVersion() 0 8 3
A setSerializeNull() 8 8 4
B serialize() 0 24 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://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\ElasticaBundle\Serializer;
13
14
use JMS\Serializer\SerializationContext;
15
use JMS\Serializer\SerializerInterface as JMSSerializer;
16
use Symfony\Component\Serializer\SerializerInterface;
17
18
class Callback
19
{
20
    protected $serializer;
21
    protected $groups = [];
22
    protected $version;
23
    protected $serializeNull = false;
24
25
    /**
26
     * @param object $serializer
27
     */
28 5
    public function setSerializer($serializer)
29
    {
30 5
        $this->serializer = $serializer;
31 5
        if (!\method_exists($this->serializer, 'serialize')) {
32 1
            throw new \RuntimeException('The serializer must have a "serialize" method.');
33
        }
34 4
    }
35
36 4 View Code Duplication
    public function setGroups(array $groups): void
37
    {
38 4
        $this->groups = $groups;
39
40 4
        if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface && !$this->serializer instanceof JMSSerializer) {
41 1
            throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer" or "Symfony\Component\Serializer\Serializer".');
42
        }
43 3
    }
44
45
    /**
46
     * @param mixed $version
47
     */
48 1
    public function setVersion($version): void
49
    {
50 1
        $this->version = $version;
51
52 1
        if ($this->version && !$this->serializer instanceof JMSSerializer) {
53
            throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
54
        }
55 1
    }
56
57 2 View Code Duplication
    public function setSerializeNull(bool $serializeNull): void
58
    {
59 2
        $this->serializeNull = $serializeNull;
60
61 2
        if (true === $this->serializeNull && !$this->serializer instanceof SerializerInterface && !$this->serializer instanceof JMSSerializer) {
62
            throw new \RuntimeException('Setting null value serialization option requires using "JMS\Serializer\Serializer" or "Symfony\Component\Serializer\Serializer".');
63
        }
64 2
    }
65
66
    /**
67
     * @param $object
68
     *
69
     * @return mixed
70
     */
71 2
    public function serialize($object)
72
    {
73 2
        $context = $this->serializer instanceof JMSSerializer ? SerializationContext::create()->enableMaxDepthChecks() : [];
74
75 2
        if (!empty($this->groups)) {
76 1
            if ($context instanceof SerializationContext) {
77 1
                $context->setGroups($this->groups);
78
            } else {
79
                $context['groups'] = $this->groups;
80
            }
81
        }
82
83 2
        if ($this->version) {
84 1
            $context->setVersion($this->version);
85
        }
86
87 2
        if ($context instanceof SerializationContext) {
88 2
            $context->setSerializeNull($this->serializeNull);
89
        } else {
90
            $context['skip_null_values'] = !$this->serializeNull;
91
        }
92
93 2
        return $this->serializer->serialize($object, 'json', $context);
94
    }
95
}
96