WebApiSerializer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 10
loc 60
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serialize() 10 10 1
A deserialize() 0 4 1
A removeRelations() 0 6 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
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the FiveLab Resource package
7
 *
8
 * (c) FiveLab
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace FiveLab\Component\Resource\Serializers\WebApi;
15
16
use FiveLab\Component\Resource\Resource\ResourceInterface;
17
use FiveLab\Component\Resource\Serializer\Context\ResourceSerializationContext;
18
use FiveLab\Component\Resource\Serializer\ResourceSerializerInterface;
19
use FiveLab\Component\Resource\Serializer\SerializerInterface;
20
21
/**
22
 * Default Web API serializer.
23
 *
24
 * @author Vitaliy Zhuk <[email protected]>
25
 */
26
class WebApiSerializer implements ResourceSerializerInterface
27
{
28
    /**
29
     * @var SerializerInterface
30
     */
31
    private $serializer;
32
33
    /**
34
     * @var string
35
     */
36
    private $format;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param SerializerInterface $serializer
42
     * @param string              $format
43
     */
44 2
    public function __construct(SerializerInterface $serializer, string $format)
45
    {
46 2
        $this->serializer = $serializer;
47 2
        $this->format = $format;
48 2
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1 View Code Duplication
    public function serialize(ResourceInterface $resource, ResourceSerializationContext $context): string
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...
54
    {
55
        $innerContext = [
56
            'after_normalization' => function (array $data) {
57 1
                return $this->removeRelations($data);
58 1
            },
59
        ];
60
61 1
        return $this->serializer->serialize($resource, $this->format, $innerContext);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function deserialize(string $data, string $resourceClass, ResourceSerializationContext $context): ResourceInterface
68
    {
69 1
        return $this->serializer->deserialize($data, $resourceClass, $this->format);
70
    }
71
72
    /**
73
     * Remove resources from data
74
     *
75
     * @param array $data
76
     *
77
     * @return array
78
     */
79 1
    protected function removeRelations(array $data): array
80
    {
81 1
        unset($data['relations'], $data['actions']);
82
83 1
        return $data;
84
    }
85
}
86