MutableConfig   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 16
dl 0
loc 39
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 2 1
A keyForMerge() 0 2 2
A subConfig() 0 2 1
A _mergeKeyValue() 0 6 2
A merge() 0 3 2
A isConfigObjectMerge() 0 4 3
A nextNumericIndex() 0 4 2
1
<?php
2
3
namespace Logikos\Util\Config;
4
5
use Logikos\Util\Config;
6
7
class MutableConfig extends Config {
8
  # Set
9 2
  public function set($offset, $value) {
10 2
    $this->offsetSet($offset, $value);
11 2
  }
12
13 6
  protected function subConfig($arrayConfig = []) {
14 6
    return new self($arrayConfig);
15
  }
16
17
  # Merge
18 6
  public function merge($b) {
19 6
    foreach ($b as $key=>$value) {
20 6
      $this->_mergeKeyValue($key, $value);
21
    }
22 6
  }
23
24 6
  private function _mergeKeyValue($key, $value) {
25 6
    if ($this->isConfigObjectMerge($key, $value)) {
26 3
      $this->offsetGet($key)->merge($value);
27 3
      return;
28
    }
29 6
    $this->offsetSet($this->keyForMerge($key), $value);
30 6
  }
31
32 6
  private function isConfigObjectMerge($key, $value) {
33 6
    return $this->offsetExists($key)
34 6
        && $this->isConfigObject($value)
35 6
        && $this->isConfigObject($this->{$key});
36
  }
37
38 6
  private function keyForMerge($key) {
39 6
    return is_numeric($key) ? $this->nextNumericIndex() : $key;
40
  }
41
42 3
  private function nextNumericIndex() {
43 3
    $numericKeys = array_filter(array_keys($this->rawValues()), 'is_numeric');
44 3
    if (count($numericKeys) === 0) return 0;
45 3
    return max($numericKeys) + 1;
46
  }
47
}