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

ReferenceManyFieldDenormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 78
loc 78
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B denormalizeField() 36 36 5
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 ReferenceManyFieldDenormalizer
17
 */
18 View Code Duplication
final class ReferenceManyFieldDenormalizer 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 callable
22
     */
23
    private $repository;
24
25
    /**
26
     * @var AccessorInterface
27
     */
28
    private $accessor;
29
30
    /**
31
     * @param callable          $repository
32
     * @param AccessorInterface $accessor
33
     */
34 5
    public function __construct(callable $repository, AccessorInterface $accessor)
35
    {
36 5
        $this->repository = $repository;
37 5
        $this->accessor = $accessor;
38 5
    }
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 5
    public function denormalizeField(
51
        string $path,
52
        $object,
53
        $value,
54
        DenormalizerContextInterface $context,
55
        DenormalizerInterface $denormalizer = null
56
    ) {
57 5
        if (null === $value) {
58 1
            $this->accessor->setValue($object, $value);
59
60 1
            return;
61
        }
62
63 4
        if (!is_array($value)) {
64 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
65
        }
66
67 3
        $repository = $this->repository;
68
69 3
        $refObjects = [];
70 3
        foreach ($value as $i => $subValue) {
71 3
            $subPath = $path.'['.$i.']';
72
73 3
            if (!is_string($subValue)) {
74 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string');
75
            }
76
77 2
            $refObject = $repository($subValue);
78
79 2
            $this->resolveProxy($refObject);
80
81 2
            $refObjects[$i] = $refObject;
82
        }
83
84 2
        $this->accessor->setValue($object, $refObjects);
85 2
    }
86
87 2
    private function resolveProxy($refObject)
88
    {
89 2
        if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy')
90 2
            && $refObject instanceof Proxy && !$refObject->__isInitialized()
91
        ) {
92 1
            $refObject->__load();
93
        }
94 2
    }
95
}
96