ResourceCollectionObjectNormalizer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 23.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 13
loc 56
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 13 36 7
A supportsNormalization() 0 4 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
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\Hateoas\Normalizer;
15
16
use FiveLab\Component\Resource\Resource\ResourceCollection;
17
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
21
/**
22
 * The normalizer for normalize resource collection.
23
 *
24
 * @author Vitaliy Zhuk <[email protected]>
25
 */
26
class ResourceCollectionObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
27
{
28
    use NormalizerAwareTrait;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @param ResourceCollection $object
34
     *
35
     * @throws \InvalidArgumentException
36
     */
37 3
    public function normalize($object, $format = null, array $context = [])
38
    {
39 3 View Code Duplication
        if (!$object instanceof ResourceCollection) {
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...
40 1
            throw new \InvalidArgumentException(\sprintf(
41 1
                'The normalizer support only "%s" but "%s" given.',
42 1
                ResourceCollection::class,
43 1
                \is_object($object) ? \get_class($object) : \gettype($object)
44
            ));
45
        }
46
47 2
        $data = [];
48 2
        $links = [];
49
50 2 View Code Duplication
        if (\count($object->getRelations())) {
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...
51 1
            $links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context));
52
        }
53
54 2 View Code Duplication
        if (\count($object->getActions())) {
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...
55 1
            $links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context));
56
        }
57
58 2
        foreach ($object as $resource) {
59 2
            $data[] = $this->normalizer->normalize($resource, $format, $context);
60
        }
61
62 2
        if (\count($links)) {
63
            return [
64 1
                '_links' => $links,
65
                '_embedded' => [
66 1
                    'items' => $data,
67
                ],
68
            ];
69
        }
70
71 1
        return $data;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function supportsNormalization($data, $format = null): bool
78
    {
79 2
        return \is_object($data) && $data instanceof ResourceCollection;
80
    }
81
}
82