Test Failed
Push — master ( 24e4d5...d08b25 )
by Todd
02:18
created

Config   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 35
dl 0
loc 114
rs 9
c 0
b 0
f 0

26 Methods

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