Completed
Push — master ( 1856ca...43d45a )
by Eric
03:58
created

JsonSerializationVisitor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 10
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A encode() 10 10 2
A visitData() 0 8 3

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\Exclusion\ExclusionStrategyInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
use Ivory\Serializer\Naming\NamingStrategyInterface;
19
use Ivory\Serializer\Visitor\AbstractSerializationVisitor;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class JsonSerializationVisitor extends AbstractSerializationVisitor
25
{
26
    /**
27
     * @var int
28
     */
29
    private $options;
30
31
    /**
32
     * @param AccessorInterface               $accessor
33
     * @param ExclusionStrategyInterface|null $exclusionStrategy
34
     * @param NamingStrategyInterface|null    $namingStrategy
35
     * @param int                             $options
36
     */
37
    public function __construct(
38
        AccessorInterface $accessor,
39
        ExclusionStrategyInterface $exclusionStrategy = null,
40
        NamingStrategyInterface $namingStrategy = null,
41
        $options = 0
42
    ) {
43
        parent::__construct($accessor, $exclusionStrategy, $namingStrategy);
44
45
        $this->options = $options;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function visitData($data, TypeMetadataInterface $type, ContextInterface $context)
52
    {
53
        if ($data === [] && class_exists($type->getName())) {
54
            $data = (object) $data;
55
        }
56
57
        return parent::visitData($data, $type, $context);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 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...
64
    {
65
        $result = @json_encode($data, $this->options);
66
67
        if (json_last_error() !== JSON_ERROR_NONE) {
68
            throw new \InvalidArgumentException(json_last_error_msg());
69
        }
70
71
        return $result;
72
    }
73
}
74