MutableConfig::nextNumericIndex()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 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
}