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

ReceivedMap::serializeMixedType()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 2
dl 0
loc 17
ccs 8
cts 9
cp 0.8889
crap 5.0342
rs 9.6111
c 0
b 0
f 0
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