Passed
Push — main ( b59441...65de47 )
by Alexandra
02:41
created

ReceivedMap   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
c 1
b 0
f 1
dl 0
loc 56
ccs 20
cts 21
cp 0.9524
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 19 5
A cleanAndFlatten() 0 3 1
A serializeMixedType() 0 17 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AHJ\ApprovalTests;
6
7
final class ReceivedMap
8
{
9 12
    public function create(array $input, array $output): string
10
    {
11 12
        $receivedMap = [];
12
13 12
        if (!empty($input)) {
14 11
            foreach ($input as $inputKey => $inputValue) {
15 11
                $receivedMap[$inputKey] = '[' . $this->serializeMixedType($inputValue) . '] -> ';
16
            }
17
        }
18
19 12
        foreach ($output as $outputKey => $outputValue) {
20 12
            if (!empty($input)) {
21 11
                $receivedMap[$outputKey] = $receivedMap[$outputKey] . '[' . $this->serializeMixedType($outputValue) . ']';
22
            } else {
23 1
                $receivedMap[$outputKey] = '[' . $this->serializeMixedType($outputValue) . ']';
24
            }
25
        }
26
27 12
        return $this->cleanAndFlatten($receivedMap);
28
    }
29
30 12
    private function serializeMixedType($inputValue, string $currentConcatenations = ''): string
31
    {
32 12
        $concatenations = '';
33
34 12
        if ('string' === gettype($inputValue)) {
35
            return $currentConcatenations . $inputValue;
36
        }
37
38 12
        foreach ($inputValue as $item) {
39 12
            if ('array' === gettype($item) || 'object' === gettype($item)) {
40 6
                $concatenations .= '[' . $this->serializeMixedType($item, $concatenations) . ']';
41
            } else {
42 12
                $concatenations .= $item . ', ';
43
            }
44
        }
45
46 12
        return $concatenations;
47
    }
48
49
    /**
50
     * @param array $received Example:
51
     *                        [
52
     *                        [0] => '[foo, 0, [bar, 10, ]] -> [foo, 10, [bar, 60, ]]',
53
     *                        [1] => '[foo, 10, [bar, 100, ]] -> [foo, 100, [bar, 600, ]]'
54
     *                        ]
55
     *
56
     * @return string Example:
57
     *                '[foo, 0, [bar, 10]] -> [foo, 10, [bar, 60]]\n
58
     *                [foo, 10, [bar, 100]] -> [foo, 100, [bar, 600]]'
59
     */
60 12
    private function cleanAndFlatten(array $received): string
61
    {
62 12
        return str_replace(', ]', ']', implode("\n", $received));
63
    }
64
}
65