JsonSerializationVisitor   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 30 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
dl 18
loc 60
c 0
b 0
f 0
wmc 9
lcom 2
cbo 2
ccs 18
cts 19
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A visitData() 8 8 3
A finishVisitingObject() 0 8 2
A encode() 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
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\Json;
13
14
use Ivory\Serializer\Accessor\AccessorInterface;
15
use Ivory\Serializer\Context\ContextInterface;
16
use Ivory\Serializer\Mapping\ClassMetadataInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
use Ivory\Serializer\Visitor\AbstractSerializationVisitor;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class JsonSerializationVisitor extends AbstractSerializationVisitor
24
{
25
    /**
26
     * @var int
27
     */
28
    private $options;
29
30
    /**
31
     * @param AccessorInterface $accessor
32
     * @param int               $options
33
     */
34 1492
    public function __construct(AccessorInterface $accessor, $options = 0)
35
    {
36 1492
        parent::__construct($accessor);
37
38 1492
        if (defined('JSON_PRESERVE_ZERO_FRACTION')) {
39 1492
            $options |= JSON_PRESERVE_ZERO_FRACTION;
40
        }
41
42 1492
        $this->options = $options;
43 1492
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 228 View Code Duplication
    public function visitData($data, TypeMetadataInterface $type, ContextInterface $context)
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...
49
    {
50 228
        if ($data === [] && class_exists($type->getName())) {
51 4
            $data = (object) $data;
52
        }
53
54 228
        return parent::visitData($data, $type, $context);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 168
    public function finishVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
61
    {
62 168
        if ($this->result === []) {
63 4
            $this->result = (object) $this->result;
64
        }
65
66 168
        return parent::finishVisitingObject($data, $class, $context);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 228 View Code Duplication
    protected function encode($data)
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...
73
    {
74 228
        $result = @json_encode($data, $this->options);
75
76 228
        if (json_last_error() !== JSON_ERROR_NONE) {
77
            throw new \InvalidArgumentException(json_last_error_msg());
78
        }
79
80 228
        return $result;
81
    }
82
}
83