Test Failed
Push — master ( 86efb6...f66ff9 )
by Gerrit
15:32
created

ArrayMapping   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 125
Duplicated Lines 9.6 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 97.87%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 3
dl 12
loc 125
ccs 46
cts 47
cp 0.9787
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getEntryMappings() 0 4 1
A describeOrigin() 0 4 1
A collectDBALColumns() 0 16 2
A resolveValue() 0 18 2
A revertValue() 0 30 4
A wakeUpMapping() 0 8 2
A assertValue() 12 12 3

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
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\RDMBundle\Mapping;
14
15
use Addiks\RDMBundle\Mapping\MappingInterface;
16
use Webmozart\Assert\Assert;
17
use Doctrine\DBAL\Schema\Column;
18
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
19
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
final class ArrayMapping implements MappingInterface
23
{
24
25
    /**
26
     * @var array<MappingInterface>
27
     */
28
    private $entryMappings = array();
29
30
    /**
31
     * @var string
32
     */
33
    private $origin;
34
35 11
    public function __construct(array $entryMappings, string $origin = "unknown")
36
    {
37 11
        $this->origin = $origin;
38
39 11
        foreach ($entryMappings as $key => $entryMapping) {
40
            /** @var MappingInterface $entryMapping */
41
42 11
            Assert::isInstanceOf($entryMapping, MappingInterface::class);
43
44 11
            $this->entryMappings[$key] = $entryMapping;
45
        }
46 11
    }
47
48 1
    public function getEntryMappings(): array
49
    {
50 1
        return $this->entryMappings;
51
    }
52
53 1
    public function describeOrigin(): string
54
    {
55 1
        return $this->origin;
56
    }
57
58 2
    public function collectDBALColumns(): array
59
    {
60
        /** @var array<Column> $dbalColumns */
61 2
        $dbalColumns = array();
62
63 2
        foreach ($this->entryMappings as $entryMapping) {
64
            /** @var MappingInterface $entryMapping */
65
66 2
            $dbalColumns = array_merge(
67 2
                $dbalColumns,
68 2
                $entryMapping->collectDBALColumns()
69
            );
70
        }
71
72 2
        return $dbalColumns;
73
    }
74
75 1
    public function resolveValue(
76
        HydrationContextInterface $context,
77
        array $dataFromAdditionalColumns
78
    ) {
79
        /** @var array<mixed> $value */
80 1
        $value = array();
81
82 1
        foreach ($this->entryMappings as $key => $entryMapping) {
83
            /** @var MappingInterface $entryMapping */
84
85 1
            $value[$key] = $entryMapping->resolveValue(
86 1
                $context,
87 1
                $dataFromAdditionalColumns
88
            );
89
        }
90
91 1
        return $value;
92
    }
93
94 2
    public function revertValue(
95
        HydrationContextInterface $context,
96
        $valueFromEntityField
97
    ): array {
98
        /** @var array<string, string> $data */
99 2
        $data = array();
100
101 2
        if (is_array($valueFromEntityField)) {
102 1
            foreach ($this->entryMappings as $key => $entryMapping) {
103
                /** @var MappingInterface $entryMapping */
104
105
                /** @var mixed $valueFromEntry */
106 1
                $valueFromEntry = null;
107
108 1
                if (isset($valueFromEntityField[$key])) {
109 1
                    $valueFromEntry = $valueFromEntityField[$key];
110
                }
111
112 1
                $data = array_merge(
113 1
                    $data,
114 1
                    $entryMapping->revertValue(
115 1
                        $context,
116 1
                        $valueFromEntry
117
                    )
118
                );
119
            }
120
        }
121
122 2
        return $data;
123
    }
124
125 1 View Code Duplication
    public function assertValue(
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...
126
        HydrationContextInterface $context,
127
        array $dataFromAdditionalColumns,
128
        $actualValue
129
    ): void {
130 1
        if (!is_array($actualValue) && !is_null($actualValue)) {
131 1
            throw FailedRDMAssertionException::expectedArray(
132 1
                $actualValue,
133 1
                $this->origin
134
            );
135
        }
136
    }
137
138 1
    public function wakeUpMapping(ContainerInterface $container): void
139
    {
140 1
        foreach ($this->entryMappings as $key => $entryMapping) {
141
            /** @var MappingInterface $entryMapping */
142
143 1
            $entryMapping->wakeUpMapping($container);
144
        }
145 1
    }
146
}
147