Skip to content

Commit 65ca5c6

Browse files
committed
Merge pull request #634 from wmde/referencelistfix
Fix ReferenceList not removing all reference with same hash
2 parents 32c63da + f7a5d02 commit 65ca5c6

2 files changed

Lines changed: 64 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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ public function testRemoveReference( ReferenceList $array ) {
157157
$this->assertTrue( true );
158158
}
159159

160+
public function testRemoveReferenceRemovesIdenticalObjects() {
161+
$reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) );
162+
$references = new ReferenceList( array( $reference, $reference ) );
163+
164+
$references->removeReference( $reference );
165+
166+
$this->assertTrue( $references->isEmpty() );
167+
}
168+
169+
public function testRemoveReferenceDoesNotRemoveCopies() {
170+
$reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) );
171+
$references = new ReferenceList( array( $reference, clone $reference ) );
172+
173+
$references->removeReference( $reference );
174+
175+
$this->assertFalse( $references->isEmpty() );
176+
$this->assertTrue( $references->hasReference( $reference ) );
177+
$this->assertNotSame( $reference, $references->getReference( $reference->getHash() ) );
178+
}
179+
160180
public function testAddReferenceOnEmptyList() {
161181
$reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) );
162182

@@ -330,6 +350,36 @@ public function testRemoveReferenceHash( ReferenceList $references ) {
330350
$this->assertEquals( 0, count( $references ) );
331351
}
332352

353+
public function testRemoveReferenceHashRemovesIdenticalObjects() {
354+
$reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) );
355+
$references = new ReferenceList( array( $reference, $reference ) );
356+
357+
$references->removeReferenceHash( $reference->getHash() );
358+
359+
$this->assertTrue( $references->isEmpty() );
360+
}
361+
362+
public function testRemoveReferenceHashDoesNotRemoveCopies() {
363+
$reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) );
364+
$references = new ReferenceList( array( $reference, clone $reference ) );
365+
366+
$references->removeReferenceHash( $reference->getHash() );
367+
368+
$this->assertFalse( $references->isEmpty() );
369+
$this->assertTrue( $references->hasReference( $reference ) );
370+
$this->assertNotSame( $reference, $references->getReference( $reference->getHash() ) );
371+
}
372+
373+
public function testRemoveReferenceHashUpdatesIndexes() {
374+
$reference1 = new Reference( array( new PropertyNoValueSnak( 1 ) ) );
375+
$reference2 = new Reference( array( new PropertyNoValueSnak( 2 ) ) );
376+
$references = new ReferenceList( array( $reference1, $reference2 ) );
377+
378+
$references->removeReferenceHash( $reference1->getHash() );
379+
380+
$this->assertSame( 0, $references->indexOf( $reference2 ) );
381+
}
382+
333383
public function testGivenOneSnak_addNewReferenceAddsSnak() {
334384
$references = new ReferenceList();
335385
$snak = new PropertyNoValueSnak( 1 );

0 commit comments

Comments
 (0)