Test Failed
Push — master ( 0bc352...8c447f )
by Todd
03:54
created

MutableConfig::subConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
  protected function subConfig($arrayConfig = []) {
14 6
    return new self($arrayConfig);
15 6
  }
16 6
17
  # Merge
18 6
  public function merge($b) {
19
    foreach ($b as $key=>$value) {
20 6
      $this->_mergeKeyValue($key, $value);
21 6
    }
22 3
  }
23 3
24
  private function _mergeKeyValue($key, $value) {
25 6
    if ($this->isConfigObjectMerge($key, $value)) {
26 6
      $this->offsetGet($key)->merge($value);
27
      return;
28 6
    }
29 6
    $this->offsetSet($this->keyForMerge($key), $value);
30 6
  }
31 6
32
  private function isConfigObjectMerge($key, $value) {
33
    return $this->offsetExists($key)
34 6
        && $this->isConfigObject($value)
35 6
        && $this->isConfigObject($this->{$key});
36
  }
37
38 3
  private function keyForMerge($key) {
39 3
    return is_numeric($key) ? $this->nextNumericIndex() : $key;
40 3
  }
41 3
42
  private function nextNumericIndex() {
43
    $numericKeys = array_filter(array_keys($this->rawValues()), 'is_numeric');
44
    if (count($numericKeys) === 0) return 0;
45
    return max($numericKeys) + 1;
46
  }
47
}