Nulls::parse()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
ccs 31
cts 31
cp 1
rs 8.7217
cc 6
nc 5
nop 1
crap 6
1
<?php
2
3
namespace Maketok\DataMigration\Input\Shaper\Processor;
4
5
use Maketok\DataMigration\Input\Shaper\Processor;
6
7
class Nulls extends Processor
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12 4
    public function parse(array $entity)
13
    {
14 4
        $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($entity));
15 4
        $current = [];
16 4
        $res = [];
17 4
        $depthMap = [];
18 4
        $prevDepth = 0;
19 4
        foreach ($iterator as $key => $value) {
20 4
            $depth = $iterator->getDepth();
21 4
            if ($depth < $prevDepth) {
22 1
                $res[] = $current;
23
                $current = array_map(function () {
24 1
                    return null;
25 1
                }, $current);
26 1
                $depthMap = [];
27 1
            }
28 4
            if (isset($depthMap[$depth]) && in_array($key, $depthMap[$depth])) {
29 3
                $res[] = $current;
30 3
                $current = array_map(function ($val) use ($current, $depthMap, $depth) {
31 3
                    $key = array_search($val, $current);
32 3
                    if (in_array($key, $depthMap[$depth])) {
33 3
                        return $val;
34
                    }
35 3
                    return null;
36 3
                }, $current);
37 3
                $depthMap = [];
38 3
                $depthMap[$depth][] = $key;
39 3
            } else {
40 4
                $depthMap[$depth][] = $key;
41
            }
42 4
            $current[$key] = $value;
43 4
            $prevDepth = $depth;
44 4
        }
45 4
        $res[] = $current;
46 4
        return $res;
47
    }
48
}
49