Completed
Push — master ( 0e7db1...c18576 )
by Tomasz
12s
created

Serializard   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 23.81 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 10
loc 42
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A unserialize() 0 4 1
A serialize() 0 4 1
A getNormalizerContext() 0 6 2
A getFormat() 10 10 2

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
namespace Thunder\Serializard;
3
4
use Thunder\Serializard\Format\FormatInterface;
5
use Thunder\Serializard\FormatContainer\FormatContainerInterface as Formats;
6
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators;
7
use Thunder\Serializard\NormalizerContainer\NormalizerContainerInterface as Normalizers;
8
use Thunder\Serializard\NormalizerContext\NormalizerContextInterface;
9
use Thunder\Serializard\NormalizerContext\ParentNormalizerContext;
10
11
/**
12
 * @author Tomasz Kowalczyk <[email protected]>
13
 */
14
final class Serializard
15
{
16
    private $normalizers;
17
    private $hydrators;
18
    /** @var Formats */
19
    private $formats;
20
21 13
    public function __construct(Formats $formats, Normalizers $normalizers, Hydrators $hydrators)
22
    {
23 13
        $this->normalizers = $normalizers;
24 13
        $this->hydrators = $hydrators;
25 13
        $this->formats = $formats;
26 13
    }
27
28 12
    public function serialize($var, $format, NormalizerContextInterface $context = null)
29
    {
30 12
        return $this->getFormat($format)->serialize($var, $this->normalizers, $this->getNormalizerContext($context, $var, $format));
31
    }
32
33 4
    public function unserialize($var, $class, $format)
34
    {
35 4
        return $this->getFormat($format)->unserialize($var, $class, $this->hydrators);
36
    }
37
38 11
    private function getNormalizerContext(NormalizerContextInterface $context = null, $var, $format)
39
    {
40 11
        $context = $context ?: new ParentNormalizerContext();
41
42 11
        return $context->withRoot($var)->withFormat($format);
43
    }
44
45 13 View Code Duplication
    private function getFormat($alias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47 13
        $format = $this->formats->get($alias);
48
49 13
        if(false === $format instanceof FormatInterface) {
50 2
            throw new \RuntimeException(sprintf('No registered format for alias %s!', $alias));
51
        }
52
53 11
        return $format;
54
    }
55
}
56