Test Setup Failed
Push — master ( aeb8b5...c4a9e3 )
by Alexpts
07:44
created

DeepArray   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 74
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace PTS\Tools;
5
6
class DeepArray
7
{
8
9
    protected bool $trim = false;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
10
11
    public function setTrim(bool $trim = false): static
12
    {
13
        $this->trim = $trim;
14
        return $this;
15 2
    }
16
17 2
    public function getTrim(): bool
18 2
    {
19
        return $this->trim;
20
    }
21 1
22
    public function getAttr(array $name, array &$context, mixed $defaultValue = null): mixed
23 1
    {
24
        $current = array_shift($name);
25
26
        if (!array_key_exists($current, $context)) {
27
            return $defaultValue;
28
        }
29
30
        return count($name)
31
            ? $this->getAttr($name, $context[$current], $defaultValue)
32
            : $context[$current];
33 6
    }
34
35 6
    public function setAttr(array $name, array &$context, mixed $value): static
36
    {
37 6
        $current = array_shift($name);
38 1
39
        if (count($name) === 0) {
40
            $this->setValueToContext($current, $context, $value);
41 6
            return $this;
42 5
        }
43 6
44
        if (!array_key_exists($current, $context) || !is_array($context[$current])) {
45
            $context[$current] = [];
46
        }
47
48
        $this->setAttr($name, $context[$current], $value);
49
50
        return $this;
51
    }
52
53 5
    protected function setValueToContext(string $name, array &$context, mixed $value): void
54
    {
55 5
        $context[$name] = $value;
56
57 5
        if ($value === null && $this->trim) {
58 5
            unset($context[$name]);
59 5
        }
60
    }
61
}
62