Skip to content

Commit 445d764

Browse files
jenkins-botGerrit Code Review
authored andcommitted
Merge "Re-enable Phan and fix warnings for `data-model-services'"
2 parents 5ee4370 + f0a0bdd commit 445d764

File tree

11 files changed

+57
-33
lines changed

11 files changed

+57
-33
lines changed

.phan/config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
$cfg['exclude_analysis_directory_list'] = array_merge(
6666
$cfg['exclude_analysis_directory_list'],
6767
[
68-
'lib/packages/wikibase/data-model-services/src/',
6968
'lib/packages/wikibase/internal-serialization/src/',
7069
'../../extensions/Babel/',
7170
'../../extensions/CirrusSearch/',

lib/packages/wikibase/data-model-services/src/DataValue/ValuesFinder.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ public function findFromSnaks( array $snaks, $dataType ) {
4444
$this->isMatchingDataType( $snak->getPropertyId(), $dataType )
4545
) {
4646
$dataValue = $snak->getDataValue();
47-
$found[$dataValue->getHash()] = $dataValue;
47+
if ( method_exists( $dataValue, 'getHash' ) ) {
48+
// We can only tag values as found that are
49+
// hashable. DataValueObject should, but `getDataValue`
50+
// only returns the `DataValue` interface, which doesn't
51+
// define `getHash`
52+
// @phan-suppress-next-line PhanUndeclaredMethod
53+
$found[$dataValue->getHash()] = $dataValue;
54+
}
4855
}
4956
}
5057

lib/packages/wikibase/data-model-services/src/Diff/Internal/SiteLinkListPatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getPatchedSiteLinkList( SiteLinkList $siteLinks, Diff $patch ) {
5959
* @return ItemId[]|null
6060
*/
6161
private function getBadgesFromSiteLinkData( array $siteLinkData ) {
62-
if ( !array_key_exists( 'badges', $siteLinkData ) ) {
62+
if ( !array_key_exists( 'badges', $siteLinkData ) || !is_array( $siteLinkData['badges'] ) ) {
6363
return null;
6464
}
6565

lib/packages/wikibase/data-model-services/src/Diff/ItemDiffer.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ public function canDiffEntityType( $entityType ) {
4949
* @throws InvalidArgumentException
5050
*/
5151
public function diffEntities( EntityDocument $from, EntityDocument $to ) {
52-
$this->assertIsItem( $from );
53-
$this->assertIsItem( $to );
52+
$fromItem = $this->assertIsItemAndCast( $from );
53+
$toItem = $this->assertIsItemAndCast( $to );
5454

55-
return $this->diffItems( $from, $to );
55+
return $this->diffItems( $fromItem, $toItem );
5656
}
5757

58-
private function assertIsItem( EntityDocument $item ) {
58+
private function assertIsItemAndCast( EntityDocument $item ): Item {
5959
if ( !( $item instanceof Item ) ) {
6060
throw new InvalidArgumentException( '$item must be an instance of Item' );
6161
}
62+
return $item;
6263
}
6364

6465
public function diffItems( Item $from, Item $to ) {
@@ -108,8 +109,8 @@ static function( ItemId $id ) {
108109
* @throws InvalidArgumentException
109110
*/
110111
public function getConstructionDiff( EntityDocument $entity ) {
111-
$this->assertIsItem( $entity );
112-
return $this->diffEntities( new Item(), $entity );
112+
$item = $this->assertIsItemAndCast( $entity );
113+
return $this->diffEntities( new Item(), $item );
113114
}
114115

115116
/**
@@ -119,8 +120,8 @@ public function getConstructionDiff( EntityDocument $entity ) {
119120
* @throws InvalidArgumentException
120121
*/
121122
public function getDestructionDiff( EntityDocument $entity ) {
122-
$this->assertIsItem( $entity );
123-
return $this->diffEntities( $entity, new Item() );
123+
$item = $this->assertIsItemAndCast( $entity );
124+
return $this->diffEntities( $item, new Item() );
124125
}
125126

126127
}

lib/packages/wikibase/data-model-services/src/Diff/ItemPatcher.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ public function canPatchEntityType( $entityType ) {
5353
* @throws InvalidArgumentException
5454
*/
5555
public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
56-
$this->assertIsItem( $entity );
56+
$item = $this->assertIsItemAndCast( $entity );
5757

58-
$this->patchItem( $entity, $patch );
58+
$this->patchItem( $item, $patch );
5959
}
6060

61-
private function assertIsItem( EntityDocument $item ) {
61+
private function assertIsItemAndCast( EntityDocument $item ): Item {
6262
if ( !( $item instanceof Item ) ) {
6363
throw new InvalidArgumentException( '$item must be an instance of Item' );
6464
}
65+
return $item;
6566
}
6667

6768
private function patchItem( Item $item, EntityDiff $patch ) {

lib/packages/wikibase/data-model-services/src/Diff/PropertyDiffer.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ public function canDiffEntityType( $entityType ) {
4949
* @throws InvalidArgumentException
5050
*/
5151
public function diffEntities( EntityDocument $from, EntityDocument $to ) {
52-
$this->assertIsProperty( $from );
53-
$this->assertIsProperty( $to );
52+
$fromProperty = $this->assertIsPropertyAndCast( $from );
53+
$toProperty = $this->assertIsPropertyAndCast( $to );
5454

55-
return $this->diffProperties( $from, $to );
55+
return $this->diffProperties( $fromProperty, $toProperty );
5656
}
5757

58-
private function assertIsProperty( EntityDocument $property ) {
58+
private function assertIsPropertyAndCast( EntityDocument $property ): Property {
5959
if ( !( $property instanceof Property ) ) {
6060
throw new InvalidArgumentException( '$property must be an instance of Property' );
6161
}
62+
return $property;
6263
}
6364

6465
public function diffProperties( Property $from, Property $to ) {
@@ -104,11 +105,10 @@ private function toDiffArray( Property $property ) {
104105
* @throws InvalidArgumentException
105106
*/
106107
public function getConstructionDiff( EntityDocument $entity ) {
107-
$this->assertIsProperty( $entity );
108+
$property = $this->assertIsPropertyAndCast( $entity );
108109

109-
/** @var Property $entity */
110-
$diffOps = $this->diffPropertyArrays( [], $this->toDiffArray( $entity ) );
111-
$diffOps['claim'] = $this->statementListDiffer->getDiff( new StatementList(), $entity->getStatements() );
110+
$diffOps = $this->diffPropertyArrays( [], $this->toDiffArray( $property ) );
111+
$diffOps['claim'] = $this->statementListDiffer->getDiff( new StatementList(), $property->getStatements() );
112112

113113
return new EntityDiff( $diffOps );
114114
}
@@ -120,11 +120,10 @@ public function getConstructionDiff( EntityDocument $entity ) {
120120
* @throws InvalidArgumentException
121121
*/
122122
public function getDestructionDiff( EntityDocument $entity ) {
123-
$this->assertIsProperty( $entity );
123+
$property = $this->assertIsPropertyAndCast( $entity );
124124

125-
/** @var Property $entity */
126-
$diffOps = $this->diffPropertyArrays( $this->toDiffArray( $entity ), [] );
127-
$diffOps['claim'] = $this->statementListDiffer->getDiff( $entity->getStatements(), new StatementList() );
125+
$diffOps = $this->diffPropertyArrays( $this->toDiffArray( $property ), [] );
126+
$diffOps['claim'] = $this->statementListDiffer->getDiff( $property->getStatements(), new StatementList() );
128127

129128
return new EntityDiff( $diffOps );
130129
}

lib/packages/wikibase/data-model-services/src/Diff/PropertyPatcher.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ public function canPatchEntityType( $entityType ) {
4646
* @throws InvalidArgumentException
4747
*/
4848
public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
49-
$this->assertIsProperty( $entity );
49+
$property = $this->assertIsPropertyAndCast( $entity );
5050

51-
$this->patchProperty( $entity, $patch );
51+
$this->patchProperty( $property, $patch );
5252
}
5353

54-
private function assertIsProperty( EntityDocument $property ) {
54+
private function assertIsPropertyAndCast( EntityDocument $property ): Property {
5555
if ( !( $property instanceof Property ) ) {
5656
throw new InvalidArgumentException( '$property must be an instance of Property' );
5757
}
58+
return $property;
5859
}
5960

6061
private function patchProperty( Property $property, EntityDiff $patch ) {

lib/packages/wikibase/data-model-services/src/Diff/StatementListDiffer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ private function toDiffArray( StatementList $statementList ): array {
4747
* @var Statement $statement
4848
*/
4949
foreach ( $statementList as $statement ) {
50-
$statementArray[$statement->getGuid()] = $statement;
50+
$guid = $statement->getGuid();
51+
if ( $guid === null ) {
52+
$statementArray[] = $statement;
53+
} else {
54+
$statementArray[$guid] = $statement;
55+
}
5156
}
5257

5358
return $statementArray;

lib/packages/wikibase/data-model-services/src/Lookup/InMemoryEntityLookup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function addException( EntityLookupException $exception ) {
7272
* @param EntityId $entityId
7373
*
7474
* @throws EntityLookupException
75-
* @return EntityDocument
75+
* @return ?EntityDocument
7676
*/
7777
public function getEntity( EntityId $entityId ) {
7878
$this->throwExceptionIfNeeded( $entityId );

lib/packages/wikibase/data-model-services/src/Lookup/ItemLookupException.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Wikibase\DataModel\Services\Lookup;
44

55
use Exception;
6+
use LogicException;
67
use Wikibase\DataModel\Entity\ItemId;
78

89
/**
@@ -30,7 +31,12 @@ public function __construct( ItemId $itemId, $message = null, ?Exception $previo
3031
* @return ItemId
3132
*/
3233
public function getItemId() {
33-
return $this->getEntityId();
34+
$itemId = $this->getEntityId();
35+
if ( !( $itemId instanceof ItemId ) ) {
36+
throw new LogicException( 'expected $itemId to be of type ItemId' );
37+
}
38+
39+
return $itemId;
3440
}
3541

3642
}

0 commit comments

Comments
 (0)