Completed
Push — master ( 1f3396...4932db )
by Cristiano
02:20
created

ArrayObject   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 208
ccs 52
cts 52
cp 1
rs 10
wmc 23

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A keys() 0 2 1
A slice() 0 2 1
A splice() 0 5 2
A shift() 0 2 1
A serialize() 0 2 1
A append() 0 7 2
A clear() 0 3 1
A offsetUnset() 0 2 1
A flip() 0 3 1
A offsetExists() 0 2 1
A offsetSet() 0 3 2
A values() 0 2 1
A join() 0 2 1
A offsetGet() 0 2 1
A unserialize() 0 4 1
A merge() 0 3 1
A prepend() 0 6 2
A getIterator() 0 2 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Phootwork package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license MIT License
8
 * @copyright Thomas Gossmann
9
 */
10
11
namespace phootwork\lang;
12
13
use phootwork\lang\parts\AccessorsPart;
14
use phootwork\lang\parts\AddAllPart;
15
use phootwork\lang\parts\AddPart;
16
use phootwork\lang\parts\EachPart;
17
use phootwork\lang\parts\IndexFindersPart;
18
use phootwork\lang\parts\PopPart;
19
use phootwork\lang\parts\ReducePart;
20
use phootwork\lang\parts\RemovePart;
21
use phootwork\lang\parts\ReversePart;
22
use phootwork\lang\parts\SortAssocPart;
23
24
class ArrayObject extends AbstractArray implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializable, Arrayable {
25
26
	use AccessorsPart;
27
	use AddAllPart;
28
	use AddPart;
29
	use EachPart;
30
	use IndexFindersPart;
31
	use PopPart;
32
	use ReducePart;
33
	use RemovePart;
34
	use ReversePart;
35
	use SortAssocPart;
36
37 46
	public function __construct(array $contents = []) {
38 46
		$this->array = $contents;
39 46
	}
40
41 2
	public function getIterator(): \ArrayIterator {
42 2
		return new \ArrayIterator($this->array);
43
	}
44
45 1
	public function serialize(): string {
46 1
		return serialize($this->array);
47
	}
48
49
	/**
50
	 * @psalm-suppress ImplementedReturnTypeMismatch
51
	 * @psalm-suppress InvalidReturnType The return type should be `void` for consistency with \Serializable interface,
52
	 *                                   but we want fluid interface.
53
	 */
54 1
	public function unserialize($serialized): self {
55 1
		$this->array = unserialize($serialized);
56
57 1
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type phootwork\lang\ArrayObject which is incompatible with the return type mandated by Serializable::unserialize() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
58
	}
59
60
	/**
61
	 * Resets the array
62
	 *
63
	 * @return $this
64
	 */
65 1
	public function clear(): self {
66 1
		$this->array = [];
67 1
		return $this;
68
	}
69
70
	//
71
	//
72
	// MUTATIONS
73
	//
74
	//
75
76
	/**
77
	 * Append one or more elements onto the end of array
78
	 *
79
	 * @param array $elements
80
	 * @return $this
81
	 */
82 7
	public function append(...$elements): self {
83
		// das ist doch behindi!
84 7
		foreach ($elements as $element) {
85 7
			array_push($this->array, $element);
86
		}
87
88 7
		return $this;
89
	}
90
91
	/**
92
	 * Prepend one or more elements to the beginning of the array
93
	 *
94
	 * @param array $elements
95
	 * @return $this
96
	 */
97 1
	public function prepend(...$elements): self {
98
		// das ist doch auch behindi!
99 1
		foreach ($elements as $element) {
100 1
			array_unshift($this->array, $element);
101
		}
102 1
		return $this;
103
	}
104
105
	/**
106
	 * Shift an element off the beginning of array
107
	 *
108
	 * @return mixed the shifted element
109
	 */
110 1
	public function shift() {
111 1
		return array_shift($this->array);
112
	}
113
114
	/**
115
	 * Remove a portion of the array and replace it with something else
116
	 *
117
	 * @param int $offset If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array.
118
	 * @param int $length If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. If length is specified and is zero, no elements will be removed.
119
	 * @param array $replacement If replacement array is specified, then the removed elements are replaced with elements from this array. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. Note that keys in replacement array are not preserved. If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself, an object or NULL.
120
	 * @return $this
121
	 */
122 1
	public function splice(int $offset, ?int $length = null, array $replacement = []): self {
123 1
		$length = $length === null ? $this->count() : $length;
124 1
		array_splice($this->array, $offset, $length, $replacement);
125
126 1
		return $this;
127
	}
128
129
	//
130
	//
131
	// SUGAR
132
	//
133
	//
134
135
	/**
136
	 * Joins the array with a string
137
	 *
138
	 * @param string $glue Defaults to an empty string.
139
	 * @return Text
140
	 * 		Returns a string containing a string representation of all the array elements in the
141
	 * 		same order, with the glue string between each element.
142
	 */
143 4
	public function join(string $glue = ''): Text {
144 4
		return new Text(implode($glue, $this->array));
145
	}
146
147
	/**
148
	 * Extract a slice of the array
149
	 *
150
	 * @param int $offset
151
	 * @param int $length
152
	 * @param boolean $preserveKeys
153
	 * @return ArrayObject
154
	 */
155 1
	public function slice(int $offset, ?int $length = null, bool $preserveKeys = false): ArrayObject {
156 1
		return new ArrayObject(array_slice($this->array, $offset, $length, $preserveKeys));
157
	}
158
159
	/**
160
	 * Merges in other values
161
	 *
162
	 * @param mixed ... Variable list of arrays to merge.
163
	 * @return ArrayObject $this
164
	 */
165 1
	public function merge(): self {
166 1
		$this->array = array_merge($this->array, func_get_args());
167 1
		return $this;
168
	}
169
170
	/**
171
	 * Returns the keys of the array
172
	 *
173
	 * @return ArrayObject the keys
174
	 */
175 1
	public function keys(): ArrayObject {
176 1
		return new ArrayObject(array_keys($this->array));
177
	}
178
179
	/**
180
	 * Returns the values of the array
181
	 *
182
	 * @return ArrayObject the values
183
	 */
184 1
	public function values(): ArrayObject {
185 1
		return new ArrayObject(array_values($this->array));
186
	}
187
188
	/**
189
	 * Flips keys and values
190
	 *
191
	 * @return ArrayObject $this
192
	 */
193 1
	public function flip(): self {
194 1
		$this->array = array_flip($this->array);
195 1
		return $this;
196
	}
197
198
	//
199
	//
200
	// INTERNALS
201
	//
202
	//
203
204
	/**
205
	 * @internal
206
	 */
207 2
	public function offsetSet($offset, $value) {
208 2
		if (!is_null($offset)) {
209 2
			$this->array[$offset] = $value;
210
		}
211 2
	}
212
213
	/**
214
	 * @internal
215
	 */
216 3
	public function offsetExists($offset) {
217 3
		return isset($this->array[$offset]);
218
	}
219
220
	/**
221
	 * @internal
222
	 */
223 1
	public function offsetUnset($offset) {
224 1
		unset($this->array[$offset]);
225 1
	}
226
227
	/**
228
	 * @internal
229
	 */
230 4
	public function offsetGet($offset) {
231 4
		return $this->array[$offset] ?? null;
232
	}
233
}
234