Skip to content

Commit 2b9dbcc

Browse files
outdooracornWMDE bot
authored andcommitted
DM: Add type hints and strict_types to StatementGuid
Change-Id: Ide6e0f5d1cfdf84b10a1dc930ea482ca16cb5d53
1 parent f3f36d7 commit 2b9dbcc

2 files changed

Lines changed: 20 additions & 68 deletions

File tree

src/Statement/StatementGuid.php

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
<?php
1+
<?php declare( strict_types=1 );
22

33
namespace Wikibase\DataModel\Statement;
44

5-
use InvalidArgumentException;
65
use Wikibase\DataModel\Entity\EntityId;
76

87
/**
@@ -22,79 +21,49 @@ class StatementGuid {
2221
*/
2322
public const SEPARATOR = '$';
2423

25-
/**
26-
* @var EntityId
27-
*/
28-
private $entityId;
29-
30-
/**
31-
* @var string
32-
*/
33-
private $guidPart;
34-
35-
/**
36-
* @var string
37-
*/
38-
private $serialization;
39-
40-
/**
41-
* @param EntityId $entityId
42-
* @param string $guid
43-
*
44-
* @throws InvalidArgumentException
45-
*/
46-
public function __construct( EntityId $entityId, $guid ) {
47-
if ( !is_string( $guid ) ) {
48-
throw new InvalidArgumentException( '$guid must be a string' );
49-
}
24+
private EntityId $entityId;
25+
private string $guidPart;
26+
private string $serialization;
5027

28+
public function __construct( EntityId $entityId, string $guid ) {
5129
$this->serialization = $entityId->getSerialization() . self::SEPARATOR . $guid;
5230
$this->entityId = $entityId;
5331
$this->guidPart = $guid;
5432
}
5533

56-
/**
57-
* @return EntityId
58-
*/
59-
public function getEntityId() {
34+
public function getEntityId(): EntityId {
6035
return $this->entityId;
6136
}
6237

6338
/**
6439
* @since 9.4
65-
*
66-
* @return string
6740
*/
68-
public function getGuidPart() {
41+
public function getGuidPart(): string {
6942
return $this->guidPart;
7043
}
7144

7245
/**
73-
* @return string
7446
* @deprecated The value returned by this method might differ in case from the original, unparsed statement GUID
7547
* (the entity ID part might have been lowercase originally, but is always normalized in the return value here),
7648
* which means that the value should not be compared to other statement GUID serializations,
7749
* e.g. to look up a statement in a StatementList.
7850
*/
79-
public function getSerialization() {
51+
public function getSerialization(): string {
8052
return $this->serialization;
8153
}
8254

8355
/**
8456
* @param mixed $target
85-
*
86-
* @return bool
8757
*/
88-
public function equals( $target ) {
58+
public function equals( $target ): bool {
8959
if ( $this === $target ) {
9060
return true;
9161
}
9262

93-
return $target instanceof self
94-
&& $target->serialization === $this->serialization;
63+
return $target instanceof self && $target->serialization === $this->serialization;
9564
}
9665

97-
public function __toString() {
66+
public function __toString(): string {
9867
return $this->serialization;
9968
}
10069

tests/unit/Statement/StatementGuidTest.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?php
1+
<?php declare( strict_types=1 );
22

33
namespace Wikibase\DataModel\Tests\Statement;
44

5-
use InvalidArgumentException;
5+
use PHPUnit\Framework\TestCase;
66
use Wikibase\DataModel\Entity\EntityId;
77
use Wikibase\DataModel\Entity\ItemId;
88
use Wikibase\DataModel\Statement\StatementGuid;
@@ -16,24 +16,24 @@
1616
* @license GPL-2.0-or-later
1717
* @author Addshore
1818
*/
19-
class StatementGuidTest extends \PHPUnit\Framework\TestCase {
19+
class StatementGuidTest extends TestCase {
2020

2121
/**
2222
* @dataProvider provideConstructionData
2323
*/
24-
public function testConstructor( EntityId $entityId, $guid, $expected ) {
24+
public function testConstructor( EntityId $entityId, string $guid, string $expected ): void {
2525
$statementGuid = new StatementGuid( $entityId, $guid );
2626

2727
$this->assertSame( $expected, $statementGuid->getSerialization() );
2828
$this->assertEquals( $entityId, $statementGuid->getEntityId() );
2929
$this->assertSame( $guid, $statementGuid->getGuidPart() );
3030
}
3131

32-
public static function provideConstructionData() {
32+
public static function provideConstructionData(): array {
3333
return [
3434
[
3535
new ItemId( 'q42' ),
36-
'D8404CDA-25E4-4334-AF13-A3290BCD9C0N' ,
36+
'D8404CDA-25E4-4334-AF13-A3290BCD9C0N',
3737
'Q42$D8404CDA-25E4-4334-AF13-A3290BCD9C0N',
3838
],
3939
[
@@ -49,24 +49,7 @@ public static function provideConstructionData() {
4949
];
5050
}
5151

52-
/**
53-
* @dataProvider provideBadConstruction
54-
*/
55-
public function testBadConstruction( EntityId $entityId, $guid ) {
56-
$this->expectException( InvalidArgumentException::class );
57-
new StatementGuid( $entityId, $guid );
58-
}
59-
60-
public static function provideBadConstruction() {
61-
$id = new ItemId( 'Q1' );
62-
63-
return [
64-
[ $id, null ],
65-
[ $id, 12345 ],
66-
];
67-
}
68-
69-
public function provideStatementGuids() {
52+
public function provideStatementGuids(): array {
7053
$argLists = [];
7154

7255
foreach ( $this->provideConstructionData() as $data ) {
@@ -79,7 +62,7 @@ public function provideStatementGuids() {
7962
/**
8063
* @dataProvider provideStatementGuids
8164
*/
82-
public function testEquals( StatementGuid $statementGuid ) {
65+
public function testEquals( StatementGuid $statementGuid ): void {
8366
$statementGuidCopy = clone $statementGuid;
8467
$this->assertTrue( $statementGuid->equals( $statementGuidCopy ) );
8568
$this->assertTrue( $statementGuidCopy->equals( $statementGuid ) );
@@ -88,7 +71,7 @@ public function testEquals( StatementGuid $statementGuid ) {
8871
/**
8972
* @dataProvider provideStatementGuids
9073
*/
91-
public function testNotEquals( StatementGuid $statementGuid ) {
74+
public function testNotEquals( StatementGuid $statementGuid ): void {
9275
$notEqualStatementGuid = new StatementGuid( new ItemId( 'q9999' ), 'someguid' );
9376
$this->assertFalse( $statementGuid->equals( $notEqualStatementGuid ) );
9477
$this->assertFalse( $notEqualStatementGuid->equals( $statementGuid ) );

0 commit comments

Comments
 (0)