Jeroen De Dauw has submitted this change and it was merged. Change subject: Replace some assert() with actual code ......................................................................
Replace some assert() with actual code This is related to the planned HHVM switch. assert() depends on a setting. It may do nothing but should in these cases. Change-Id: Ic5548eba2e0b673bd5c937bfc8c7c82915de014c --- M lib/includes/ChangesTable.php M lib/includes/EntityRetrievingDataTypeLookup.php M lib/includes/serializers/ClaimSerializer.php M lib/includes/store/ChunkCache.php M lib/includes/store/sql/PropertyInfoTable.php M lib/includes/store/sql/SiteLinkTable.php 6 files changed, 43 insertions(+), 27 deletions(-) Approvals: WikidataJenkins: Verified Jeroen De Dauw: Looks good to me, approved jenkins-bot: Checked diff --git a/lib/includes/ChangesTable.php b/lib/includes/ChangesTable.php index a4a8c2f..0f63910 100644 --- a/lib/includes/ChangesTable.php +++ b/lib/includes/ChangesTable.php @@ -1,6 +1,7 @@ <?php namespace Wikibase; + use IORMRow; use MWException; @@ -54,7 +55,7 @@ * @return string */ public function getRowClass() { - return '\Wikibase\ChangeRow'; + return 'Wikibase\ChangeRow'; } /** @@ -117,11 +118,10 @@ * * @param ChangeRow|IORMRow $row * + * @throws MWException * @return array */ protected function getWriteValues( IORMRow $row ) { - assert( $row instanceof ChangeRow ); - $values = parent::getWriteValues( $row ); $infoField = $this->getPrefixedField( 'info' ); @@ -129,6 +129,10 @@ $userIdField = $this->getPrefixedField( 'user_id' ); if ( isset( $values[$infoField] ) ) { + if ( !( $row instanceof ChangeRow ) ) { + throw new MWException( '$row must be a ChangeRow.' ); + } + $values[$infoField] = $row->serializeInfo( $values[$infoField] ); } @@ -181,4 +185,5 @@ /* @var Change $rec */ return $rec->getId(); } + } diff --git a/lib/includes/EntityRetrievingDataTypeLookup.php b/lib/includes/EntityRetrievingDataTypeLookup.php index 7cd9d55..6413682 100644 --- a/lib/includes/EntityRetrievingDataTypeLookup.php +++ b/lib/includes/EntityRetrievingDataTypeLookup.php @@ -44,11 +44,10 @@ private function getProperty( PropertyId $propertyId ) { $property = $this->entityLookup->getEntity( $propertyId ); - if ( $property === null ) { + if ( !( $property instanceof Property ) ) { throw new PropertyNotFoundException( $propertyId ); } - assert( $property instanceof Property ); return $property; } diff --git a/lib/includes/serializers/ClaimSerializer.php b/lib/includes/serializers/ClaimSerializer.php index c88c9c9..822c0e5 100644 --- a/lib/includes/serializers/ClaimSerializer.php +++ b/lib/includes/serializers/ClaimSerializer.php @@ -115,10 +115,10 @@ $serialization['qualifiers'] = $qualifiers; $serialization['qualifiers-order'] = array(); + /** @var Snak $snak */ foreach( $claim->getQualifiers() as $snak ) { - /** @var Snak $snak $id */ $id = $snak->getPropertyId()->getPrefixedId(); - if( !in_array( $id, $serialization['qualifiers-order'] ) ) { + if ( !in_array( $id, $serialization['qualifiers-order'] ) ) { $serialization['qualifiers-order'][] = $snak->getPropertyId()->getPrefixedId(); } } @@ -170,7 +170,7 @@ * @return ByPropertyListSerializer|ListSerializer */ private function getListSerializer() { - if( in_array( 'qualifiers', $this->options->getOption( SerializationOptions::OPT_GROUP_BY_PROPERTIES ) ) ){ + if ( in_array( 'qualifiers', $this->options->getOption( SerializationOptions::OPT_GROUP_BY_PROPERTIES ) ) ) { return new ByPropertyListSerializer( 'qualifiers', $this->snakSerializer, @@ -227,7 +227,7 @@ $claim = new Claim( $mainSnak ); } - if( array_key_exists( 'id', $serialization ) ){ + if ( array_key_exists( 'id', $serialization ) ) { $claim->setGuid( $serialization['id'] ); } @@ -238,9 +238,6 @@ throw new InvalidArgumentException( 'Invalid statement rank provided' ); } - /** - * @var Statement $claim - */ $claim->setRank( self::unserializeRank( $serialization['rank'] ) ); if ( array_key_exists( 'references', $serialization ) ) { @@ -272,17 +269,15 @@ protected function unserializeQualifiers( $serialization, $snakUnserializer ) { if ( !array_key_exists( 'qualifiers', $serialization ) ) { return new SnakList(); - } else { - - if( $this->isAssociative( $serialization['qualifiers'] ) ){ + if ( $this->isAssociative( $serialization['qualifiers'] ) ) { $unserializer = new ByPropertyListUnserializer( $snakUnserializer ); } else { $unserializer = new ListUnserializer( $snakUnserializer ); } $snakList = new SnakList( $unserializer->newFromSerialization( $serialization['qualifiers'] ) ); - if( array_key_exists( 'qualifiers-order', $serialization ) ) { + if ( array_key_exists( 'qualifiers-order', $serialization ) ) { $snakList->orderByProperty( $serialization['qualifiers-order'] ); } diff --git a/lib/includes/store/ChunkCache.php b/lib/includes/store/ChunkCache.php index 47e179b..178c8cc 100644 --- a/lib/includes/store/ChunkCache.php +++ b/lib/includes/store/ChunkCache.php @@ -93,8 +93,6 @@ * @return int the position if found, or the negative insert position minus one, if not. */ public function findEntryPosition( $key ) { - assert( '$key >= 0' ); - if ( empty( $this->entries ) ) { return -1; } @@ -212,6 +210,7 @@ * @param int $size the maximum size of the chunk to load * @param int $before insert into the internal entry list before this position. * + * @throws MWException * @return array|bool the cache entry created by inserting the new chunk, or false if * there is no more data to load from the source at the given position. * The cache entry is an associative array containing the following keys: @@ -221,9 +220,11 @@ * - touched: (logical) timestamp of the entry's creation (taken from $this->modCount) */ private function insertChunk( $start, $size, $before ) { - assert( '$start >= 0' ); - assert( '$size >= 0' ); - assert( '$before >= 0' ); + if ( !is_int( $start ) || !is_int( $size ) || !is_int( $before ) + || $start < 0 || $size < 0 || $before < 0 + ) { + throw new MWException( '$start, $size and $before must be non-negative integers.' ); + } $data = $this->source->loadChunk( $start, $size ); diff --git a/lib/includes/store/sql/PropertyInfoTable.php b/lib/includes/store/sql/PropertyInfoTable.php index 2e2fc6f..fe18463 100644 --- a/lib/includes/store/sql/PropertyInfoTable.php +++ b/lib/includes/store/sql/PropertyInfoTable.php @@ -31,10 +31,16 @@ * @param bool $isReadonly Whether the table can be modified. * @param string|bool $wiki The wiki's database to connect to. * Must be a value LBFactory understands. Defaults to false, which is the local wiki. + * + * @throws InvalidArgumentException */ public function __construct( $isReadonly, $wiki = false ) { - assert( is_bool( $isReadonly ) ); - assert( is_string( $wiki ) || $wiki === false ); + if ( !is_bool( $isReadonly ) ) { + throw new InvalidArgumentException( '$isReadonly must be boolean.' ); + } + if ( !is_string( $wiki ) && $wiki !== false ) { + throw new InvalidArgumentException( '$wiki must be a string or false.' ); + } $this->tableName = 'wb_property_info'; $this->isReadonly = $isReadonly; diff --git a/lib/includes/store/sql/SiteLinkTable.php b/lib/includes/store/sql/SiteLinkTable.php index 8a1a9d7..f2aaa45 100644 --- a/lib/includes/store/sql/SiteLinkTable.php +++ b/lib/includes/store/sql/SiteLinkTable.php @@ -41,13 +41,23 @@ * @param bool $readonly Whether the table can be modified. * @param string|bool $wiki The wiki's database to connect to. * Must be a value LBFactory understands. Defaults to false, which is the local wiki. + * + * @throws MWException */ public function __construct( $table, $readonly, $wiki = false ) { + if ( !is_string( $table ) ) { + throw new MWException( '$table must be a string.' ); + } + if ( !is_bool( $readonly ) ) { + throw new MWException( '$readonly must be boolean.' ); + } + if ( !is_string( $wiki ) && $wiki !== false ) { + throw new MWException( '$wiki must be a string or false.' ); + } + $this->table = $table; $this->readonly = $readonly; $this->wiki = $wiki; - - assert( is_bool( $this->readonly ) ); } /** @@ -281,11 +291,11 @@ * @since 0.1 * * @param Item $item - * @param \DatabaseBase|null $db + * @param DatabaseBase|null $db * * @return array of array */ - public function getConflictsForItem( Item $item, \DatabaseBase $db = null ) { + public function getConflictsForItem( Item $item, DatabaseBase $db = null ) { wfProfileIn( __METHOD__ ); $links = $item->getSiteLinks(); -- To view, visit https://gerrit.wikimedia.org/r/155246 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic5548eba2e0b673bd5c937bfc8c7c82915de014c Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits