Map   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 144
ccs 45
cts 72
cp 0.625
rs 9.6
c 0
b 0
f 0
wmc 35

15 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceWith() 0 6 3
A offsetSet() 0 6 2
B mergeWith() 0 22 7
A asArray() 0 9 3
A __get() 0 3 1
A __isset() 0 3 1
A jsonSerialize() 0 3 1
A __toString() 0 3 1
A __set() 0 3 1
A offsetExists() 0 3 1
A __clone() 0 5 3
A offsetGet() 0 11 3
A offsetUnset() 0 3 1
A getIterator() 0 3 1
A __construct() 0 14 6
1
<?php
2
namespace SamIT\Yii2\Components;
3
4
5
use yii\helpers\Json;
6
7
class Map implements \ArrayAccess, \JsonSerializable, \IteratorAggregate
8
{
9
    protected $data = [];
10
11 18
    public function __construct($data = [])
12
    {
13 18
        if (is_string($data)) {
14 5
            $this->data = json_decode($data, true);
15 17
        } elseif ($data === null || $data instanceof NullObject) {
16 6
            $this->data = [];
17 15
        } elseif ($data instanceof self) {
18 1
            $this->replaceWith($data);
19
        } else {
20 15
            $this->data = $data;
21
        }
22
23 18
        if(!is_array($this->data)) {
24 2
            throw new \DomainException('Data must be array or json encoded array');
25
        }
26
27 16
    }
28
29
30
    public function __isset($name)
31
    {
32
        return $this->offsetExists($name);
33
    }
34
35
36
37 11
    public function offsetExists($offset)
38
    {
39 11
        return isset($this->data[$offset]);
40
    }
41
42 7
    public function offsetGet($offset)
43
    {
44 7
        if (array_key_exists($offset, $this->data)) {
45 7
            if (is_array($this->data[$offset])) {
46 5
                $this->data[$offset] = new self($this->data[$offset]);
47
            }
48 7
            $result = $this->data[$offset];
49
        } else {
50 2
            $result = $this->data[$offset] = new Map();
51
        }
52 7
        return $result;
53
    }
54
55 3
    public function offsetSet($offset, $value)
56
    {
57 3
        if ($value instanceof self) {
58
            $this->data[$offset] = clone $value;
59
        } else {
60 3
            $this->data[$offset] = $value;
61
        }
62 3
    }
63
64 1
    public function offsetUnset($offset)
65
    {
66 1
        unset($this->data[$offset]);
67 1
    }
68
69 2
    public function __toString()
70
    {
71 2
        return Json::encode($this->data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
72
    }
73
74 1
    public function jsonSerialize()
75
    {
76 1
        return $this->data;
77
    }
78
79 1
    public function __get($name)
80
    {
81 1
        return $this->offsetGet($name);
82
    }
83
84
    public function __set($name, $value)
85
    {
86
        $this->offsetSet($name, $value);
87
    }
88
89 1
    public function getIterator()
90
    {
91 1
        return new \ArrayIterator($this->data);
92
    }
93
94
    /**
95
     * Merges some extra data into the current map.
96
     * Supports recursive maps in the new data.
97
     * @param $extraData
98
     * @return mixed
99
     */
100
    public function mergeWith($extraData): void
101
    {
102
        if (is_array($extraData)) {
103
            $this->mergeWith(new self($extraData));
104
            return;
105
        }
106
107
        if ($extraData instanceof self) {
108
            foreach($extraData as $key => $value) {
109
                if (isset($this[$key])
110
                    && $value instanceof self
111
                    && $this[$key] instanceof self
112
                ) {
113
                    $this[$key]->mergeWith($value);
114
                } else {
115
                    $this[$key] = $value;
116
                }
117
            }
118
            return;
119
        }
120
121
        throw new \InvalidArgumentException("Argument must be array or Map");
122
    }
123
124 1
    public function replaceWith($data) {
125 1
        if (is_array($data)) {
126
            return $this->replaceWith(new self($data));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->replaceWith(new self($data)) targeting SamIT\Yii2\Components\Map::replaceWith() 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...
127 1
        } elseif ($data instanceof self) {
128 1
            $cloned = clone $data;
129 1
            $this->data = $cloned->data;
130
        }
131 1
    }
132
133 1
    public function __clone()
134
    {
135 1
        foreach($this->data as $key => $value) {
136 1
            if ($value instanceof self) {
137
                $this->data[$key] = clone $value;
138
            }
139
        }
140 1
    }
141
142
    public function asArray() {
143
        $result = $this->data;
144
        foreach($result as $key => $value) {
145
            if ($value instanceof self) {
146
                $result[$key] = $value->asArray();
147
            }
148
149
        }
150
        return $result;
151
    }
152
}
153