Skip to content

Commit ed97ff9

Browse files
lucaswerkmeisterWMDE bot
authored andcommitted
Use strict types for entity ID parsers
Requires a few tweaks to ensure the tests still pass; notably, in WikibaseRepo.datatypes.php, we should check that the given ID is a string *before* passing it into the parser, rather than after, to avoid a repeat of T334719 (see repo DataValueDeserializerTest). Bug: T423185 Change-Id: Ibe946ce2002dca32307515ade93169bc74fc6783 Depends-On: I496fbe16c33b496b678d483614c19816125e543d Depends-On: Ibb5acb562b94144565dee5c373d5e6bf9095db6d
1 parent d5b296c commit ed97ff9

8 files changed

Lines changed: 27 additions & 53 deletions

src/Entity/BasicEntityIdParser.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Entity;
46

57
/**
@@ -19,12 +21,9 @@ public function __construct() {
1921
}
2022

2123
/**
22-
* @param string $idSerialization
23-
*
24-
* @return EntityId
2524
* @throws EntityIdParsingException
2625
*/
27-
public function parse( $idSerialization ) {
26+
public function parse( string $idSerialization ): EntityId {
2827
return $this->idParser->parse( $idSerialization );
2928
}
3029

@@ -41,7 +40,7 @@ public function parse( $idSerialization ) {
4140
*
4241
* @return callable[]
4342
*/
44-
public static function getBuilders() {
43+
public static function getBuilders(): array {
4544
return [
4645
ItemId::PATTERN => static function( $serialization ) {
4746
return new ItemId( $serialization );

src/Entity/DispatchingEntityIdParser.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Entity;
46

57
use InvalidArgumentException;
@@ -15,7 +17,7 @@ class DispatchingEntityIdParser implements EntityIdParser {
1517
/**
1618
* @var callable[]
1719
*/
18-
private $idBuilders;
20+
private array $idBuilders;
1921

2022
/**
2123
* Takes an array in which each key is a preg_match pattern.
@@ -31,14 +33,9 @@ public function __construct( array $idBuilders ) {
3133
}
3234

3335
/**
34-
* @param string $idSerialization
35-
*
3636
* @throws EntityIdParsingException
37-
* @return EntityId
3837
*/
39-
public function parse( $idSerialization ) {
40-
$this->assertIdIsString( $idSerialization );
41-
38+
public function parse( string $idSerialization ): EntityId {
4239
if ( $this->idBuilders === [] ) {
4340
throw new EntityIdParsingException( 'No id builders are configured' );
4441
}
@@ -55,28 +52,9 @@ public function parse( $idSerialization ) {
5552
}
5653

5754
/**
58-
* @param string $idSerialization
59-
*
60-
* @throws EntityIdParsingException
61-
*/
62-
private function assertIdIsString( $idSerialization ) {
63-
if ( !is_string( $idSerialization ) ) {
64-
throw new EntityIdParsingException(
65-
'$idSerialization must be a string, got ' . ( is_object( $idSerialization )
66-
? get_class( $idSerialization )
67-
: getType( $idSerialization ) )
68-
);
69-
}
70-
}
71-
72-
/**
73-
* @param callable $idBuilder
74-
* @param string $idSerialization
75-
*
7655
* @throws EntityIdParsingException
77-
* @return EntityId
7856
*/
79-
private function buildId( $idBuilder, $idSerialization ) {
57+
private function buildId( callable $idBuilder, string $idSerialization ): EntityId {
8058
try {
8159
return $idBuilder( $idSerialization );
8260
} catch ( InvalidArgumentException $ex ) {

src/Entity/EntityIdParser.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Entity;
46

57
/**
@@ -13,11 +15,8 @@
1315
interface EntityIdParser {
1416

1517
/**
16-
* @param string $idSerialization
17-
*
18-
* @return EntityId
1918
* @throws EntityIdParsingException
2019
*/
21-
public function parse( $idSerialization );
20+
public function parse( string $idSerialization ): EntityId;
2221

2322
}

src/Entity/EntityIdParsingException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Entity;
46

57
use RuntimeException;

src/Entity/ItemIdParser.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Entity;
46

57
use InvalidArgumentException;
@@ -17,12 +19,9 @@
1719
class ItemIdParser implements EntityIdParser {
1820

1921
/**
20-
* @param string $idSerialization
21-
*
2222
* @throws EntityIdParsingException
23-
* @return ItemId
2423
*/
25-
public function parse( $idSerialization ) {
24+
public function parse( string $idSerialization ): ItemId {
2625
try {
2726
return new ItemId( $idSerialization );
2827
} catch ( InvalidArgumentException $ex ) {

tests/unit/Entity/BasicEntityIdParserTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Tests\Entity;
46

57
use Wikibase\DataModel\Entity\BasicEntityIdParser;
@@ -39,7 +41,7 @@ public static function entityIdProvider() {
3941
/**
4042
* @dataProvider invalidIdSerializationProvider
4143
*/
42-
public function testCannotParseInvalidId( $invalidIdSerialization ) {
44+
public function testCannotParseInvalidId( string $invalidIdSerialization ) {
4345
$parser = new BasicEntityIdParser();
4446

4547
$this->expectException( EntityIdParsingException::class );
@@ -49,9 +51,6 @@ public function testCannotParseInvalidId( $invalidIdSerialization ) {
4951
public static function invalidIdSerializationProvider() {
5052
return [
5153
[ 'FOO' ],
52-
[ null ],
53-
[ 42 ],
54-
[ [] ],
5554
[ '' ],
5655
[ 'q0' ],
5756
[ '1p' ],

tests/unit/Entity/DispatchingEntityIdParserTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Tests\Entity;
46

57
use Wikibase\DataModel\Entity\BasicEntityIdParser;
@@ -25,7 +27,7 @@ private function getBasicParser() {
2527
/**
2628
* @dataProvider entityIdProvider
2729
*/
28-
public function testCanParseEntityId( $idString, EntityId $expected ) {
30+
public function testCanParseEntityId( string $idString, EntityId $expected ) {
2931
$parser = $this->getBasicParser();
3032
$actual = $parser->parse( $idString );
3133

@@ -44,7 +46,7 @@ public static function entityIdProvider() {
4446
/**
4547
* @dataProvider invalidIdSerializationProvider
4648
*/
47-
public function testCannotParseInvalidId( $invalidIdSerialization ) {
49+
public function testCannotParseInvalidId( string $invalidIdSerialization ) {
4850
$parser = $this->getBasicParser();
4951

5052
$this->expectException( EntityIdParsingException::class );
@@ -54,9 +56,6 @@ public function testCannotParseInvalidId( $invalidIdSerialization ) {
5456
public static function invalidIdSerializationProvider() {
5557
return [
5658
[ 'FOO' ],
57-
[ null ],
58-
[ 42 ],
59-
[ [] ],
6059
[ '' ],
6160
[ 'q0' ],
6261
[ '1p' ],

tests/unit/Entity/ItemIdParserTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace Wikibase\DataModel\Tests\Entity;
46

57
use Wikibase\DataModel\Entity\EntityIdParsingException;
@@ -34,7 +36,7 @@ public static function entityIdProvider() {
3436
/**
3537
* @dataProvider invalidIdSerializationProvider
3638
*/
37-
public function testCannotParseInvalidId( $invalidIdSerialization ) {
39+
public function testCannotParseInvalidId( string $invalidIdSerialization ) {
3840
$parser = new ItemIdParser();
3941

4042
$this->expectException( EntityIdParsingException::class );
@@ -44,9 +46,6 @@ public function testCannotParseInvalidId( $invalidIdSerialization ) {
4446
public static function invalidIdSerializationProvider() {
4547
return [
4648
[ 'FOO' ],
47-
[ null ],
48-
[ 42 ],
49-
[ [] ],
5049
[ '' ],
5150
[ 'q0' ],
5251
[ '1p' ],

0 commit comments

Comments
 (0)