Passed
Push — Facets ( b72148...68bbc0 )
by Daniel
07:49 queued 03:58
created

SnakObject   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 59.52%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 148
wmc 21
lcom 1
cbo 4
ccs 25
cts 42
cp 0.5952
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 5
A getPropertyId() 0 3 1
A getHash() 0 3 1
A equals() 0 9 4
A serialize() 0 3 1
A unserialize() 0 3 1
A hasFacet() 0 3 2
A listFacets() 0 3 2
A getFacet() 0 7 2
A addFacet() 0 7 2
1
<?php
2
3
namespace Wikibase\DataModel\Snak;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Entity\Property;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Facet\FacetContainer;
10
use Wikibase\DataModel\Facet\NoSuchFacetException;
11
use Wikibase\DataModel\Internal\FacetManager;
12
13
/**
14
 * Base class for snaks.
15
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#Snaks
16
 *
17
 * @since 0.1
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 * @author Daniel Kinzler
22
 */
23
abstract class SnakObject implements Snak, FacetContainer {
24
25
	/**
26
	 * @var FacetManager|null
27
	 */
28
	private $facetManager = null;
29
30
	/**
31
	 * @since 0.1
32
	 *
33
	 * @var PropertyId
34
	 */
35
	protected $propertyId;
36
37
	/**
38
	 * Support for passing in an EntityId instance that is not a PropertyId instance has
39
	 * been deprecated since 0.5.
40
	 *
41
	 * @since 0.1
42
	 *
43
	 * @param PropertyId|EntityId|int $propertyId
44
	 *
45
	 * @throws InvalidArgumentException
46
	 */
47 40
	public function __construct( $propertyId ) {
48 40
		if ( is_int( $propertyId ) ) {
49 4
			$propertyId = PropertyId::newFromNumber( $propertyId );
50 4
		}
51
52 40
		if ( !( $propertyId instanceof EntityId ) ) {
53 9
			throw new InvalidArgumentException( '$propertyId must be an instance of EntityId' );
54
		}
55
56 31
		if ( $propertyId->getEntityType() !== Property::ENTITY_TYPE ) {
57 3
			throw new InvalidArgumentException( '$propertyId must have an entityType of ' . Property::ENTITY_TYPE );
58
		}
59
60 28
		if ( !( $propertyId instanceof PropertyId ) ) {
61
			$propertyId = new PropertyId( $propertyId->getSerialization() );
62
		}
63
64 28
		$this->propertyId = $propertyId;
65 28
	}
66
67
	/**
68
	 * @see PropertyIdProvider::getPropertyId
69
	 *
70
	 * @since 0.1
71
	 *
72
	 * @return PropertyId
73
	 */
74 3
	public function getPropertyId() {
75 3
		return $this->propertyId;
76
	}
77
78
	/**
79
	 * @see Hashable::getHash
80
	 *
81
	 * @return string
82
	 */
83 16
	public function getHash() {
84 16
		return sha1( serialize( $this ) );
85
	}
86
87
	/**
88
	 * @see Comparable::equals
89
	 *
90
	 * @since 0.3
91
	 *
92
	 * @param mixed $target
93
	 *
94
	 * @return bool
95
	 */
96 12
	public function equals( $target ) {
97 12
		if ( $this === $target ) {
98
			return true;
99
		}
100
101 12
		return is_object( $target )
102 12
			&& get_called_class() === get_class( $target )
103 12
			&& $this->getHash() === $target->getHash();
104
	}
105
106
	/**
107
	 * @see Serializable::serialize
108
	 *
109
	 * @since 0.1
110
	 *
111
	 * @return string
112
	 */
113 12
	public function serialize() {
114 12
		return serialize( $this->propertyId->getNumericId() );
115
	}
116
117
	/**
118
	 * @see Serializable::unserialize
119
	 *
120
	 * @since 0.1
121
	 *
122
	 * @param string $serialized
123
	 */
124 2
	public function unserialize( $serialized ) {
125 2
		$this->propertyId = PropertyId::newFromNumber( unserialize( $serialized ) );
126 2
	}
127
128
	/**
129
	 * @param string $name
130
	 *
131
	 * @return boolean
132
	 */
133
	public function hasFacet( $name ) {
134
		return $this->facetManager && $this->facetManager->hasFacet( $name );
135
	}
136
137
	/**
138
	 * @return string[]
139
	 */
140
	public function listFacets() {
141
		return $this->facetManager ? $this->facetManager->listFacets() : array();
142
	}
143
144
	/**
145
	 * @param string $name
146
	 * @param string|null $type The desired type
147
	 *
148
	 * @return object
149
	 */
150
	public function getFacet( $name, $type = null ) {
151
		if ( !$this->facetManager ) {
152
			throw new NoSuchFacetException( $name );
153
		}
154
155
		return $this->facetManager->getFacet( $name, $type );
156
	}
157
158
	/**
159
	 * @param string $name
160
	 * @param object $facetObject
161
	 */
162
	public function addFacet( $name, $facetObject ) {
163
		if ( !$this->facetManager ) {
164
			$this->facetManager = new FacetManager();
165
		}
166
167
		$this->facetManager->addFacet( $name, $facetObject );
168
	}
169
170
}
171