Passed
Pull Request — master (#7)
by Dominik
06:07 queued 02:55
created

EmbedOneFieldDenormalizer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 83
loc 83
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B denormalizeField() 25 25 4
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 EmbedOneFieldDenormalizer
17
 */
18 View Code Duplication
final class EmbedOneFieldDenormalizer 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 6
    public function __construct($class, AccessorInterface $accessor)
35
    {
36 6
        $this->class = $class;
37 6
        $this->accessor = $accessor;
38 6
    }
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 6
    public function denormalizeField(
51
        string $path,
52
        $object,
53
        $value,
54
        DenormalizerContextInterface $context,
55
        DenormalizerInterface $denormalizer = null
56
    ) {
57 6
        if (null === $value) {
58 1
            $this->accessor->setValue($object, $value);
59
60 1
            return;
61
        }
62
63 5
        if (null === $denormalizer) {
64 1
            throw DeserializerLogicException::createMissingDenormalizer($path);
65
        }
66
67 4
        if (!is_array($value)) {
68 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
69
        }
70
71 3
        $embObject = $this->getEmbObjectOrClass($this->accessor->getValue($object));
72
73 3
        $this->accessor->setValue($object, $denormalizer->denormalize($embObject, $value, $context, $path));
74 3
    }
75
76
    /**
77
     * @param object|null $existEmbObject
78
     *
79
     * @return string
80
     */
81 3
    private function getEmbObjectOrClass($existEmbObject)
82
    {
83 3
        if (null === $existEmbObject) {
84 1
            return $this->class;
85
        }
86
87 2
        $this->resolveProxy($existEmbObject);
88
89 2
        return $existEmbObject;
90
    }
91
92 2
    private function resolveProxy($refObject)
93
    {
94 2
        if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy')
95 2
            && $refObject instanceof Proxy && !$refObject->__isInitialized()
96
        ) {
97 1
            $refObject->__load();
98
        }
99 2
    }
100
}
101