Skip to content

Commit 7a36752

Browse files
committed
Do not let int as value in Entity::setId
Bug: T151577
1 parent 48dda32 commit 7a36752

4 files changed

Lines changed: 12 additions & 29 deletions

File tree

src/Entity/Item.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,13 @@ public function getId() {
8787
* Can be ItemId since 0.5.
8888
* Can be null since 1.0.
8989
*
90-
* @param ItemId|int|null $id
90+
* @param ItemId|null $id
9191
*
9292
* @throws InvalidArgumentException
9393
*/
9494
public function setId( $id ) {
9595
if ( $id === null || $id instanceof ItemId ) {
9696
$this->id = $id;
97-
} elseif ( is_int( $id ) ) {
98-
$this->id = ItemId::newFromNumber( $id );
9997
} else {
10098
throw new InvalidArgumentException( '$id must be an instance of ItemId, an integer,'
10199
. ' or null' );

src/Entity/Property.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,13 @@ public function getId() {
8585
* Can be PropertyId since 0.5.
8686
* Can be null since 1.0.
8787
*
88-
* @param PropertyId|int|null $id
88+
* @param PropertyId|null $id
8989
*
9090
* @throws InvalidArgumentException
9191
*/
9292
public function setId( $id ) {
9393
if ( $id === null || $id instanceof PropertyId ) {
9494
$this->id = $id;
95-
} elseif ( is_int( $id ) ) {
96-
$this->id = PropertyId::newFromNumber( $id );
9795
} else {
9896
throw new InvalidArgumentException( '$id must be an instance of PropertyId, an integer,'
9997
. ' or null' );

tests/unit/Entity/ItemTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ public function testGetId() {
4848
$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
4949
}
5050

51-
public function testSetIdUsingNumber() {
52-
$item = new Item();
53-
$item->setId( 42 );
54-
$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
55-
}
56-
5751
public function testGetSiteLinkWithNonSetSiteId() {
5852
$item = new Item();
5953

@@ -257,10 +251,10 @@ public function equalsProvider() {
257251
$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
258252

259253
$secondItemWithId = $secondItem->copy();
260-
$secondItemWithId->setId( 42 );
254+
$secondItemWithId->setId( new ItemId( 'Q42' ) );
261255

262256
$differentId = $secondItemWithId->copy();
263-
$differentId->setId( 43 );
257+
$differentId->setId( new ItemId( 'Q43' ) );
264258

265259
return [
266260
[ new Item(), new Item() ],
@@ -529,7 +523,7 @@ public function instanceProvider() {
529523

530524
// ID only
531525
$entity = clone $entity;
532-
$entity->setId( 44 );
526+
$entity->setId( new ItemId( 'Q44' ) );
533527

534528
$entities[] = $entity;
535529

@@ -543,7 +537,7 @@ public function instanceProvider() {
543537

544538
// with labels etc and ID
545539
$entity = clone $entity;
546-
$entity->setId( 42 );
540+
$entity->setId( new ItemId( 'Q42' ) );
547541

548542
$entities[] = $entity;
549543

tests/unit/Entity/PropertyTest.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ public function testSetAndGetDataTypeId() {
7979
}
8080
}
8181

82-
public function testWhenIdSetWithNumber_GetIdReturnsPropertyId() {
83-
$property = Property::newFromType( 'string' );
84-
$property->setId( 42 );
85-
86-
$this->assertHasCorrectIdType( $property );
87-
}
88-
8982
protected function assertHasCorrectIdType( Property $property ) {
9083
$this->assertInstanceOf( PropertyId::class, $property->getId() );
9184
}
@@ -103,7 +96,7 @@ public function testPropertyWithTypeIsEmpty() {
10396

10497
public function testPropertyWithIdIsEmpty() {
10598
$property = Property::newFromType( 'string' );
106-
$property->setId( 1337 );
99+
$property->setId( new PropertyId( 'P1337' ) );
107100
$this->assertTrue( $property->isEmpty() );
108101
}
109102

@@ -144,10 +137,10 @@ public function equalsProvider() {
144137
$secondProperty->setStatements( $this->newNonEmptyStatementList() );
145138

146139
$secondPropertyWithId = $secondProperty->copy();
147-
$secondPropertyWithId->setId( 42 );
140+
$secondPropertyWithId->setId( new PropertyId( 'P42' ) );
148141

149142
$differentId = $secondPropertyWithId->copy();
150-
$differentId->setId( 43 );
143+
$differentId->setId( new PropertyId( 'P43' ) );
151144

152145
return [
153146
[ Property::newFromType( 'string' ), Property::newFromType( 'string' ) ],
@@ -168,7 +161,7 @@ public function testEquals( Property $firstProperty, Property $secondProperty )
168161
private function getBaseProperty() {
169162
$property = Property::newFromType( 'string' );
170163

171-
$property->setId( 42 );
164+
$property->setId( new PropertyId( 'P42' ) );
172165
$property->setLabel( 'en', 'Same' );
173166
$property->setDescription( 'en', 'Same' );
174167
$property->setAliases( 'en', [ 'Same' ] );
@@ -407,7 +400,7 @@ public function instanceProvider() {
407400

408401
// ID only
409402
$entity = clone $entity;
410-
$entity->setId( 44 );
403+
$entity->setId( new PropertyId( 'P44' ) );
411404

412405
$entities[] = $entity;
413406

@@ -421,7 +414,7 @@ public function instanceProvider() {
421414

422415
// with labels etc and ID
423416
$entity = clone $entity;
424-
$entity->setId( 42 );
417+
$entity->setId( new PropertyId( 'P42' ) );
425418

426419
$entities[] = $entity;
427420

0 commit comments

Comments
 (0)