Skip to content

Commit

Permalink
DM: Add type hints to Statement
Browse files Browse the repository at this point in the history
* Investigation into adding type hints to the Wikibase DataModel by first type hinting a part of the DataModel.
* Remove support for calling `Statement::addNewReference()` with a single array argument, which was deprecated in `Version 9.6.0 (2021-03-31)`. This should now be called with a variadic argument list.

Bug: T314769
Change-Id: I179ffb3c421b5c01d59a2bd9d166f679e0441619
  • Loading branch information
outdooracorn authored and WMDE bot committed Mar 6, 2023
1 parent ff2f4a0 commit 924a00d
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Deserializers/LegacyStatementDeserializer.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
<?php declare( strict_types=1 );

namespace Wikibase\InternalSerialization\Deserializers;

use Deserializers\Deserializer;
use Deserializers\DispatchableDeserializer;
use Deserializers\Exceptions\DeserializationException;
use Deserializers\Exceptions\MissingAttributeException;
use InvalidArgumentException;
use TypeError;
use Wikibase\DataModel\Reference;
use Wikibase\DataModel\ReferenceList;
use Wikibase\DataModel\Statement\Statement;
Expand Down Expand Up @@ -61,17 +61,19 @@ private function assertHasKey( array $serialization, $key, $message ) {
}

private function newStatement( array $serialization ) {
$statement = new Statement(
$this->snakDeserializer->deserialize( $serialization['m'] ),
$this->snakListDeserializer->deserialize( $serialization['q'] ),
$this->getReferences( $serialization['refs'] )
);
/* @var Snak $snak */
$snak = $this->snakDeserializer->deserialize( $serialization['m'] );
/* @var SnakList $snakList */
$snakList = $this->snakListDeserializer->deserialize( $serialization['q'] );
$statement = new Statement( $snak, $snakList, $this->getReferences( $serialization['refs'] ) );

try {
$statement->setRank( $serialization['rank'] );
$statement->setGuid( $serialization['g'] );
} catch ( InvalidArgumentException $ex ) {
throw new DeserializationException( $ex->getMessage(), $ex );
} catch ( TypeError $ex ) {
// DeserializationException only accepts Exception instead of Throwable, like it's parent
// TODO: uncomment $ex when DeserializationException accepts Throwable
throw new DeserializationException( $ex->getMessage() /*, $ex*/ );
}

return $statement;
Expand All @@ -98,8 +100,8 @@ private function getReferences( array $refs ) {
*/
public function isDeserializerFor( $serialization ) {
return is_array( $serialization )
// This element is called 'mainsnak' in the current serialization.
&& array_key_exists( 'm', $serialization );
// This element is called 'mainsnak' in the current serialization.
&& array_key_exists( 'm', $serialization );
}

}

0 comments on commit 924a00d

Please sign in to comment.