Single   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 5
c 3
b 2
f 1
lcom 1
cbo 1
dl 0
loc 29
ccs 18
cts 18
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 23 5
1
<?php
2
3
namespace GFG\Mapper\Data\Type\Assoc;
4
5
use GFG\Mapper\Data\MapperInterface;
6
use GFG\Mapper\Data\Type;
7
8
/**
9
 * For assoc single structures like:
10
 *
11
 * $data = [
12
 *     'myObject' => [
13
 *         4 => 5
14
 *     ]
15
 * ];
16
 * $structure = [
17
 *     'myObject' => [
18
 *         'type' => 'assoc-single',
19
 *         'prefix' => 'my_object',
20
 *         'use' => 'raw'   // if raw, uses non mapped keys to fetch values
21
 *     ]
22
 * ];
23
 */
24
class Single extends Type\Single
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function run(&$data, $key = null)
30
    {
31 2
        $new = [];
32 2
        foreach ($data as $key => $value) {
33 2
            if (!is_array($value)) { // check for single to be sure!
34 2
                $oldKey = $key;
35
36 2
                parent::run($key);
37
38 2
                if ($value) {
39 2
                    if ($this->options['use'] == 'raw') {
40 1
                        parent::run($value, $oldKey . '_' . $value);
41 1
                    } else {
42 1
                        parent::run($value, $key . '_' . $value);
43
                    }
44 2
                }
45
46 2
                unset($data[$oldKey]);
47 2
                $new[$key] = $value;
48 2
            }
49 2
        }
50 2
        $data += $new; // yep, union operator here!
51 2
    }
52
}
53