Skip to content

Commit f800f09

Browse files
authored
Merge pull request #713 from wmde/equalHashArrays
Move HashArray::equals to SnakList::equals
2 parents 989ec20 + fa615f6 commit f800f09

4 files changed

Lines changed: 68 additions & 34 deletions

File tree

src/HashArray.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Wikibase\DataModel;
44

55
use ArrayObject;
6-
use Comparable;
76
use Hashable;
87
use InvalidArgumentException;
98
use Traversable;
@@ -26,7 +25,7 @@
2625
* @license GPL-2.0+
2726
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
2827
*/
29-
abstract class HashArray extends ArrayObject implements Comparable {
28+
abstract class HashArray extends ArrayObject {
3029

3130
/**
3231
* Maps element hashes to their offsets.
@@ -231,26 +230,6 @@ public function offsetUnset( $index ) {
231230
}
232231
}
233232

234-
/**
235-
* @see Comparable::equals
236-
*
237-
* The comparison is done purely value based, ignoring the order of the elements in the array.
238-
*
239-
* @since 0.3
240-
*
241-
* @param mixed $target
242-
*
243-
* @return bool
244-
*/
245-
public function equals( $target ) {
246-
if ( $this === $target ) {
247-
return true;
248-
}
249-
250-
return $target instanceof self
251-
&& $this->getHash() === $target->getHash();
252-
}
253-
254233
/**
255234
* @see ArrayObject::append
256235
*

src/Snak/SnakList.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Wikibase\DataModel\Snak;
44

5+
use Comparable;
56
use Hashable;
67
use InvalidArgumentException;
78
use Traversable;
@@ -18,7 +19,7 @@
1819
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1920
* @author Addshore
2021
*/
21-
class SnakList extends HashArray implements Hashable {
22+
class SnakList extends HashArray implements Comparable, Hashable {
2223

2324
/**
2425
* @param Snak[]|Traversable $snaks
@@ -108,6 +109,26 @@ public function getSnak( $snakHash ) {
108109
return $this->getByElementHash( $snakHash );
109110
}
110111

112+
/**
113+
* @see Comparable::equals
114+
*
115+
* The comparison is done purely value based, ignoring the order of the elements in the array.
116+
*
117+
* @since 0.3
118+
*
119+
* @param mixed $target
120+
*
121+
* @return bool
122+
*/
123+
public function equals( $target ) {
124+
if ( $this === $target ) {
125+
return true;
126+
}
127+
128+
return $target instanceof self
129+
&& $this->getHash() === $target->getHash();
130+
}
131+
111132
/**
112133
* @see HashArray::getHash
113134
*

tests/unit/HashArray/HashArrayTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Wikibase\DataModel\Tests\HashArray;
44

5-
use Wikibase\DataModel\HashArray;
6-
75
/**
86
* @covers Wikibase\DataModel\HashArray
97
*
@@ -39,15 +37,6 @@ public function instanceProvider() {
3937
return $instances;
4038
}
4139

42-
/**
43-
* @dataProvider instanceProvider
44-
* @param HashArray $array
45-
*/
46-
public function testEquals( HashArray $array ) {
47-
$this->assertTrue( $array->equals( $array ) );
48-
$this->assertFalse( $array->equals( 42 ) );
49-
}
50-
5140
/**
5241
* @param array $elements
5342
*

tests/unit/Snak/SnakListTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Wikibase\DataModel\Tests\Snak;
44

5+
use Comparable;
56
use DataValues\StringValue;
67
use Hashable;
78
use InvalidArgumentException;
@@ -294,6 +295,50 @@ public function testOrderByProperty( SnakList $snakList, SnakList $expected, arr
294295
}
295296
}
296297

298+
public function testComparableInterface() {
299+
$this->assertInstanceOf( Comparable::class, new SnakList() );
300+
}
301+
302+
/**
303+
* @dataProvider equalsProvider
304+
*/
305+
public function testEquals( SnakList $list1, SnakList $list2, $expected ) {
306+
$this->assertSame( $expected, $list1->equals( $list2 ) );
307+
}
308+
309+
public function equalsProvider() {
310+
$empty = new SnakList();
311+
$oneSnak = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
312+
313+
return [
314+
'empty object is equal to itself' => [
315+
$empty,
316+
$empty,
317+
true
318+
],
319+
'non-empty object is equal to itself' => [
320+
$oneSnak,
321+
$oneSnak,
322+
true
323+
],
324+
'different empty objects are equal' => [
325+
$empty,
326+
new SnakList(),
327+
true
328+
],
329+
'different objects with same content are equal' => [
330+
$oneSnak,
331+
new SnakList( [ new PropertyNoValueSnak( 1 ) ] ),
332+
true
333+
],
334+
'different objects with different content are not equal' => [
335+
$oneSnak,
336+
new SnakList( [ new PropertyNoValueSnak( 2 ) ] ),
337+
false
338+
],
339+
];
340+
}
341+
297342
public function testHashableInterface() {
298343
$this->assertInstanceOf( Hashable::class, new SnakList() );
299344
}

0 commit comments

Comments
 (0)