Completed
Push — master ( 7912f2...809b2b )
by Dominik
07:10
created

EmbedManyFieldNormalizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 8
loc 59
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B normalizeField() 0 24 4
B resolveProxy() 8 8 5

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
namespace Chubbyphp\Serialization\Normalizer\Relation;
6
7
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
9
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
10
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
11
use Chubbyphp\Serialization\SerializerLogicException;
12
use Doctrine\Common\Persistence\Proxy;
13
14
final class EmbedManyFieldNormalizer implements FieldNormalizerInterface
15
{
16
    /**
17
     * @var AccessorInterface
18
     */
19
    private $accessor;
20
21
    /**
22
     * @param AccessorInterface $accessor
23
     */
24 5
    public function __construct(AccessorInterface $accessor)
25
    {
26 5
        $this->accessor = $accessor;
27 5
    }
28
29
    /**
30
     * @param string                     $path
31
     * @param object                     $object
32
     * @param NormalizerContextInterface $context
33
     * @param NormalizerInterface|null   $normalizer
34
     *
35
     * @return mixed
36
     *
37
     * @throws SerializerLogicException
38
     */
39 5
    public function normalizeField(
40
        string $path,
41
        $object,
42
        NormalizerContextInterface $context,
43
        NormalizerInterface $normalizer = null
44
    ) {
45 5
        if (null === $normalizer) {
46 1
            throw SerializerLogicException::createMissingNormalizer($path);
47
        }
48
49 4
        if (null === $relatedObjects = $this->accessor->getValue($object)) {
50 1
            return null;
51
        }
52
53 3
        $values = [];
54 3
        foreach ($relatedObjects as $i => $relatedObject) {
55 2
            $this->resolveProxy($relatedObject);
56
57 2
            $subPath = $path.'['.$i.']';
58 2
            $values[$i] = $normalizer->normalize($relatedObject, $context, $subPath);
59
        }
60
61 3
        return $values;
62
    }
63
64 2 View Code Duplication
    private function resolveProxy($relatedObject)
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...
65
    {
66 2
        if (null !== $relatedObject && interface_exists('Doctrine\Common\Persistence\Proxy')
67 2
            && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()
68
        ) {
69 1
            $relatedObject->__load();
70
        }
71 2
    }
72
}
73