Skip to content

Commit f0c5833

Browse files
committed
Restore behaviour of ReferenceList::removeReferenceHash
1 parent 32c63da commit f0c5833

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/ReferenceList.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function hasReference( Reference $reference ) {
117117
}
118118

119119
/**
120-
* Returns the index of a reference or false if the reference could not be found.
120+
* Returns the index of the Reference object or false if the Reference could not be found.
121121
*
122122
* @since 0.5
123123
*
@@ -160,29 +160,31 @@ public function hasReferenceHash( $referenceHash ) {
160160
}
161161

162162
/**
163-
* Removes the reference with the provided hash if it exists in the list.
163+
* Looks for the first Reference object in this list with the provided hash.
164+
* Removes all occurences of that object.
164165
*
165166
* @since 0.3
166167
*
167168
* @param string $referenceHash `
168169
*/
169170
public function removeReferenceHash( $referenceHash ) {
170-
foreach ( $this->references as $index => $reference ) {
171-
if ( $reference->getHash() === $referenceHash ) {
172-
$this->removeReferenceAtIndex( $index );
171+
$reference = $this->getReference( $referenceHash );
172+
173+
if ( $reference === null ) {
174+
return;
175+
}
176+
177+
foreach ( $this->references as $index => $ref ) {
178+
if ( $ref === $reference ) {
179+
unset( $this->references[$index] );
173180
}
174181
}
175-
}
176182

177-
/**
178-
* @param int $index
179-
*/
180-
private function removeReferenceAtIndex( $index ) {
181-
array_splice( $this->references, $index, 1 );
183+
$this->references = array_values( $this->references );
182184
}
183185

184186
/**
185-
* Returns the reference with the provided hash, or
187+
* Returns the first Reference object with the provided hash, or
186188
* null if there is no such reference in the list.
187189
*
188190
* @since 0.3

tests/unit/ReferenceListTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,21 @@ public function testRemoveReferenceHash( ReferenceList $references ) {
330330
$this->assertEquals( 0, count( $references ) );
331331
}
332332

333+
public function testRemoveReferenceHash_occurrencesOfSameObjectGetRemoved() {
334+
$reference = new Reference( array( new PropertyNoValueSnak( 42 ) ) );
335+
$references = new ReferenceList( array(
336+
$reference,
337+
clone $reference,
338+
$reference
339+
) );
340+
341+
$references->removeReferenceHash( $reference->getHash() );
342+
343+
$this->assertTrue( $references->hasReferenceHash( $reference->getHash() ) );
344+
$this->assertCount( 1, $references );
345+
$this->assertNotSame( $reference, $references->getReference( $reference->getHash() ) );
346+
}
347+
333348
public function testGivenOneSnak_addNewReferenceAddsSnak() {
334349
$references = new ReferenceList();
335350
$snak = new PropertyNoValueSnak( 1 );

0 commit comments

Comments
 (0)