Skip to content

Commit dcf592f

Browse files
committed
Drop index parameter from Claims::addClaim
1 parent 028ed44 commit dcf592f

3 files changed

Lines changed: 13 additions & 45 deletions

File tree

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Removed `ClaimList`
1313
* Removed `ClaimListAccess`
1414
* Removed `addClaim`, `hasClaims` and `newClaim` from all entity classes
15+
* `Claims::addClaim` no longer supports setting an index
1516
* Removed `Claims::getBestClaims` (you can use `StatementList::getBestStatements` instead)
1617
* Removed `Claims::getByRank` and `Claims::getByRanks` (you can use `StatementList::getWithRank` instead)
1718
* Removed `Claims::getMainSnaks` (you can use `StatementList::getMainSnaks` instead)

src/Claim/Claims.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
use Hashable;
88
use InvalidArgumentException;
99
use Traversable;
10-
use Wikibase\DataModel\ByPropertyIdGrouper;
11-
use Wikibase\DataModel\Entity\PropertyId;
12-
use Wikibase\DataModel\Snak\Snak;
13-
use Wikibase\DataModel\Statement\Statement;
1410

1511
/**
1612
* A claim (identified using it's GUID) can only be added once.
@@ -83,38 +79,15 @@ private function getClaimKey( Claim $claim ) {
8379
* @since 0.1
8480
*
8581
* @param Claim $claim
86-
* @param int|null $index
8782
*
8883
* @throws InvalidArgumentException
8984
*/
90-
public function addClaim( Claim $claim, $index = null ) {
91-
if ( !is_null( $index ) && !is_integer( $index ) ) {
92-
throw new InvalidArgumentException( '$index must be an integer or null; got ' . gettype( $index ) );
93-
} elseif ( is_null( $index ) || $index >= count( $this ) ) {
94-
$this[] = $claim;
95-
} else {
96-
$this->insertClaimAtIndex( $claim, $index );
97-
}
98-
}
99-
100-
/**
101-
* @param Claim $claim
102-
* @param int $index
103-
*/
104-
private function insertClaimAtIndex( Claim $claim, $index ) {
105-
// Determine the claims to shift and remove them from the array:
106-
$claimsToShift = array_slice( (array)$this, $index );
107-
108-
foreach ( $claimsToShift as $object ) {
109-
$this->offsetUnset( $this->getClaimKey( $object ) );
85+
public function addClaim( Claim $claim ) {
86+
if ( func_num_args() > 1 ) {
87+
throw new InvalidArgumentException( '$index is not supported any more' );
11088
}
11189

112-
// Append the new claim and re-append the previously removed claims:
11390
$this[] = $claim;
114-
115-
foreach ( $claimsToShift as $object ) {
116-
$this[] = $object;
117-
}
11891
}
11992

12093
/**

tests/unit/Claim/ClaimsTest.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,27 +234,21 @@ public function testAddClaim() {
234234
$this->assertNotNull( $claims->getClaimWithGuid( $claim1->getGuid() ) );
235235
$this->assertNotNull( $claims->getClaimWithGuid( $claim2->getGuid() ) );
236236

237-
// Insert claim at the beginning:
238-
$claim3 = $this->makeClaim( new PropertyNoValueSnak( new PropertyId( 'P17' ) ) );
239-
$claims->addClaim( $claim3, 0 );
240-
241-
// Insert claim at another index:
242-
$claim4 = $this->makeClaim( new PropertyNoValueSnak( new PropertyId( 'P18' ) ) );
243-
$claims->addClaim( $claim4, 1 );
244-
245-
// Insert claim with an index out of bounds:
246-
$claim5 = $this->makeClaim( new PropertyNoValueSnak( new PropertyId( 'P19' ) ) );
247-
$claims->addClaim( $claim5, 99999 );
248-
249237
$this->assertSame( array(
250-
'TEST$CLAIM-3' => $claim3,
251-
'TEST$CLAIM-4' => $claim4,
252238
'TEST$CLAIM-1' => $claim1,
253239
'TEST$CLAIM-2' => $claim2,
254-
'TEST$CLAIM-5' => $claim5,
255240
), $claims->getArrayCopy() );
256241
}
257242

243+
/**
244+
* @expectedException InvalidArgumentException
245+
*/
246+
public function testAddClaimWithIndexFails() {
247+
$claims = new Claims();
248+
$claim = new Claim( new PropertyNoValueSnak( 42 ) );
249+
$claims->addClaim( $claim, 0 );
250+
}
251+
258252
public function testAppend() {
259253
$claims = new Claims();
260254
$claim1 = $this->makeClaim( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) );

0 commit comments

Comments
 (0)