Completed
Pull Request — master (#654)
by no
05:53 queued 03:18
created

ReferenceList::addReference()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 9
cts 9
cp 1
rs 6.6037
cc 8
eloc 12
nc 5
nop 2
crap 8
1
<?php
2
3
namespace Wikibase\DataModel;
4
5
use ArrayIterator;
6
use Comparable;
7
use Countable;
8
use InvalidArgumentException;
9
use IteratorAggregate;
10
use Serializable;
11
use Traversable;
12
use Wikibase\DataModel\Internal\MapValueHasher;
13
use Wikibase\DataModel\Snak\Snak;
14
15
/**
16
 * List of Reference objects.
17
 *
18
 * @since 0.1
19
 * Does not implement References anymore since 2.0
20
 * Does not extend SplObjectStorage since 5.0
21
 *
22
 * @license GPL-2.0+
23
 * @author Jeroen De Dauw < [email protected] >
24
 * @author H. Snater < [email protected] >
25
 * @author Thiemo Mättig
26
 * @author Bene* < [email protected] >
27
 */
28
class ReferenceList implements Comparable, Countable, IteratorAggregate, Serializable {
29
30
	/**
31
	 * @var Reference[] Ordered list or references, indexed by SPL object hash.
32
	 */
33
	private $references = [];
34 29
35 29
	/**
36 8
	 * @param Reference[]|Traversable $references
37
	 *
38
	 * @throws InvalidArgumentException
39 21
	 */
40 12
	public function __construct( $references = [] ) {
41 4
		if ( !is_array( $references ) && !( $references instanceof Traversable ) ) {
42
			throw new InvalidArgumentException( '$references must be an array or an instance of Traversable' );
43
		}
44 8
45 17
		foreach ( $references as $reference ) {
46 17
			if ( !( $reference instanceof Reference ) ) {
47
				throw new InvalidArgumentException( 'Every element in $references must be an instance of Reference' );
48
			}
49
50
			$this->addReference( $reference );
51
		}
52
	}
53
54
	/**
55
	 * Adds the provided reference to the list.
56
	 * Empty references are ignored.
57
	 *
58 14
	 * @since 0.1
59 14
	 *
60
	 * @param Reference $reference
61
	 * @param int|null $index New position of the added reference, or null to append.
62
	 *
63 14
	 * @throws InvalidArgumentException
64
	 */
65 14
	public function addReference( Reference $reference, $index = null ) {
66 14
		if ( $index !== null && ( !is_int( $index ) || $index < 0 ) ) {
67 2
			throw new InvalidArgumentException( '$index must be a non-negative integer or null' );
68
		}
69 14
70
		if ( $reference->isEmpty() ) {
71
			return;
72
		}
73
74
		$splHash = spl_object_hash( $reference );
75
76
		if ( array_key_exists( $splHash, $this->references ) ) {
77 14
			return;
78 14
		}
79 14
80 14
		if ( $index === null || $index >= count( $this->references ) ) {
81 14
			// Append object to the end of the reference list.
82
			$this->references[$splHash] = $reference;
83
		} else {
84
			$this->insertReferenceAtIndex( $reference, $index );
85
		}
86
	}
87
88
	/**
89
	 * @since 1.1
90
	 *
91 6
	 * @param Snak[]|Snak $snaks
92 6
	 * @param Snak [$snak2,...]
93 5
	 *
94 5
	 * @throws InvalidArgumentException
95
	 */
96 6
	public function addNewReference( $snaks = [] /*...*/ ) {
97 5
		if ( $snaks instanceof Snak ) {
98
			$snaks = func_get_args();
99
		}
100
101
		$this->addReference( new Reference( $snaks ) );
102
	}
103 2
104 2
	/**
105 2
	 * @param Reference $reference
106
	 * @param int $index
107
	 */
108 2
	private function insertReferenceAtIndex( Reference $reference, $index ) {
109 2
		$splHash = spl_object_hash( $reference );
110 2
111 2
		$this->references = array_merge(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge(array_slice(...s->references, $index)) of type array<integer|string,obj...e\DataModel\Reference>> is incompatible with the declared type array<integer,object<Wik...e\DataModel\Reference>> of property $references.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
112 2
			array_slice( $this->references, 0, $index ),
113
			[ $splHash => $reference ],
114 2
			array_slice( $this->references, $index )
115 2
		);
116 2
	}
117
118
	/**
119 2
	 * Returns if the list contains a reference with the same hash as the provided reference.
120
	 *
121 2
	 * @since 0.1
122 2
	 *
123 2
	 * @param Reference $reference
124 2
	 *
125
	 * @return bool
126
	 */
127
	public function hasReference( Reference $reference ) {
128
		return $this->hasReferenceHash( $reference->getHash() );
129
	}
130
131
	/**
132
	 * Returns the index of the Reference object or false if the Reference could not be found.
133
	 *
134
	 * @since 0.5
135 6
	 *
136 6
	 * @param Reference $reference
137 6
	 *
138
	 * @return int|bool
139
	 */
140
	public function indexOf( Reference $reference ) {
141
		$index = 0;
142
143
		foreach ( $this->references as $ref ) {
144
			if ( $ref === $reference ) {
145
				return $index;
146
			}
147
148
			$index++;
149 2
		}
150 2
151
		return false;
152 2
	}
153 1
154 1
	/**
155
	 * Removes the reference with the same hash as the provided reference if such a reference exists in the list.
156 1
	 *
157 2
	 * @since 0.1
158
	 *
159 2
	 * @param Reference $reference
160
	 */
161
	public function removeReference( Reference $reference ) {
162
		$this->removeReferenceHash( $reference->getHash() );
163
	}
164
165
	/**
166
	 * Returns if the list contains a reference with the provided hash.
167
	 *
168
	 * @since 0.3
169 3
	 *
170 3
	 * @param string $referenceHash
171 3
	 *
172
	 * @return bool
173
	 */
174
	public function hasReferenceHash( $referenceHash ) {
175
		return $this->getReference( $referenceHash ) !== null;
176
	}
177
178
	/**
179
	 * Looks for the first Reference object in this list with the provided hash.
180
	 * Removes all occurences of that object.
181
	 *
182 8
	 * @since 0.3
183 8
	 *
184
	 * @param string $referenceHash	`
185
	 */
186
	public function removeReferenceHash( $referenceHash ) {
187
		$reference = $this->getReference( $referenceHash );
188
189
		if ( $reference === null ) {
190
			return;
191
		}
192
193 5
		foreach ( $this->references as $splObjectHash => $ref ) {
194 5
			if ( $ref === $reference ) {
195
				unset( $this->references[$splObjectHash] );
196 5
			}
197 3
		}
198 3
	}
199 5
200
	/**
201
	 * Returns the first Reference object with the provided hash, or
202
	 * null if there is no such reference in the list.
203
	 *
204
	 * @since 0.3
205
	 *
206
	 * @param string $referenceHash
207
	 *
208
	 * @return Reference|null
209
	 */
210 14
	public function getReference( $referenceHash ) {
211
		foreach ( $this->references as $reference ) {
212
			if ( $reference->getHash() === $referenceHash ) {
213
				return $reference;
214 14
			}
215 10
		}
216 10
217
		return null;
218 9
	}
219
220 9
	/**
221
	 * @see Serializable::serialize
222
	 *
223
	 * @since 2.1
224
	 *
225
	 * @return string
226
	 */
227
	public function serialize() {
228
		return serialize( array_values( $this->references ) );
229
	}
230 3
231 3
	/**
232
	 * @see Serializable::unserialize
233
	 *
234
	 * @since 2.1
235
	 *
236
	 * @param string $serialized
237
	 */
238
	public function unserialize( $serialized ) {
239
		$this->__construct( unserialize( $serialized ) );
240
	}
241 3
242 3
	/**
243 3
	 * @since 4.4
244
	 *
245
	 * @return bool
246
	 */
247
	public function isEmpty() {
248
		return empty( $this->references );
249
	}
250 3
251 3
	/**
252
	 * The hash is purely valuer based. Order of the elements in the array is not held into account.
253
	 *
254
	 * @since 0.3
255
	 *
256
	 * @return string
257
	 */
258
	public function getValueHash() {
259
		$hasher = new MapValueHasher();
260
		return $hasher->hash( $this->references );
261
	}
262
263
	/**
264
	 * @see Comparable::equals
265
	 *
266
	 * The comparison is done purely value based, ignoring the order of the elements in the array.
267
	 *
268
	 * @since 0.3
269
	 *
270
	 * @param mixed $target
271
	 *
272
	 * @return bool
273
	 */
274
	public function equals( $target ) {
275
		if ( $this === $target ) {
276
			return true;
277
		}
278
279
		return $target instanceof self
280
		       && $this->getValueHash() === $target->getValueHash();
281
	}
282
283
	/**
284
	 * @see Countable::count
285
	 *
286
	 * @return int
287
	 */
288
	public function count() {
289
		return count( $this->references );
290
	}
291
292
	/**
293
	 * @see IteratorAggregate::getIterator
294
	 *
295
	 * @since 5.0
296
	 *
297
	 * @return Traversable
298
	 */
299
	public function getIterator() {
300
		return new ArrayIterator( array_values( $this->references ) );
301
	}
302
303
}
304