Passed
Push — master ( bf9aa7...532905 )
by Todd
01:48
created

Config::blockIfLocked()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 1
nop 0
crap 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 49
  public function __construct(array $arrayConfig = []) {
15 49
    foreach($arrayConfig as $key => $value)
16 44
      $this->offsetSet($key, $value);
17
18 49
    $this->onConstruct();
19 49
  }
20
21
  // override this if you want to
22
  protected function onConstruct() {}
23
24 44
  public function isLocked() {
25 44
    return $this->locked;
26
  }
27
28 7
  public function get($key, $default = null) {
29 7
    return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
30
  }
31
32 1
  public function has($key) {
33 1
    return $this->offsetExists($key);
34
  }
35
36 6
  public function toArray() {
37 6
    return array_map(
38 6
        function ($value) {
39 6
          return $this->hasToArray($value) ? $value->toArray() : $value;
40 6
        },
41 6
        $this->values
42
    );
43
  }
44
45 6
  private function hasToArray($value): bool {
46 6
    return is_object($value) && method_exists($value, 'toArray');
47
  }
48
49 4
  public function path($path, $default = null, $delimiter = '.') {
50 4
    $tokens = explode($delimiter, $path);
51 4
    $token  = array_shift($tokens);
52 4
    if ($this->get($token) instanceof self)
53 2
      return $this[$token]->path(implode('.', $tokens), $default);
54 4
    return $this->get($token, $default);
55
  }
56
57
58
  # Countable
59 3
  public function count() {
60 3
    return count($this->values);
61
  }
62
63
64
  # ArrayAccess
65 30
  public function offsetExists($offset) {
66 30
    return array_key_exists($offset, $this->values);
67
  }
68
69 18
  public function offsetGet($offset) {
70 18
    if (!$this->offsetExists($offset))
71 2
      throw new \OutOfBoundsException("offset '{$offset}' does not exist");
72 16
    return $this->values[$offset];
73
  }
74
75 44
  public function offsetSet($offset, $value) {
76 44
    $this->blockIfLocked();
77 44
    $this->values[strval($offset)] = is_array($value)
78 10
        ? new static($value)
79 44
        : $value;
80 44
  }
81
82 4
  public function offsetUnset($offset) {
83 4
    $this->blockIfLocked();
84 2
    unset($this->values[strval($offset)]);
85 2
  }
86
87
88
  # Iterator
89
  public function rewind()  { return reset($this->values);        }
90
  public function key()     { return key($this->values);          }
91
  public function current() { return current($this->values);      }
92
  public function next()    { return next($this->values);         }
93
  public function valid()   { return key($this->values) !== null; }
94
95
96
  # Magic Property Access
97
  public function __set($offset, $value) { $this->offsetSet($offset, $value);   }
98
  public function __unset($offset)       { $this->offsetUnset($offset);         }
99
  public function __get($offset)         { return $this->offsetGet($offset);    }
100
  public function __isset($offset)       { return $this->offsetExists($offset); }
101
102
  /**
103
   * @param array $data
104
   * @return static
105
   */
106 1
  public static function __set_state(array $data): Config {
107 1
    return new static($data);
108
  }
109
110 12
  public function lock() {
111 12
    $this->locked = true;
112 12
  }
113
114 5
  protected function rawValues() {
115 5
    return $this->values;
116
  }
117
118 5
  protected function isConfigObject($value) {
119 5
    return $value instanceof Config;
120
  }
121
122 44
  private function blockIfLocked() {
123 44
    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...
124 6
      throw new CanNotMutateException();
125
  }
126
}