Skip to content

Commit

Permalink
Merge "Re-enable Phan and fix warnings for `data-model-services'"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Mar 3, 2025
2 parents 5ee4370 + f0a0bdd commit 445d764
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 33 deletions.
1 change: 0 additions & 1 deletion .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
$cfg['exclude_analysis_directory_list'] = array_merge(
$cfg['exclude_analysis_directory_list'],
[
'lib/packages/wikibase/data-model-services/src/',
'lib/packages/wikibase/internal-serialization/src/',
'../../extensions/Babel/',
'../../extensions/CirrusSearch/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ public function findFromSnaks( array $snaks, $dataType ) {
$this->isMatchingDataType( $snak->getPropertyId(), $dataType )
) {
$dataValue = $snak->getDataValue();
$found[$dataValue->getHash()] = $dataValue;
if ( method_exists( $dataValue, 'getHash' ) ) {
// We can only tag values as found that are
// hashable. DataValueObject should, but `getDataValue`
// only returns the `DataValue` interface, which doesn't
// define `getHash`
// @phan-suppress-next-line PhanUndeclaredMethod
$found[$dataValue->getHash()] = $dataValue;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getPatchedSiteLinkList( SiteLinkList $siteLinks, Diff $patch ) {
* @return ItemId[]|null
*/
private function getBadgesFromSiteLinkData( array $siteLinkData ) {
if ( !array_key_exists( 'badges', $siteLinkData ) ) {
if ( !array_key_exists( 'badges', $siteLinkData ) || !is_array( $siteLinkData['badges'] ) ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ public function canDiffEntityType( $entityType ) {
* @throws InvalidArgumentException
*/
public function diffEntities( EntityDocument $from, EntityDocument $to ) {
$this->assertIsItem( $from );
$this->assertIsItem( $to );
$fromItem = $this->assertIsItemAndCast( $from );
$toItem = $this->assertIsItemAndCast( $to );

return $this->diffItems( $from, $to );
return $this->diffItems( $fromItem, $toItem );
}

private function assertIsItem( EntityDocument $item ) {
private function assertIsItemAndCast( EntityDocument $item ): Item {
if ( !( $item instanceof Item ) ) {
throw new InvalidArgumentException( '$item must be an instance of Item' );
}
return $item;
}

public function diffItems( Item $from, Item $to ) {
Expand Down Expand Up @@ -108,8 +109,8 @@ static function( ItemId $id ) {
* @throws InvalidArgumentException
*/
public function getConstructionDiff( EntityDocument $entity ) {
$this->assertIsItem( $entity );
return $this->diffEntities( new Item(), $entity );
$item = $this->assertIsItemAndCast( $entity );
return $this->diffEntities( new Item(), $item );
}

/**
Expand All @@ -119,8 +120,8 @@ public function getConstructionDiff( EntityDocument $entity ) {
* @throws InvalidArgumentException
*/
public function getDestructionDiff( EntityDocument $entity ) {
$this->assertIsItem( $entity );
return $this->diffEntities( $entity, new Item() );
$item = $this->assertIsItemAndCast( $entity );
return $this->diffEntities( $item, new Item() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ public function canPatchEntityType( $entityType ) {
* @throws InvalidArgumentException
*/
public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
$this->assertIsItem( $entity );
$item = $this->assertIsItemAndCast( $entity );

$this->patchItem( $entity, $patch );
$this->patchItem( $item, $patch );
}

private function assertIsItem( EntityDocument $item ) {
private function assertIsItemAndCast( EntityDocument $item ): Item {
if ( !( $item instanceof Item ) ) {
throw new InvalidArgumentException( '$item must be an instance of Item' );
}
return $item;
}

private function patchItem( Item $item, EntityDiff $patch ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ public function canDiffEntityType( $entityType ) {
* @throws InvalidArgumentException
*/
public function diffEntities( EntityDocument $from, EntityDocument $to ) {
$this->assertIsProperty( $from );
$this->assertIsProperty( $to );
$fromProperty = $this->assertIsPropertyAndCast( $from );
$toProperty = $this->assertIsPropertyAndCast( $to );

return $this->diffProperties( $from, $to );
return $this->diffProperties( $fromProperty, $toProperty );
}

private function assertIsProperty( EntityDocument $property ) {
private function assertIsPropertyAndCast( EntityDocument $property ): Property {
if ( !( $property instanceof Property ) ) {
throw new InvalidArgumentException( '$property must be an instance of Property' );
}
return $property;
}

public function diffProperties( Property $from, Property $to ) {
Expand Down Expand Up @@ -104,11 +105,10 @@ private function toDiffArray( Property $property ) {
* @throws InvalidArgumentException
*/
public function getConstructionDiff( EntityDocument $entity ) {
$this->assertIsProperty( $entity );
$property = $this->assertIsPropertyAndCast( $entity );

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

return new EntityDiff( $diffOps );
}
Expand All @@ -120,11 +120,10 @@ public function getConstructionDiff( EntityDocument $entity ) {
* @throws InvalidArgumentException
*/
public function getDestructionDiff( EntityDocument $entity ) {
$this->assertIsProperty( $entity );
$property = $this->assertIsPropertyAndCast( $entity );

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

return new EntityDiff( $diffOps );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ public function canPatchEntityType( $entityType ) {
* @throws InvalidArgumentException
*/
public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
$this->assertIsProperty( $entity );
$property = $this->assertIsPropertyAndCast( $entity );

$this->patchProperty( $entity, $patch );
$this->patchProperty( $property, $patch );
}

private function assertIsProperty( EntityDocument $property ) {
private function assertIsPropertyAndCast( EntityDocument $property ): Property {
if ( !( $property instanceof Property ) ) {
throw new InvalidArgumentException( '$property must be an instance of Property' );
}
return $property;
}

private function patchProperty( Property $property, EntityDiff $patch ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ private function toDiffArray( StatementList $statementList ): array {
* @var Statement $statement
*/
foreach ( $statementList as $statement ) {
$statementArray[$statement->getGuid()] = $statement;
$guid = $statement->getGuid();
if ( $guid === null ) {
$statementArray[] = $statement;
} else {
$statementArray[$guid] = $statement;
}
}

return $statementArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function addException( EntityLookupException $exception ) {
* @param EntityId $entityId
*
* @throws EntityLookupException
* @return EntityDocument
* @return ?EntityDocument
*/
public function getEntity( EntityId $entityId ) {
$this->throwExceptionIfNeeded( $entityId );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wikibase\DataModel\Services\Lookup;

use Exception;
use LogicException;
use Wikibase\DataModel\Entity\ItemId;

/**
Expand Down Expand Up @@ -30,7 +31,12 @@ public function __construct( ItemId $itemId, $message = null, ?Exception $previo
* @return ItemId
*/
public function getItemId() {
return $this->getEntityId();
$itemId = $this->getEntityId();
if ( !( $itemId instanceof ItemId ) ) {
throw new LogicException( 'expected $itemId to be of type ItemId' );
}

return $itemId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wikibase\DataModel\Services\Lookup;

use Exception;
use LogicException;
use Wikibase\DataModel\Entity\PropertyId;

/**
Expand Down Expand Up @@ -30,7 +31,11 @@ public function __construct( PropertyId $propertyId, $message = null, ?Exception
* @return PropertyId
*/
public function getPropertyId() {
return $this->getEntityId();
$propertyId = $this->getEntityId();
if ( !( $propertyId instanceof PropertyId ) ) {
throw new LogicException( 'expected $propertyId to be of type PropertyId' );
}
return $propertyId;
}

}

0 comments on commit 445d764

Please sign in to comment.