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

ReferenceOneFieldNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 8
loc 56
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A normalizeField() 0 14 2
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 ReferenceOneFieldNormalizer implements FieldNormalizerInterface
15
{
16
    /**
17
     * @var AccessorInterface
18
     */
19
    private $identifierAccessor;
20
21
    /**
22
     * @var AccessorInterface
23
     */
24
    private $accessor;
25
26
    /**
27
     * @param AccessorInterface $identifierAccessor
28
     * @param AccessorInterface $accessor
29
     */
30 3
    public function __construct(AccessorInterface $identifierAccessor, AccessorInterface $accessor)
31
    {
32 3
        $this->identifierAccessor = $identifierAccessor;
33 3
        $this->accessor = $accessor;
34 3
    }
35
36
    /**
37
     * @param string                     $path
38
     * @param object                     $object
39
     * @param NormalizerContextInterface $context
40
     * @param NormalizerInterface|null   $normalizer
41
     *
42
     * @return mixed
43
     *
44
     * @throws SerializerLogicException
45
     */
46 3
    public function normalizeField(
47
        string $path,
48
        $object,
49
        NormalizerContextInterface $context,
50
        NormalizerInterface $normalizer = null
51
    ) {
52 3
        if (null === $relatedObject = $this->accessor->getValue($object)) {
53 1
            return null;
54
        }
55
56 2
        $this->resolveProxy($relatedObject);
57
58 2
        return $this->identifierAccessor->getValue($relatedObject);
59
    }
60
61 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...
62
    {
63 2
        if (null !== $relatedObject && interface_exists('Doctrine\Common\Persistence\Proxy')
64 2
            && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()
65
        ) {
66 1
            $relatedObject->__load();
67
        }
68 2
    }
69
}
70