AbstractOptionsOutput::renameRecursivelyKeys()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 1
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 5
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace CMEN\GoogleChartsBundle\Output;
4
5
/**
6
 * @author Christophe Meneses
7
 */
8
abstract class AbstractOptionsOutput implements OptionsOutputInterface
9
{
10 4
    public function removeRecursivelyNullValue(&$options): void
11
    {
12 4
        $options = array_filter((array) $options, fn ($val) => !is_null($val));
13
14 4
        foreach ($options as $key => $value) {
15 4
            if (is_object($value) || is_array($value)) {
16 4
                $this->removeRecursivelyNullValue($options[$key]);
17
            }
18
        }
19
    }
20
21 4
    public function removeRecursivelyEmptyArray(array &$options): void
22
    {
23 4
        foreach ($options as $key => $value) {
24 4
            if (is_array($value)) {
25 4
                $this->removeRecursivelyEmptyArray($options[$key]);
26
27 4
                if (empty($options[$key])) {
28 2
                    unset($options[$key]);
29
                }
30
            }
31
        }
32
    }
33
34 4
    public function renameRecursivelyKeys(array $options): array
35
    {
36 4
        $newOptions = [];
37
38 4
        foreach ($options as $key => $value) {
39 4
            if (!is_numeric($key)) {
40 4
                $newKey = preg_replace('/\x00\*\x00/', '', $key);
41 4
                $newOptions[$newKey] = $value;
42
43 4
                if (is_array($options[$key])) {
44 4
                    $newOptions[$newKey] = $this->renameRecursivelyKeys($options[$key]);
45
                }
46
            } else {
47 3
                $newOptions[$key] = $value;
48
49 3
                if (is_array($options[$key])) {
50 2
                    $newOptions[$key] = $this->renameRecursivelyKeys($options[$key]);
51
                }
52
            }
53
        }
54
55 4
        return $newOptions;
56
    }
57
}
58