Passed
Pull Request — master (#7)
by Dominik
02:53
created

EmbedManyFieldDenormalizer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 96
loc 96
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B denormalizeField() 38 38 6
A getEmbObjectOrClass() 10 10 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\Deserialization\Denormalizer\Relation;
6
7
use Chubbyphp\Deserialization\Accessor\AccessorInterface;
8
use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface;
9
use Chubbyphp\Deserialization\Denormalizer\DenormalizerInterface;
10
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
11
use Chubbyphp\Deserialization\DeserializerLogicException;
12
use Chubbyphp\Deserialization\DeserializerRuntimeException;
13
use Doctrine\Common\Persistence\Proxy;
14
15
/**
16
 * @deprecated use Basic or Doctrine EmbedManyFieldDenormalizer
17
 */
18 View Code Duplication
final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * @var string
22
     */
23
    private $class;
24
25
    /**
26
     * @var AccessorInterface
27
     */
28
    private $accessor;
29
30
    /**
31
     * @param string            $class
32
     * @param AccessorInterface $accessor
33
     */
34 7
    public function __construct(string $class, AccessorInterface $accessor)
35
    {
36 7
        $this->class = $class;
37 7
        $this->accessor = $accessor;
38 7
    }
39
40
    /**
41
     * @param string                       $path
42
     * @param object                       $object
43
     * @param mixed                        $value
44
     * @param DenormalizerContextInterface $context
45
     * @param DenormalizerInterface|null   $denormalizer
46
     *
47
     * @throws DeserializerLogicException
48
     * @throws DeserializerRuntimeException
49
     */
50 7
    public function denormalizeField(
51
        string $path,
52
        $object,
53
        $value,
54
        DenormalizerContextInterface $context,
55
        DenormalizerInterface $denormalizer = null
56
    ) {
57 7
        if (null === $value) {
58 1
            $this->accessor->setValue($object, $value);
59
60 1
            return;
61
        }
62
63 6
        if (null === $denormalizer) {
64 1
            throw DeserializerLogicException::createMissingDenormalizer($path);
65
        }
66
67 5
        if (!is_array($value)) {
68 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
69
        }
70
71 4
        $existEmbObjects = $this->accessor->getValue($object);
72
73 4
        $embObjects = [];
74 4
        foreach ($value as $i => $subValue) {
75 4
            $subPath = $path.'['.$i.']';
76
77 4
            if (!is_array($subValue)) {
78 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array');
79
            }
80
81 3
            $embObject = $this->getEmbObjectOrClass($existEmbObjects[$i] ?? null);
82
83 3
            $embObjects[$i] = $denormalizer->denormalize($embObject, $subValue, $context, $subPath);
84
        }
85
86 3
        $this->accessor->setValue($object, $embObjects);
87 3
    }
88
89
    /**
90
     * @param object|null $existEmbObject
91
     *
92
     * @return string
93
     */
94 3
    private function getEmbObjectOrClass($existEmbObject)
95
    {
96 3
        if (null === $existEmbObject) {
97 1
            return $this->class;
98
        }
99
100 2
        $this->resolveProxy($existEmbObject);
101
102 2
        return $existEmbObject;
103
    }
104
105 2
    private function resolveProxy($refObject)
106
    {
107 2
        if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy')
108 2
            && $refObject instanceof Proxy && !$refObject->__isInitialized()
109
        ) {
110 1
            $refObject->__load();
111
        }
112 2
    }
113
}
114