Reference::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Wikibase\DataModel;
4
5
use Countable;
6
use InvalidArgumentException;
7
use Wikibase\DataModel\Snak\Snak;
8
use Wikibase\DataModel\Snak\SnakList;
9
10
/**
11
 * Object that represents a single Wikibase reference.
12
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#ReferenceRecords
13
 *
14
 * @since 0.1, instantiable since 0.4
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class Reference implements Countable {
20
21
	/**
22
	 * @var SnakList
23
	 */
24
	private $snaks;
25
26
	/**
27
	 * An array of Snak objects is only supported since version 1.1.
28
	 *
29
	 * @param Snak[]|SnakList $snaks
30
	 * @throws InvalidArgumentException
31
	 */
32
	public function __construct( $snaks = [] ) {
33
		if ( is_array( $snaks ) ) {
34
			$snaks = new SnakList( $snaks );
35 20
		}
36 20
37 8
		if ( !( $snaks instanceof SnakList ) ) {
38 7
			throw new InvalidArgumentException( '$snaks must be an array or an instance of SnakList' );
39
		}
40 19
41 9
		$this->snaks = $snaks;
42
	}
43
44 10
	/**
45 10
	 * Returns the property snaks that make up this reference.
46
	 * Modification of the snaks should NOT happen through this getter.
47
	 *
48
	 * @since 0.1
49
	 *
50
	 * @return SnakList
51
	 */
52
	public function getSnaks() {
53
		return $this->snaks;
54
	}
55 7
56 7
	/**
57
	 * @see Countable::count
58
	 *
59
	 * @since 0.3
60
	 *
61
	 * @return integer
62
	 */
63
	public function count() {
64
		return count( $this->snaks );
65
	}
66
67
	/**
68
	 * @since 2.6
69
	 *
70
	 * @return bool
71
	 */
72
	public function isEmpty() {
73
		return $this->snaks->isEmpty();
74
	}
75 1
76 1
	/**
77
	 * @since 0.1
78
	 *
79
	 * @return string
80
	 */
81
	public function getHash() {
82
		// For considering the reference snaks' property order without actually manipulating the
83
		// reference snaks's order, a new SnakList is generated. The new SnakList is ordered
84
		// by property and its hash is returned.
85
		$orderedSnaks = new SnakList( $this->snaks );
86 12
87
		$orderedSnaks->orderByProperty();
88
89
		return $orderedSnaks->getHash();
90 12
	}
91
92 12
	/**
93
	 *
94 12
	 * The comparison is done purely value based, ignoring the order of the snaks.
95
	 *
96
	 * @since 0.3
97
	 *
98
	 * @param mixed $target
99
	 *
100
	 * @return bool
101
	 */
102
	public function equals( $target ) {
103
		if ( $this === $target ) {
104
			return true;
105
		}
106
107
		return $target instanceof self
108 4
			&& $this->snaks->equals( $target->snaks );
109 4
	}
110 1
111
}
112