Completed
Push — master ( 284cd3...7e1f1b )
by Vitaliy
01:44
created

ResourceCollectionObjectNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
C normalize() 7 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
        if (count($object->getRelations())) {
51 1
            $links = array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context));
0 ignored issues
show
Bug introduced by
It seems like $object->getRelations() targeting FiveLab\Component\Resour...Support::getRelations() can also be of type array<integer,object<Fiv...ion\RelationInterface>>; however, Symfony\Component\Serial...rInterface::normalize() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
        }
53
54 2
        if (count($object->getActions())) {
55 1
            $links = array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context));
0 ignored issues
show
Bug introduced by
It seems like $object->getActions() targeting FiveLab\Component\Resour...ceSupport::getActions() can also be of type array<integer,object<Fiv...ction\ActionInterface>>; however, Symfony\Component\Serial...rInterface::normalize() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
        }
57
58 2
        foreach ($object as $resource) {
59 2
            $data[] = $this->normalizer->normalize($resource, $format, $context);
0 ignored issues
show
Security Bug introduced by
It seems like $resource defined by $resource on line 58 can also be of type false; however, Symfony\Component\Serial...rInterface::normalize() does only seem to accept object, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
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