Passed
Push — master ( 9670ea...dc3621 )
by Todd
02:03
created

Config   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 110
ccs 51
cts 51
cp 1
rs 9.2
c 0
b 0
f 0
wmc 34

27 Methods

Rating   Name   Duplication   Size   Complexity  
A isConfigObject() 0 2 1
A valid() 0 1 1
A __get() 0 1 1
A blockIfLocked() 0 3 2
A __set_state() 0 2 1
A key() 0 1 1
A __isset() 0 1 1
A count() 0 2 1
A next() 0 1 1
A current() 0 1 1
A rewind() 0 1 1
A rawValues() 0 2 1
A hasToArray() 0 2 2
A lock() 0 2 1
A get() 0 2 2
A offsetExists() 0 2 1
A path() 0 2 1
A isLocked() 0 2 1
A offsetSet() 0 5 2
A __set() 0 1 1
A offsetUnset() 0 3 1
A toArray() 0 6 2
A has() 0 2 1
A __unset() 0 1 1
A onConstruct() 0 1 1
A offsetGet() 0 4 2
A __construct() 0 5 2
1
<?php
2
3
namespace Logikos\Util;
4
5
use Logikos\Util\Config\PathEval;
6
7
/**
8
 * This is largely inspired by \Phalcon\Config - https://docs.phalconphp.com/hr/3.2/api/Phalcon_Config
9
 * NOTICE: \Phalcon\Config will be much faster than this class and you are encouraged to use it
10
 * @see ../docs/config/README.md
11
 */
12
abstract class Config implements \ArrayAccess, \Countable, \Iterator {
13
  private $locked   = false;
14
  private $values   = [];
15
16 50
  public function __construct(array $arrayConfig = []) {
17 50
    foreach($arrayConfig as $key => $value)
18 45
      $this->offsetSet($key, $value);
19
20 50
    $this->onConstruct();
21 50
  }
22
23
  // override this if you want to
24
  protected function onConstruct() {}
25
26 45
  public function isLocked() {
27 45
    return $this->locked;
28
  }
29
30 8
  public function get($key, $default = null) {
31 8
    return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
32
  }
33
34 1
  public function has($key) {
35 1
    return $this->offsetExists($key);
36
  }
37
38 6
  public function toArray() {
39 6
    return array_map(
40 6
        function ($value) {
41 6
          return $this->hasToArray($value) ? $value->toArray() : $value;
42 6
        },
43 6
        $this->values
44
    );
45
  }
46
47 6
  private function hasToArray($value): bool {
48 6
    return is_object($value) && method_exists($value, 'toArray');
49
  }
50
51 5
  public function path($path, $default = null, $delimiter = '.') {
52 5
    return (new PathEval($this))->find($path, $default, $delimiter);
53
  }
54
55
  # Countable
56 3
  public function count() {
57 3
    return count($this->values);
58
  }
59
60
61
  # ArrayAccess
62 31
  public function offsetExists($offset) {
63 31
    return array_key_exists($offset, $this->values);
64
  }
65
66 19
  public function offsetGet($offset) {
67 19
    if (!$this->offsetExists($offset))
68 2
      throw new \OutOfBoundsException("offset '{$offset}' does not exist");
69 17
    return $this->values[$offset];
70
  }
71
72 45
  public function offsetSet($offset, $value) {
73 45
    $this->blockIfLocked();
74 45
    $this->values[strval($offset)] = is_array($value)
75 11
        ? new static($value)
76 45
        : $value;
77 45
  }
78
79 4
  public function offsetUnset($offset) {
80 4
    $this->blockIfLocked();
81 2
    unset($this->values[strval($offset)]);
82 2
  }
83
84
85
  # Iterator
86
  public function rewind()  { return reset($this->values);        }
87
  public function key()     { return key($this->values);          }
88
  public function current() { return current($this->values);      }
89
  public function next()    { return next($this->values);         }
90
  public function valid()   { return key($this->values) !== null; }
91
92
93
  # Magic Property Access
94
  public function __set($offset, $value) { $this->offsetSet($offset, $value);   }
95
  public function __unset($offset)       { $this->offsetUnset($offset);         }
96
  public function __get($offset)         { return $this->offsetGet($offset);    }
97
  public function __isset($offset)       { return $this->offsetExists($offset); }
98
99
  /**
100
   * @param array $data
101
   * @return static
102
   */
103 1
  public static function __set_state(array $data): Config {
104 1
    return new static($data);
105
  }
106
107 12
  public function lock() {
108 12
    $this->locked = true;
109 12
  }
110
111 5
  protected function rawValues() {
112 5
    return $this->values;
113
  }
114
115 5
  protected function isConfigObject($value) {
116 5
    return $value instanceof Config;
117
  }
118
119 45
  private function blockIfLocked() {
120 45
    if ($this->isLocked())
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->isLocked() targeting Logikos\Util\Config::isLocked() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121 6
      throw new CanNotMutateException();
122
  }
123
}