Skip to content

Commit e8d4428

Browse files
author
addwiki-ci
committed
Typing & PHP74 (#55)
* Initial round of typing * RELNOTES for PHP7.4+ and typing * Fix command exit codes (and thus returns) * Switch lowest CI version to be 7.4 * Fix typehints that psalm detected as wrong * Fix tests * composer fix * rector pokes * FQNs and style * Fixes in an around MediaWikiApi * Add missing @var
1 parent b55b922 commit e8d4428

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+176
-368
lines changed

RELEASENOTES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Installable with 7.4+ (including PHP8)
6+
- Typing added throughout
57
- PSR4 namespacing. Now in `Addwiki\Wikibase`
68

79
## Version 2.8 (16 February 2021)

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
]
2424
},
2525
"require": {
26-
"php": ">=7.3",
26+
"php": ">=7.4",
2727
"addwiki/mediawiki-api-base": "^3.0",
2828
"addwiki/mediawiki-datamodel": "^3.0",
2929
"serialization/serialization": "~3.2 || ~4.0",

phpcs.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
<exclude name="MediaWiki.Commenting.FunctionComment.MissingParamComment" />
99
<exclude name="MediaWiki.Commenting.FunctionComment.MissingParamTag" />
1010
<exclude name="MediaWiki.Commenting.FunctionComment.MissingReturn" />
11+
<exclude name="MediaWiki.Commenting.FunctionComment.ParamNameNoMatch" />
1112
<exclude name="MediaWiki.Commenting.MissingCovers.MissingCovers" />
1213
<exclude name="MediaWiki.Commenting.PropertyDocumentation.MissingDocumentationPrivate" />
14+
<exclude name="MediaWiki.Commenting.PropertyDocumentation.MissingDocumentationProtected" />
1315
<exclude name="MediaWiki.Usage.ForbiddenFunctions.is_resource" />
1416
<exclude name="MediaWiki.Usage.ForbiddenFunctions.passthru" />
1517
<exclude name="MediaWiki.Usage.InArrayUsage.Found" />

src/Api/GenericOptions.php

+9-18
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
*/
1818
final class GenericOptions {
1919

20-
/**
21-
* @var array
22-
*/
23-
private $options = [];
20+
private array $options = [];
2421

2522
/**
2623
* @since 0.2
2724
*
2825
* @throws InvalidArgumentException
26+
* @param mixed[] $options
2927
*/
3028
public function __construct( array $options = [] ) {
3129
foreach ( array_keys( $options ) as $option ) {
@@ -42,12 +40,10 @@ public function __construct( array $options = [] ) {
4240
*
4341
* @since 0.2
4442
*
45-
* @param string $option
4643
* @param mixed $value
47-
*
4844
* @throws InvalidArgumentException
4945
*/
50-
public function setOption( $option, $value ) {
46+
public function setOption( string $option, $value ): void {
5147
if ( !is_string( $option ) ) {
5248
throw new InvalidArgumentException( 'Option name needs to be a string' );
5349
}
@@ -61,11 +57,10 @@ public function setOption( $option, $value ) {
6157
*
6258
* @since 0.2
6359
*
64-
* @param string $option
6560
*
6661
* @throws OutOfBoundsException
6762
*/
68-
public function getOption( $option ) {
63+
public function getOption( string $option ) {
6964
if ( !array_key_exists( $option, $this->options ) ) {
7065
throw new OutOfBoundsException( sprintf( "Option '%s' has not been set so cannot be obtained", $option ) );
7166
}
@@ -78,11 +73,9 @@ public function getOption( $option ) {
7873
*
7974
* @since 0.2
8075
*
81-
* @param string $option
8276
*
83-
* @return bool
8477
*/
85-
public function hasOption( $option ) {
78+
public function hasOption( string $option ): bool {
8679
return array_key_exists( $option, $this->options );
8780
}
8881

@@ -91,10 +84,9 @@ public function hasOption( $option ) {
9184
*
9285
* @since 0.2
9386
*
94-
* @param string $option
9587
* @param mixed $default
9688
*/
97-
public function defaultOption( $option, $default ) {
89+
public function defaultOption( string $option, $default ): void {
9890
if ( !$this->hasOption( $option ) ) {
9991
$this->setOption( $option, $default );
10092
}
@@ -106,11 +98,10 @@ public function defaultOption( $option, $default ) {
10698
*
10799
* @since 0.2
108100
*
109-
* @param string $option
110101
*
111102
* @throws RuntimeException
112103
*/
113-
public function requireOption( $option ) {
104+
public function requireOption( string $option ): void {
114105
if ( !$this->hasOption( $option ) ) {
115106
throw new RuntimeException( 'Required option"' . $option . '" is not set' );
116107
}
@@ -121,9 +112,9 @@ public function requireOption( $option ) {
121112
*
122113
* @since 0.2
123114
*
124-
* @return array
115+
* @return mixed[]
125116
*/
126-
public function getOptions() {
117+
public function getOptions(): array {
127118
return $this->options;
128119
}
129120

src/Api/Lookup/EntityApiLookup.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
*/
1414
class EntityApiLookup implements EntityLookup {
1515

16-
/**
17-
* @var RevisionGetter
18-
*/
19-
private $revisionGetter;
16+
private RevisionGetter $revisionGetter;
2017

2118
/**
2219
* @param RevisionGetter $revisionGetter
@@ -27,6 +24,7 @@ public function __construct( RevisionGetter $revisionGetter ) {
2724

2825
/**
2926
* @see EntityLookup::getEntity
27+
* @return null|mixed
3028
*/
3129
public function getEntity( EntityId $entityId ) {
3230
$revision = $this->revisionGetter->getFromId( $entityId );
@@ -40,6 +38,7 @@ public function getEntity( EntityId $entityId ) {
4038

4139
/**
4240
* @see EntityLookup::hasEntity
41+
* @return bool
4342
*/
4443
public function hasEntity( EntityId $entityId ) {
4544
$revision = $this->revisionGetter->getFromId( $entityId );

src/Api/Lookup/EntityRedirectApiLookup.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use BadMethodCallException;
88
use Wikibase\DataModel\Entity\BasicEntityIdParser;
99
use Wikibase\DataModel\Entity\EntityId;
10+
use Wikibase\DataModel\Entity\ItemId;
11+
use Wikibase\DataModel\Entity\PropertyId;
1012
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookup;
1113
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookupException;
1214

@@ -17,13 +19,10 @@
1719
*/
1820
class EntityRedirectApiLookup implements EntityRedirectLookup {
1921

20-
/**
21-
* @var MediawikiApi
22-
*/
23-
private $api;
22+
private MediawikiApi $api;
2423

2524
/**
26-
* @param \Addwiki\Mediawiki\Api\Client\MediawikiApi $api
25+
* @param MediawikiApi $api
2726
*/
2827
public function __construct( MediawikiApi $api ) {
2928
$this->api = $api;
@@ -40,6 +39,7 @@ public function getRedirectIds( EntityId $targetId ) {
4039

4140
/**
4241
* @see EntityRedirectLookup::getRedirectForEntityId
42+
* @return ItemId|PropertyId
4343
*/
4444
public function getRedirectForEntityId( EntityId $entityId, $forUpdate = '' ) {
4545
$entityIdSerialization = $entityId->getSerialization();

src/Api/Lookup/ItemApiLookup.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Addwiki\Wikibase\Api\Lookup;
44

5+
use Wikibase\DataModel\Entity\EntityDocument;
56
use Wikibase\DataModel\Entity\ItemId;
67
use Wikibase\DataModel\Services\Lookup\EntityLookup;
78
use Wikibase\DataModel\Services\Lookup\ItemLookup;
@@ -14,10 +15,7 @@
1415
*/
1516
class ItemApiLookup implements ItemLookup {
1617

17-
/**
18-
* @var EntityLookup
19-
*/
20-
private $entityLookup;
18+
private \Wikibase\DataModel\Services\Lookup\EntityLookup $entityLookup;
2119

2220
/**
2321
* @param EntityLookup $entityLookup
@@ -28,6 +26,7 @@ public function __construct( EntityLookup $entityLookup ) {
2826

2927
/**
3028
* @see ItemLookup::getItemForId
29+
* @return EntityDocument|null
3130
*/
3231
public function getItemForId( ItemId $itemId ) {
3332
return $this->entityLookup->getEntity( $itemId );

src/Api/Lookup/PropertyApiLookup.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Addwiki\Wikibase\Api\Lookup;
44

5+
use Wikibase\DataModel\Entity\EntityDocument;
56
use Wikibase\DataModel\Entity\PropertyId;
67
use Wikibase\DataModel\Services\Lookup\EntityLookup;
78
use Wikibase\DataModel\Services\Lookup\PropertyLookup;
@@ -14,10 +15,7 @@
1415
*/
1516
class PropertyApiLookup implements PropertyLookup {
1617

17-
/**
18-
* @var EntityLookup
19-
*/
20-
private $entityLookup;
18+
private \Wikibase\DataModel\Services\Lookup\EntityLookup $entityLookup;
2119

2220
/**
2321
* @param EntityLookup $entityLookup
@@ -28,6 +26,7 @@ public function __construct( EntityLookup $entityLookup ) {
2826

2927
/**
3028
* @see ItemLookup::getPropertyForId
29+
* @return EntityDocument|null
3130
*/
3231
public function getPropertyForId( PropertyId $propertyId ) {
3332
return $this->entityLookup->getEntity( $propertyId );

src/Api/Service/AliasGroupSetter.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
*/
1919
class AliasGroupSetter {
2020

21-
/**
22-
* @var WikibaseApi
23-
*/
24-
private $api;
21+
private WikibaseApi $api;
2522

2623
/**
2724
* @param WikibaseApi $api
@@ -36,10 +33,8 @@ public function __construct( WikibaseApi $api ) {
3633
* @param AliasGroup $aliasGroup
3734
* @param EntityId|Item|Property|SiteLink $target
3835
* @param EditInfo|null $editInfo
39-
*
40-
* @return bool
4136
*/
42-
public function set( AliasGroup $aliasGroup, $target, EditInfo $editInfo = null ) {
37+
public function set( AliasGroup $aliasGroup, $target, EditInfo $editInfo = null ): bool {
4338
$this->throwExceptionsOnBadTarget( $target );
4439

4540
$params = $this->getTargetParamsFromTarget(
@@ -60,7 +55,7 @@ public function set( AliasGroup $aliasGroup, $target, EditInfo $editInfo = null
6055
*
6156
* @todo Fix duplicated code
6257
*/
63-
private function throwExceptionsOnBadTarget( $target ) {
58+
private function throwExceptionsOnBadTarget( $target ): void {
6459
if ( !$target instanceof EntityId && !$target instanceof Item && !$target instanceof Property && !$target instanceof SiteLink ) {
6560
throw new UnexpectedValueException( '$target needs to be an EntityId, Item, Property or SiteLink' );
6661
}

src/Api/Service/BadgeIdsGetter.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
*/
1414
class BadgeIdsGetter {
1515

16-
/**
17-
* @var MediawikiApi
18-
*/
19-
private $api;
16+
private MediawikiApi $api;
2017

2118
/**
2219
* @param MediawikiApi $api
@@ -29,7 +26,7 @@ public function __construct( MediawikiApi $api ) {
2926
* @since 0.5
3027
* @return ItemId[]
3128
*/
32-
public function get() {
29+
public function get(): array {
3330
$result = $this->api->getRequest( new SimpleRequest( 'wbavailablebadges' ) );
3431
$ids = [];
3532
foreach ( $result['badges'] as $badgeIdString ) {

src/Api/Service/DescriptionSetter.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
*/
1919
class DescriptionSetter {
2020

21-
/**
22-
* @var WikibaseApi
23-
*/
24-
private $api;
21+
private WikibaseApi $api;
2522

2623
/**
2724
* @param WikibaseApi $api
@@ -36,10 +33,8 @@ public function __construct( WikibaseApi $api ) {
3633
* @param Term $description
3734
* @param EntityId|Item|Property|SiteLink $target
3835
* @param EditInfo|null $editInfo
39-
*
40-
* @return bool
4136
*/
42-
public function set( Term $description, $target, EditInfo $editInfo = null ) {
37+
public function set( Term $description, $target, EditInfo $editInfo = null ): bool {
4338
$this->throwExceptionsOnBadTarget( $target );
4439

4540
$params = $this->getTargetParamsFromTarget(
@@ -60,7 +55,7 @@ public function set( Term $description, $target, EditInfo $editInfo = null ) {
6055
*
6156
* @todo Fix duplicated code
6257
*/
63-
private function throwExceptionsOnBadTarget( $target ) {
58+
private function throwExceptionsOnBadTarget( $target ): void {
6459
if ( !$target instanceof EntityId && !$target instanceof Item && !$target instanceof Property && !$target instanceof SiteLink ) {
6560
throw new UnexpectedValueException( '$target needs to be an EntityId, Item, Property or SiteLink' );
6661
}

src/Api/Service/EntityDocumentSaver.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Addwiki\Mediawiki\DataModel\EditInfo;
77
use Addwiki\Mediawiki\DataModel\Revision;
88
use Wikibase\DataModel\Entity\EntityDocument;
9+
use Wikibase\DataModel\Entity\Item;
10+
use Wikibase\DataModel\Entity\Property;
911

1012
/**
1113
* @access private
@@ -14,10 +16,7 @@
1416
*/
1517
class EntityDocumentSaver {
1618

17-
/**
18-
* @var RevisionSaver
19-
*/
20-
private $revisionSaver;
19+
private RevisionSaver $revisionSaver;
2120

2221
public function __construct( RevisionSaver $revisionSaver ) {
2322
$this->revisionSaver = $revisionSaver;
@@ -29,7 +28,7 @@ public function __construct( RevisionSaver $revisionSaver ) {
2928
* @param EntityDocument $entityDocument
3029
* @param EditInfo $editInfo
3130
*
32-
* @return EntityDocument
31+
* @return Item|Property
3332
*/
3433
public function save( EntityDocument $entityDocument, EditInfo $editInfo ) {
3534
return $this->revisionSaver->save( new Revision( new Content( $entityDocument ) ), $editInfo );

src/Api/Service/EntitySearcher.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
*/
1313
class EntitySearcher {
1414

15-
/**
16-
* @var MediawikiApi
17-
*/
18-
private $api;
15+
private MediawikiApi $api;
1916

2017
/**
21-
* @param \Addwiki\Mediawiki\Api\Client\MediawikiApi $api
18+
* @param MediawikiApi $api
2219
*/
2320
public function __construct( MediawikiApi $api ) {
2421
$this->api = $api;
@@ -27,12 +24,9 @@ public function __construct( MediawikiApi $api ) {
2724
/**
2825
* @since 0.8
2926
*
30-
* @param string $entityType
31-
* @param string $string
32-
* @param string $language
3327
* @return string[] EntityIds
3428
*/
35-
public function search( $entityType, $string, $language ) {
29+
public function search( string $entityType, string $string, string $language ): array {
3630
$params = [
3731
'search' => $string,
3832
'language' => $language,

0 commit comments

Comments
 (0)