Merge "Add typehints to several methods"

This commit is contained in:
jenkins-bot 2021-08-31 16:50:57 +00:00 committed by Gerrit Code Review
commit e2bcd5b878
22 changed files with 80 additions and 79 deletions

View file

@ -380,8 +380,9 @@ class MutableRevisionRecord extends RevisionRecord {
*
* @return MutableRevisionSlots
*/
public function getSlots() {
public function getSlots(): RevisionSlots {
// Overwritten just guarantee the more narrow return type.
// TODO Update return typehint once full return type covariance is allowed (PHP 7.4+, T278139)
return parent::getSlots();
}

View file

@ -115,7 +115,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
* @return bool True if this RevisionRecord is known to have same content as $rec.
* False if the content is different (or not known to be the same).
*/
public function hasSameContent( RevisionRecord $rec ) {
public function hasSameContent( RevisionRecord $rec ): bool {
if ( $rec === $this ) {
return true;
}
@ -153,7 +153,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
*
* @return Content|null The content of the given slot, or null if access is forbidden.
*/
public function getContent( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ) {
public function getContent( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ): ?Content {
// XXX: throwing an exception would be nicer, but would a further
// departure from the old signature of Revision::getContent() when it existed,
// and thus result in more complex and error prone refactoring.
@ -177,7 +177,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
* @return SlotRecord The slot meta-data. If access to the slot's content is forbidden,
* calling getContent() on the SlotRecord will throw an exception.
*/
public function getSlot( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ) {
public function getSlot( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ): SlotRecord {
$slot = $this->mSlots->getSlot( $role );
if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $performer ) ) {
@ -194,7 +194,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
*
* @return bool
*/
public function hasSlot( $role ) {
public function hasSlot( $role ): bool {
return $this->mSlots->hasSlot( $role );
}
@ -204,7 +204,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
*
* @return string[]
*/
public function getSlotRoles() {
public function getSlotRoles(): array {
return $this->mSlots->getSlotRoles();
}
@ -219,7 +219,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
*
* @return RevisionSlots
*/
public function getSlots() {
public function getSlots(): RevisionSlots {
return $this->mSlots;
}
@ -237,7 +237,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
*
* @return RevisionSlots
*/
public function getOriginalSlots() {
public function getOriginalSlots(): RevisionSlots {
return new RevisionSlots( $this->mSlots->getOriginalSlots() );
}
@ -252,7 +252,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
*
* @return RevisionSlots
*/
public function getInheritedSlots() {
public function getInheritedSlots(): RevisionSlots {
return new RevisionSlots( $this->mSlots->getInheritedSlots() );
}
@ -367,7 +367,7 @@ abstract class RevisionRecord implements WikiAwareEntity {
*
* @return PageIdentity
*/
public function getPage() {
public function getPage(): PageIdentity {
return $this->mPage;
}

View file

@ -63,7 +63,7 @@ class RevisionSlots {
/**
* @param SlotRecord[] $slots
*/
private function setSlotsInternal( array $slots ) {
private function setSlotsInternal( array $slots ): void {
Assert::parameterElementType( SlotRecord::class, $slots, '$slots' );
$this->slots = [];
@ -90,7 +90,7 @@ class RevisionSlots {
* could not be lazy-loaded. See SlotRecord::getContent() for details.
* @return Content
*/
public function getContent( $role ) {
public function getContent( $role ): Content {
// Return a copy to be safe. Immutable content objects return $this from copy().
return $this->getSlot( $role )->getContent()->copy();
}
@ -105,7 +105,7 @@ class RevisionSlots {
* could not be lazy-loaded.
* @return SlotRecord
*/
public function getSlot( $role ) {
public function getSlot( $role ): SlotRecord {
$slots = $this->getSlots();
if ( isset( $slots[$role] ) ) {
@ -125,7 +125,7 @@ class RevisionSlots {
*
* @return bool
*/
public function hasSlot( $role ) {
public function hasSlot( $role ): bool {
$slots = $this->getSlots();
return isset( $slots[$role] );
@ -137,7 +137,7 @@ class RevisionSlots {
*
* @return string[]
*/
public function getSlotRoles() {
public function getSlotRoles(): array {
$slots = $this->getSlots();
return array_keys( $slots );
}
@ -150,7 +150,7 @@ class RevisionSlots {
*
* @return int
*/
public function computeSize() {
public function computeSize(): int {
return array_reduce( $this->getPrimarySlots(), static function ( $accu, SlotRecord $slot ) {
return $accu + $slot->getSize();
}, 0 );
@ -165,7 +165,7 @@ class RevisionSlots {
*
* @return SlotRecord[] revision slot/content rows, keyed by slot role name.
*/
public function getSlots() {
public function getSlots(): array {
if ( is_callable( $this->slots ) ) {
$slots = call_user_func( $this->slots );
@ -192,7 +192,7 @@ class RevisionSlots {
*
* @return string
*/
public function computeSha1() {
public function computeSha1(): string {
$slots = $this->getPrimarySlots();
ksort( $slots );
@ -215,7 +215,7 @@ class RevisionSlots {
*
* @return SlotRecord[]
*/
public function getOriginalSlots() {
public function getOriginalSlots(): array {
return array_filter(
$this->getSlots(),
static function ( SlotRecord $slot ) {
@ -232,7 +232,7 @@ class RevisionSlots {
*
* @return SlotRecord[]
*/
public function getInheritedSlots() {
public function getInheritedSlots(): array {
return array_filter(
$this->getSlots(),
static function ( SlotRecord $slot ) {
@ -265,7 +265,7 @@ class RevisionSlots {
*
* @return bool
*/
public function hasSameContent( RevisionSlots $other ) {
public function hasSameContent( RevisionSlots $other ): bool {
if ( $other === $this ) {
return true;
}
@ -300,7 +300,7 @@ class RevisionSlots {
*
* @return string[] a list of slot roles that are different.
*/
public function getRolesWithDifferentContent( RevisionSlots $other ) {
public function getRolesWithDifferentContent( RevisionSlots $other ): array {
if ( $other === $this ) {
return [];
}

View file

@ -4236,7 +4236,7 @@ class Title implements LinkTarget, PageIdentity, IDBAccessObject {
*
* @return BacklinkCache
*/
public function getBacklinkCache() {
public function getBacklinkCache(): BacklinkCache {
return BacklinkCache::get( $this );
}

View file

@ -270,7 +270,7 @@ class ApiParse extends ApiBase {
$pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
$titleObj = $pageObj->getTitle();
if ( !$titleObj || !$titleObj->exists() ) {
if ( !$titleObj->exists() ) {
$this->dieWithError( 'apierror-missingtitle' );
}

View file

@ -306,7 +306,7 @@ class DBConnRef implements IDatabase {
public function selectFieldValues(
$table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
) {
): array {
return $this->__call( __FUNCTION__, func_get_args() );
}

View file

@ -1848,7 +1848,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
public function selectFieldValues(
$table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
) {
): array {
if ( $var === '*' ) { // sanity
throw new DBUnexpectedError( $this, "Cannot use a * field" );
} elseif ( !is_string( $var ) ) { // sanity

View file

@ -634,7 +634,7 @@ interface IDatabase {
*/
public function selectFieldValues(
$table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
);
): array;
/**
* Execute a SELECT query constructed using the various parameters provided

View file

@ -117,7 +117,7 @@ interface ILBFactory {
* @param int|null $owner Owner ID of the new instance (e.g. this LBFactory ID)
* @return ILoadBalancer
*/
public function newMainLB( $domain = false, $owner = null );
public function newMainLB( $domain = false, $owner = null ): ILoadBalancer;
/**
* Get the tracked load balancer instance for a main cluster
@ -127,7 +127,7 @@ interface ILBFactory {
* @param bool|string $domain Domain ID, or false for the current domain
* @return ILoadBalancer
*/
public function getMainLB( $domain = false );
public function getMainLB( $domain = false ): ILoadBalancer;
/**
* Create a new load balancer instance for an external cluster
@ -144,7 +144,7 @@ interface ILBFactory {
* @param int|null $owner Owner ID of the new instance (e.g. this LBFactory ID)
* @return ILoadBalancer
*/
public function newExternalLB( $cluster, $owner = null );
public function newExternalLB( $cluster, $owner = null ): ILoadBalancer;
/**
* Get the tracked load balancer instance for an external cluster
@ -154,7 +154,7 @@ interface ILBFactory {
* @param string $cluster External cluster name
* @return ILoadBalancer
*/
public function getExternalLB( $cluster );
public function getExternalLB( $cluster ): ILoadBalancer;
/**
* Get the tracked load balancer instances for all main clusters
@ -166,7 +166,7 @@ interface ILBFactory {
* @return ILoadBalancer[] Map of (cluster name => ILoadBalancer)
* @since 1.29
*/
public function getAllMainLBs();
public function getAllMainLBs(): array;
/**
* Get the tracked load balancer instances for all external clusters
@ -176,7 +176,7 @@ interface ILBFactory {
* @return ILoadBalancer[] Map of (cluster name => ILoadBalancer)
* @since 1.29
*/
public function getAllExternalLBs();
public function getAllExternalLBs(): array;
/**
* Execute a function for each instantiated tracked load balancer instance

View file

@ -146,7 +146,7 @@ class LBFactoryMulti extends LBFactory {
}
}
public function newMainLB( $domain = false, $owner = null ) {
public function newMainLB( $domain = false, $owner = null ): ILoadBalancer {
$domainInstance = $this->resolveDomainInstance( $domain );
$database = $domainInstance->getDatabase();
$section = $this->getSectionFromDatabase( $database );
@ -172,7 +172,7 @@ class LBFactoryMulti extends LBFactory {
);
}
public function getMainLB( $domain = false ) {
public function getMainLB( $domain = false ): ILoadBalancer {
$domainInstance = $this->resolveDomainInstance( $domain );
$section = $this->getSectionFromDatabase( $domainInstance->getDatabase() );
@ -183,7 +183,7 @@ class LBFactoryMulti extends LBFactory {
return $this->mainLBs[$section];
}
public function newExternalLB( $cluster, $owner = null ) {
public function newExternalLB( $cluster, $owner = null ): ILoadBalancer {
if ( !isset( $this->externalLoadsByCluster[$cluster] ) ) {
throw new InvalidArgumentException( "Unknown cluster '$cluster'" );
}
@ -200,7 +200,7 @@ class LBFactoryMulti extends LBFactory {
);
}
public function getExternalLB( $cluster ) {
public function getExternalLB( $cluster ): ILoadBalancer {
if ( !isset( $this->externalLBs[$cluster] ) ) {
$this->externalLBs[$cluster] = $this->newExternalLB(
$cluster,
@ -211,7 +211,7 @@ class LBFactoryMulti extends LBFactory {
return $this->externalLBs[$cluster];
}
public function getAllMainLBs() {
public function getAllMainLBs(): array {
$lbs = [];
foreach ( $this->sectionsByDB as $db => $section ) {
if ( !isset( $lbs[$section] ) ) {
@ -222,7 +222,7 @@ class LBFactoryMulti extends LBFactory {
return $lbs;
}
public function getAllExternalLBs() {
public function getAllExternalLBs(): array {
$lbs = [];
foreach ( $this->externalLoadsByCluster as $cluster => $unused ) {
$lbs[$cluster] = $this->getExternalLB( $cluster );

View file

@ -74,7 +74,7 @@ class LBFactorySimple extends LBFactory {
}
}
public function newMainLB( $domain = false, $owner = null ) {
public function newMainLB( $domain = false, $owner = null ): ILoadBalancer {
return $this->newLoadBalancer(
self::CLUSTER_MAIN_DEFAULT,
$this->mainServers,
@ -82,7 +82,7 @@ class LBFactorySimple extends LBFactory {
);
}
public function getMainLB( $domain = false ) {
public function getMainLB( $domain = false ): ILoadBalancer {
if ( $this->mainLB === null ) {
$this->mainLB = $this->newMainLB( $domain, $this->getOwnershipId() );
}
@ -90,7 +90,7 @@ class LBFactorySimple extends LBFactory {
return $this->mainLB;
}
public function newExternalLB( $cluster, $owner = null ) {
public function newExternalLB( $cluster, $owner = null ): ILoadBalancer {
if ( !isset( $this->externalServersByCluster[$cluster] ) ) {
throw new InvalidArgumentException( "Unknown cluster '$cluster'." );
}
@ -102,7 +102,7 @@ class LBFactorySimple extends LBFactory {
);
}
public function getExternalLB( $cluster ) {
public function getExternalLB( $cluster ): ILoadBalancer {
if ( !isset( $this->externalLBs[$cluster] ) ) {
$this->externalLBs[$cluster] = $this->newExternalLB(
$cluster,
@ -113,11 +113,11 @@ class LBFactorySimple extends LBFactory {
return $this->externalLBs[$cluster];
}
public function getAllMainLBs() {
public function getAllMainLBs(): array {
return [ self::CLUSTER_MAIN_DEFAULT => $this->getMainLB() ];
}
public function getAllExternalLBs() {
public function getAllExternalLBs(): array {
$lbs = [];
foreach ( array_keys( $this->externalServersByCluster ) as $cluster ) {
$lbs[$cluster] = $this->getExternalLB( $cluster );

View file

@ -69,27 +69,27 @@ class LBFactorySingle extends LBFactory {
) );
}
public function newMainLB( $domain = false, $owner = null ) {
public function newMainLB( $domain = false, $owner = null ): ILoadBalancer {
throw new BadMethodCallException( "Method is not supported." );
}
public function getMainLB( $domain = false ) {
public function getMainLB( $domain = false ): ILoadBalancer {
return $this->lb;
}
public function newExternalLB( $cluster, $owner = null ) {
public function newExternalLB( $cluster, $owner = null ): ILoadBalancer {
throw new BadMethodCallException( "Method is not supported." );
}
public function getExternalLB( $cluster ) {
public function getExternalLB( $cluster ): ILoadBalancer {
throw new BadMethodCallException( "Method is not supported." );
}
public function getAllMainLBs() {
public function getAllMainLBs(): array {
return [ self::CLUSTER_MAIN_DEFAULT => $this->lb ];
}
public function getAllExternalLBs() {
public function getAllExternalLBs(): array {
return [];
}

View file

@ -402,11 +402,11 @@ interface ILoadBalancer {
* @param string[]|string $groups Query group(s) in preference order; [] for the default group
* @param string|bool $domain DB domain ID or false for the local domain
* @param int $flags Bitfield of CONN_* class constants
* @return DBConnRef Live connection handle or null on failure
* @return DBConnRef Live connection handle
* @throws DBError If no live handle could be obtained
* @throws DBAccessError If disable() was previously called
*/
public function getLazyConnectionRef( $i, $groups = [], $domain = false, $flags = 0 );
public function getLazyConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ): DBConnRef;
/**
* Get a live database handle, suitable for migrations and schema changes, for a server index
@ -429,7 +429,7 @@ interface ILoadBalancer {
* @throws DBError If no live handle could be obtained
* @throws DBAccessError If disable() was previously called
*/
public function getMaintenanceConnectionRef( $i, $groups = [], $domain = false, $flags = 0 );
public function getMaintenanceConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ): MaintainableDBConnRef;
/**
* Get the specific server index of the primary server

View file

@ -1099,7 +1099,7 @@ class LoadBalancer implements ILoadBalancer {
return new DBConnRef( $this, $conn, $role );
}
public function getLazyConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ) {
public function getLazyConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ): DBConnRef {
if ( self::fieldHasBit( $flags, self::CONN_SILENCE_ERRORS ) ) {
throw new UnexpectedValueException(
__METHOD__ . ' got CONN_SILENCE_ERRORS; connection is already deferred'
@ -1112,7 +1112,12 @@ class LoadBalancer implements ILoadBalancer {
return new DBConnRef( $this, [ $i, $groups, $domain, $flags ], $role );
}
public function getMaintenanceConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ) {
public function getMaintenanceConnectionRef(
$i,
$groups = [],
$domain = false,
$flags = 0
): MaintainableDBConnRef {
if ( self::fieldHasBit( $flags, self::CONN_SILENCE_ERRORS ) ) {
throw new UnexpectedValueException(
__METHOD__ . ' CONN_SILENCE_ERRORS is not supported'

View file

@ -70,7 +70,7 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
* @todo make protected
* @note for access by subclasses only
*/
public $mTitle = null;
public $mTitle;
/**
* @var bool
@ -315,7 +315,7 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
* Get the title object of the article
* @return Title Title object of this page
*/
public function getTitle() {
public function getTitle(): Title {
return $this->mTitle;
}

View file

@ -1199,7 +1199,7 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
// First fetch the wl_ids from the watchlist table.
// We'd prefer to do a INSERT/SELECT in the same query with IDatabase::insertSelect(),
// but it doesn't allow us to use the "ON DUPLICATE KEY UPDATE" clause.
$wlIds = (array)$dbw->selectFieldValues( 'watchlist', 'wl_id', $cond, __METHOD__ );
$wlIds = $dbw->selectFieldValues( 'watchlist', 'wl_id', $cond, __METHOD__ );
$expiry = $dbw->timestamp( $expiry );

View file

@ -71,7 +71,7 @@ class GetTextMaint extends Maintenance {
RevisionRecord::FOR_PUBLIC;
$content = $rev->getContent( SlotRecord::MAIN, $audience );
if ( $content === false ) {
if ( $content === null ) {
$titleText = $title->getPrefixedText();
$this->fatalError( "Couldn't extract the text from $titleText.\n" );
}

View file

@ -170,9 +170,6 @@ class PopulateArchiveRevId extends LoggedUpdateMaintenance {
[ 'rev_timestamp' => self::$dummyRev['rev_timestamp'] ],
$fname
);
if ( !is_array( $revIds ) ) {
throw new UnexpectedValueException( 'Failed to insert dummy revisions' );
}
if ( count( $revIds ) !== count( $arIds ) ) {
throw new UnexpectedValueException(
'Tried to insert ' . count( $arIds ) . ' dummy revisions, but found '

View file

@ -23,6 +23,7 @@ use ParserOutput;
use PHPUnit\Framework\MockObject\MockObject;
use Title;
use TitleFactory;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use WikitextContent;
@ -48,13 +49,11 @@ class RevisionRendererTest extends MediaWikiIntegrationTestCase {
}
/**
* @param IDatabase&MockObject $db
* @param int $maxRev
*
* @return IDatabase
* @return IDatabase&MockObject
*/
private function getMockDatabaseConnection( $maxRev = 100 ) {
/** @var IDatabase|MockObject $db */
$db = $this->createMock( IDatabase::class );
private function mockDatabaseConnection( $db, $maxRev = 100 ) {
$db->method( 'selectField' )
->willReturnCallback(
function ( $table, $fields, $cond ) use ( $maxRev ) {
@ -78,19 +77,17 @@ class RevisionRendererTest extends MediaWikiIntegrationTestCase {
private function newRevisionRenderer( $maxRev = 100, $usePrimary = false ) {
$dbIndex = $usePrimary ? DB_PRIMARY : DB_REPLICA;
$db = $this->getMockDatabaseConnection( $maxRev );
/** @var ILoadBalancer|MockObject $lb */
$lb = $this->createMock( ILoadBalancer::class );
$lb->method( 'getConnection' )
->with( $dbIndex )
->willReturn( $db );
->willReturn( $this->mockDatabaseConnection( $this->createMock( IDatabase::class ), $maxRev ) );
$lb->method( 'getConnectionRef' )
->with( $dbIndex )
->willReturn( $db );
->willReturn( $this->mockDatabaseConnection( $this->createMock( DBConnRef::class ), $maxRev ) );
$lb->method( 'getLazyConnectionRef' )
->with( $dbIndex )
->willReturn( $db );
->willReturn( $this->mockDatabaseConnection( $this->createMock( DBConnRef::class ), $maxRev ) );
/** @var NameTableStore|MockObject $slotRoles */
$slotRoles = $this->getMockBuilder( NameTableStore::class )

View file

@ -7,9 +7,9 @@ use MediaWiki\User\UserGroupManagerFactory;
use MediaWikiIntegrationTestCase;
use stdClass;
use UserRightsProxy;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Rdbms\MaintainableDBConnRef;
/**
* @coversDefaultClass UserRightsProxy
@ -25,7 +25,7 @@ class UserRightsProxyTest extends MediaWikiIntegrationTestCase {
'wgLocalDatabases' => [ 'foowiki' ],
] );
$dbMock = $this->createMock( DBConnRef::class );
$dbMock = $this->createMock( MaintainableDBConnRef::class );
$row = new stdClass;
$row->user_name = 'UserRightsProxyTest';
@ -155,7 +155,7 @@ class UserRightsProxyTest extends MediaWikiIntegrationTestCase {
$key = 'foo';
$value = 'bar';
$dbMock = $this->createMock( DBConnRef::class );
$dbMock = $this->createMock( MaintainableDBConnRef::class );
$row = new stdClass;
$row->user_name = 'UserRightsProxyTest';
$row->user_id = 12345;

View file

@ -5,6 +5,7 @@ namespace Wikimedia\Tests\Rdbms;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Wikimedia\Rdbms\ConnectionManager;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\LoadBalancer;
@ -134,7 +135,7 @@ class ConnectionManagerTest extends TestCase {
}
public function testGetLazyReadConnectionRef_nullGroups() {
$database = $this->getIDatabaseMock();
$database = $this->createMock( DBConnRef::class );
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
@ -149,7 +150,7 @@ class ConnectionManagerTest extends TestCase {
}
public function testGetLazyReadConnectionRef_withGroups() {
$database = $this->getIDatabaseMock();
$database = $this->createMock( DBConnRef::class );
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )

View file

@ -1527,7 +1527,7 @@ class WatchedItemStoreUnitTest extends MediaWikiUnitTestCase {
$mockDb = $this->getMockDb();
$mockDb->expects( $this->once() )
->method( 'selectFieldValues' )
->willReturn( null );
->willReturn( [] );
$mockDb->expects( $this->never() )
->method( 'delete' );
$mockDb->expects( $this->never() )