Completed
Push — master ( 1e2cfa...28995d )
by Eric
04:38
created

Serializer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 8.24 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 7
loc 85
ccs 25
cts 25
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A navigate() 0 15 2
A serialize() 0 4 1
A deserialize() 7 16 4

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 Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer;
13
14
use Ivory\Serializer\Context\Context;
15
use Ivory\Serializer\Context\ContextInterface;
16
use Ivory\Serializer\Mapping\TypeMetadataInterface;
17
use Ivory\Serializer\Navigator\Navigator;
18
use Ivory\Serializer\Navigator\NavigatorInterface;
19
use Ivory\Serializer\Registry\VisitorRegistry;
20
use Ivory\Serializer\Registry\VisitorRegistryInterface;
21
use Ivory\Serializer\Type\Parser\TypeParser;
22
use Ivory\Serializer\Type\Parser\TypeParserInterface;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class Serializer implements SerializerInterface
28
{
29
    /**
30
     * @var NavigatorInterface
31
     */
32
    private $navigator;
33
34
    /**
35
     * @var VisitorRegistryInterface
36
     */
37
    private $visitorRegistry;
38
39
    /**
40
     * @var TypeParserInterface
41
     */
42
    private $typeParser;
43
44
    /**
45
     * @param NavigatorInterface|null       $navigator
46
     * @param VisitorRegistryInterface|null $visitorRegistry
47
     * @param TypeParserInterface|null      $typeParser
48
     */
49 1492
    public function __construct(
50
        NavigatorInterface $navigator = null,
51
        VisitorRegistryInterface $visitorRegistry = null,
52
        TypeParserInterface $typeParser = null
53
    ) {
54 1492
        $this->navigator = $navigator ?: new Navigator();
55 1492
        $this->visitorRegistry = $visitorRegistry ?: VisitorRegistry::create();
56 1492
        $this->typeParser = $typeParser ?: new TypeParser();
57 1492
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 888
    public function serialize($data, $format, ContextInterface $context = null)
63
    {
64 888
        return $this->navigate($data, Direction::SERIALIZATION, $format, $context);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 606
    public function deserialize($data, $type, $format, ContextInterface $context = null)
71
    {
72 606
        if (is_string($type)) {
73 590
            $type = $this->typeParser->parse($type);
74 296
        }
75
76 606 View Code Duplication
        if (!$type instanceof TypeMetadataInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77 20
            throw new \InvalidArgumentException(sprintf(
78 16
                'The type must be a string or a "%s", got "%s".',
79 16
                TypeMetadataInterface::class,
80 18
                is_object($type) ? get_class($type) : gettype($type)
81 10
            ));
82 2
        }
83
84 588
        return $this->navigate($data, Direction::DESERIALIZATION, $format, $context, $type);
85
    }
86
87
    /**
88
     * @param mixed                      $data
89
     * @param int                        $direction
90
     * @param string                     $format
91
     * @param ContextInterface|null      $context
92
     * @param TypeMetadataInterface|null $type
93
     *
94
     * @return mixed
95
     */
96 1476
    private function navigate(
97
        $data,
98
        $direction,
99
        $format,
100
        ContextInterface $context = null,
101
        TypeMetadataInterface $type = null
102
    ) {
103 1476
        $visitor = $this->visitorRegistry->getVisitor($direction, $format);
104
105 1472
        $context = $context ?: new Context();
106 1472
        $context->initialize($this->navigator, $visitor, $direction, $format);
107 1472
        $this->navigator->navigate($visitor->prepare($data, $context), $context, $type);
108
109 1456
        return $visitor->getResult();
110
    }
111
}
112