Passed
Pull Request — master (#5)
by
unknown
03:01
created

BaseConfig   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 39
ccs 0
cts 31
cp 0
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 4 2
A __set() 0 7 3
A __construct() 0 3 1
A toArray() 0 7 2
A __get() 0 4 2
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Config;
4
5
class BaseConfig
6
{
7
    protected $data = [];
8
9
    public function __construct(Params $params)
10
    {
11
        $this->configure($params->get(static::class, []));
12
    }
13
14
    public function __get($name)
15
    {
16
        $getter = 'get' . ucfirst($name);
17
        return method_exists($this, $getter) ? $this->$getter() : ($this->data[$name] ?? null);
18
    }
19
20
    public function __set($name, $value): void
21
    {
22
        $setter = 'set' . ucfirst($name);
23
        if (method_exists($this, $setter)) {
24
            $this->$setter($value);
25
        } elseif (key_exists($name, $this->data)) {
26
            $this->data[$name] = $value;
27
        }
28
    }
29
30
    public function configure(array $params): void
31
    {
32
        foreach ($params as $k => $v) {
33
            $this->__set($k, $v);
34
        }
35
    }
36
37
    public function toArray(): array
38
    {
39
        $result = [];
40
        foreach ($this->data as $k => $v) {
41
            $result[$k] = $this->__get($k);
42
        }
43
        return $result;
44
    }
45
}
46