Test Failed
Push — master ( 3b6ceb...9670ea )
by Todd
01:41
created

Config::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 0
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Logikos\Util;
4
5
use Logikos\Util\Config\PathEval;
0 ignored issues
show
Bug introduced by
The type Logikos\Util\Config\PathEval was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 49
  private $values   = [];
15 49
16 44
  public function __construct(array $arrayConfig = []) {
17
    foreach($arrayConfig as $key => $value)
18 49
      $this->offsetSet($key, $value);
19 49
20
    $this->onConstruct();
21
  }
22
23
  // override this if you want to
24 44
  protected function onConstruct() {}
25 44
26
  public function isLocked() {
27
    return $this->locked;
28 7
  }
29 7
30
  public function get($key, $default = null) {
31
    return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
32 1
  }
33 1
34
  public function has($key) {
35
    return $this->offsetExists($key);
36 6
  }
37 6
38 6
  public function toArray() {
39 6
    return array_map(
40 6
        function ($value) {
41 6
          return $this->hasToArray($value) ? $value->toArray() : $value;
42
        },
43
        $this->values
44
    );
45 6
  }
46 6
47
  private function hasToArray($value): bool {
48
    return is_object($value) && method_exists($value, 'toArray');
49 4
  }
50 4
51 2
  public function path($path, $default = null, $delimiter = '.') {
52
    return (new PathEval($this))->find($path, $default, $delimiter);
53 4
  }
54
55
  # Countable
56 4
  public function count() {
57 4
    return count($this->values);
58
  }
59
60 4
61 4
  # ArrayAccess
62
  public function offsetExists($offset) {
63
    return array_key_exists($offset, $this->values);
64 4
  }
65 4
66
  public function offsetGet($offset) {
67
    if (!$this->offsetExists($offset))
68 2
      throw new \OutOfBoundsException("offset '{$offset}' does not exist");
69 2
    return $this->values[$offset];
70 2
  }
71 2
72 2
  public function offsetSet($offset, $value) {
73
    $this->blockIfLocked();
74
    $this->values[strval($offset)] = is_array($value)
75
        ? new static($value)
76
        : $value;
77
  }
78
79
  public function offsetUnset($offset) {
80
    $this->blockIfLocked();
81
    unset($this->values[strval($offset)]);
82
  }
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 2
  public function __set($offset, $value) { $this->offsetSet($offset, $value);   }
95 2
  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 4
99 4
  /**
100
   * @param array $data
101
   * @return static
102
   */
103 3
  public static function __set_state(array $data): Config {
104 3
    return new static($data);
105
  }
106
107
  public function lock() {
108
    $this->locked = true;
109 30
  }
110 30
111
  protected function rawValues() {
112
    return $this->values;
113 18
  }
114 18
115 2
  protected function isConfigObject($value) {
116 16
    return $value instanceof Config;
117
  }
118
119 44
  private function blockIfLocked() {
120 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...
121 44
      throw new CanNotMutateException();
122
  }
123
}