Passed
Pull Request — main (#3)
by Daryl
02:28
created

Base::parseArgs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Clubdeuce\Tessitura\Base;
4
5
class Base {
6
    protected array $_extraArgs = [];
7
8 67
    public function __construct(array $args = [])
9
    {
10 67
        $this->setState($args);
11
    }
12
13 67
    protected function setState(array $args = []): void
14
    {
15 67
        foreach ($args as $key => $value) {
16 63
            $property = "_{$key}";
17
18 63
            if (property_exists($this, $property)) {
19 26
                $this->{$property} = $value;
20
            } else {
21 52
                $this->_extraArgs[$key] = $value;
22
            }
23
        }
24
    }
25
26 6
    public function __call(string $name, array $args = []): mixed
27
    {
28 6
        $property = "_{$name}";
29
30 6
        if (property_exists($this, $property)) {
31 4
            return $this->{$property};
32
        }
33
34 2
        if (isset($this->_extraArgs[$name])) {
35 2
            return $this->_extraArgs[$name];
36
        }
37
38 1
        return false;
39
    }
40
41 17
    public function parseArgs(array $args = [], array $defaults = []): array
42
    {
43 17
        foreach ($defaults as $key => $value) {
44 17
            if (!isset($args[$key])) {
45 17
                $args[$key] = $value;
46
            }
47
        }
48
49 17
        return $args;
50
    }
51
52 9
    public function extraArgs(): array
53
    {
54 9
        return $this->_extraArgs;
55
    }
56
}