Completed
Push — master ( a7a790...5c4323 )
by Eric
03:39
created

AbstractDeserializationVisitor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 29.07 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 6
dl 25
loc 86
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A prepare() 0 4 1
A visitBoolean() 0 8 1
decode() 0 1 ?
B doVisitObjectProperty() 25 25 6
A createResult() 0 4 1

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\Visitor;
13
14
use Ivory\Serializer\Context\ContextInterface;
15
use Ivory\Serializer\Instantiator\InstantiatorInterface;
16
use Ivory\Serializer\Mapping\PropertyMetadataInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
use Ivory\Serializer\Mutator\MutatorInterface;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
abstract class AbstractDeserializationVisitor extends AbstractGenericVisitor
24
{
25
    /**
26
     * @var InstantiatorInterface
27
     */
28
    private $instantiator;
29
30
    /**
31
     * @var MutatorInterface
32
     */
33
    private $mutator;
34
35
    /**
36
     * @param InstantiatorInterface $instantiator
37
     * @param MutatorInterface      $mutator
38
     */
39 738
    public function __construct(InstantiatorInterface $instantiator, MutatorInterface $mutator)
40
    {
41 738
        $this->instantiator = $instantiator;
42 738
        $this->mutator = $mutator;
43 738
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 297
    public function prepare($data, ContextInterface $context)
49
    {
50 297
        return $this->decode(parent::prepare($data, $context));
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 27
    public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context)
57
    {
58 27
        return parent::visitBoolean(
59 27
            filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
60 18
            $type,
61
            $context
62 18
        );
63
    }
64
65
    /**
66
     * @param mixed $data
67
     *
68
     * @return mixed
69
     */
70
    abstract protected function decode($data);
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 216 View Code Duplication
    protected function doVisitObjectProperty(
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...
76
        $data,
77
        $name,
78
        PropertyMetadataInterface $property,
79
        ContextInterface $context
80
    ) {
81 216
        if (!$property->isWritable() || !isset($data[$name])) {
82 102
            return false;
83
        }
84
85 171
        $value = $this->navigator->navigate($data[$name], $context, $property->getType());
86
87 171
        if ($value === null && $context->isNullIgnored()) {
88
            return false;
89
        }
90
91
        // FIXME - Detect errors
92 171
        $this->mutator->setValue(
93 171
            $this->result,
94 171
            $property->hasMutator() ? $property->getMutator() : $property->getName(),
95
            $value
96 114
        );
97
98 171
        return true;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 216
    protected function createResult($class)
105
    {
106 216
        return $this->instantiator->instantiate($class);
107
    }
108
}
109