Hygiene: Add return types for known services

We can define return types using `function(): TYPE` notation.
We should do that when it's possible.

For now, it's not possible to type nullable return type (PHP7.1
supports that, but PHP7.0 and most probably HVVM doesn't),

Change-Id: I6b58a882383832313ef47a296c726a4044a97954
This commit is contained in:
Piotr Miazga 2019-06-21 18:52:22 +02:00
parent 6352190684
commit 8c7553935e
12 changed files with 32 additions and 31 deletions

View File

@ -30,7 +30,7 @@ use MediaWiki\Minerva\SkinOptions;
use MediaWiki\Minerva\SkinUserPageHelper; use MediaWiki\Minerva\SkinUserPageHelper;
return [ return [
'Minerva.Menu.MainDirector' => function ( MediaWikiServices $services ) { 'Minerva.Menu.MainDirector' => function ( MediaWikiServices $services ): MainMenu\Director {
$context = RequestContext::getMain(); $context = RequestContext::getMain();
/** @var SkinOptions $options */ /** @var SkinOptions $options */
$options = $services->getService( 'Minerva.SkinOptions' ); $options = $services->getService( 'Minerva.SkinOptions' );
@ -43,7 +43,8 @@ return [
return new MainMenu\Director( $builder ); return new MainMenu\Director( $builder );
}, },
'Minerva.Menu.PageActionsDirector' => function ( MediaWikiServices $services ) { 'Minerva.Menu.PageActionsDirector' =>
function ( MediaWikiServices $services ): PageActionsMenu\PageActionsDirector {
/** /**
* @var SkinOptions $skinOptions * @var SkinOptions $skinOptions
* @var SkinMinerva $skin * @var SkinMinerva $skin
@ -77,12 +78,11 @@ return [
$overflowBuilder, $overflowBuilder,
$context $context
); );
}, },
'Minerva.SkinUserPageHelper' => function (): SkinUserPageHelper {
'Minerva.SkinUserPageHelper' => function () {
return new SkinUserPageHelper( RequestContext::getMain()->getTitle() ); return new SkinUserPageHelper( RequestContext::getMain()->getTitle() );
}, },
'Minerva.SkinOptions' => function () { 'Minerva.SkinOptions' => function (): SkinOptions {
return new SkinOptions(); return new SkinOptions();
}, },
'Minerva.Permissions' => function ( MediaWikiServices $services ): IMinervaPagePermissions { 'Minerva.Permissions' => function ( MediaWikiServices $services ): IMinervaPagePermissions {

View File

@ -35,11 +35,11 @@ use MediaWiki\Minerva\Menu\Group;
class AdvancedBuilder extends DefaultBuilder { class AdvancedBuilder extends DefaultBuilder {
/** /**
* @return array|Group[] * @return Group[]
* @throws FatalError * @throws FatalError
* @throws MWException * @throws MWException
*/ */
public function getGroups() { public function getGroups(): array {
return [ return [
$this->getDiscoveryTools(), $this->getDiscoveryTools(),
$this->getPersonalTools(), $this->getPersonalTools(),
@ -54,7 +54,7 @@ class AdvancedBuilder extends DefaultBuilder {
* @throws FatalError * @throws FatalError
* @throws MWException * @throws MWException
*/ */
public function getSiteTools() { public function getSiteTools(): Group {
$group = new Group(); $group = new Group();
$this->definitions->insertSpecialPages( $group ); $this->definitions->insertSpecialPages( $group );

View File

@ -66,7 +66,7 @@ class DefaultBuilder implements IBuilder {
* @throws FatalError * @throws FatalError
* @throws MWException * @throws MWException
*/ */
public function getGroups() { public function getGroups(): array {
return [ return [
$this->getDiscoveryTools(), $this->getDiscoveryTools(),
$this->getPersonalTools(), $this->getPersonalTools(),
@ -80,7 +80,7 @@ class DefaultBuilder implements IBuilder {
* @throws FatalError * @throws FatalError
* @throws MWException * @throws MWException
*/ */
protected function getDiscoveryTools() { protected function getDiscoveryTools(): Group {
$group = new Group(); $group = new Group();
$this->definitions->insertHomeItem( $group ); $this->definitions->insertHomeItem( $group );
@ -101,7 +101,7 @@ class DefaultBuilder implements IBuilder {
* @throws FatalError * @throws FatalError
* @throws MWException * @throws MWException
*/ */
protected function getPersonalTools() { protected function getPersonalTools(): Group {
$group = new Group(); $group = new Group();
$this->definitions->insertLogInOutMenuItem( $group ); $this->definitions->insertLogInOutMenuItem( $group );
@ -124,7 +124,7 @@ class DefaultBuilder implements IBuilder {
* @return Group * @return Group
* @throws MWException * @throws MWException
*/ */
protected function getConfigurationTools() { protected function getConfigurationTools(): Group {
$group = new Group(); $group = new Group();
$this->showMobileOptions ? $this->showMobileOptions ?
@ -139,7 +139,7 @@ class DefaultBuilder implements IBuilder {
* @return Group Collection of site links * @return Group Collection of site links
* @throws MWException * @throws MWException
*/ */
public function getSiteLinks() { public function getSiteLinks(): Group {
$group = new Group(); $group = new Group();
$this->definitions->insertAboutItem( $group ); $this->definitions->insertAboutItem( $group );

View File

@ -47,7 +47,7 @@ final class Director {
* Returns a data representation of the main menus * Returns a data representation of the main menus
* @return array * @return array
*/ */
public function getMenuData() { public function getMenuData(): array {
if ( $this->menuData === null ) { if ( $this->menuData === null ) {
$this->menuData = $this->buildMenu(); $this->menuData = $this->buildMenu();
} }
@ -58,7 +58,7 @@ final class Director {
* Build the menu data array that can be passed to views/javascript * Build the menu data array that can be passed to views/javascript
* @return array * @return array
*/ */
private function buildMenu() { private function buildMenu(): array {
$menuData = [ $menuData = [
'groups' => [], 'groups' => [],
'sitelinks' => $this->builder->getSiteLinks()->getEntries() 'sitelinks' => $this->builder->getSiteLinks()->getEntries()

View File

@ -27,10 +27,10 @@ interface IBuilder {
/** /**
* @return Group[] * @return Group[]
*/ */
public function getGroups(); public function getGroups(): array;
/** /**
* @return Group * @return Group
*/ */
public function getSiteLinks(); public function getSiteLinks(): Group;
} }

View File

@ -44,7 +44,7 @@ class DefaultOverflowBuilder implements IOverflowBuilder {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function getGroup( array $toolbox ) { public function getGroup( array $toolbox ): Group {
$group = new Group(); $group = new Group();
$possibleEntries = array_filter( [ $possibleEntries = array_filter( [
$this->build( 'info', 'info', 'info', $toolbox ), $this->build( 'info', 'info', 'info', $toolbox ),

View File

@ -27,7 +27,7 @@ class EmptyOverflowBuilder implements IOverflowBuilder {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function getGroup( array $toolbox ) { public function getGroup( array $toolbox ): Group {
return new Group(); return new Group();
} }
} }

View File

@ -28,5 +28,5 @@ interface IOverflowBuilder {
* @param array $toolbox An array of common toolbox items from the sidebar menu * @param array $toolbox An array of common toolbox items from the sidebar menu
* @return Group * @return Group
*/ */
public function getGroup( array $toolbox ); public function getGroup( array $toolbox ): Group;
} }

View File

@ -62,7 +62,7 @@ class PageActionMenuEntry implements IMenuEntry {
* @param Message $message Message * @param Message $message Message
* @return self * @return self
*/ */
public static function create( $name, $href, $componentClass, Message $message ) { public static function create( $name, $href, $componentClass, Message $message ): self {
return new PageActionMenuEntry( $name, $href, $componentClass, $message ); return new PageActionMenuEntry( $name, $href, $componentClass, $message );
} }
@ -93,7 +93,7 @@ class PageActionMenuEntry implements IMenuEntry {
* @param Message $message Title message * @param Message $message Title message
* @return $this * @return $this
*/ */
public function setTitle( Message $message ) { public function setTitle( Message $message ): self {
$this->component['title'] = $message->escaped(); $this->component['title'] = $message->escaped();
return $this; return $this;
} }
@ -103,7 +103,7 @@ class PageActionMenuEntry implements IMenuEntry {
* @param string $nodeID * @param string $nodeID
* @return $this * @return $this
*/ */
public function setNodeID( $nodeID ) { public function setNodeID( $nodeID ): self {
$this->component['id'] = $nodeID; $this->component['id'] = $nodeID;
return $this; return $this;
} }

View File

@ -23,6 +23,7 @@ namespace MediaWiki\Minerva\Menu\PageActions;
use ExtensionRegistry; use ExtensionRegistry;
use Hooks; use Hooks;
use MediaWiki\Minerva\Menu\Group; use MediaWiki\Minerva\Menu\Group;
use MediaWiki\Minerva\Menu\IMenuEntry;
use MediaWiki\Minerva\Menu\LanguageSelectorEntry; use MediaWiki\Minerva\Menu\LanguageSelectorEntry;
use MediaWiki\Minerva\Permissions\IMinervaPagePermissions; use MediaWiki\Minerva\Permissions\IMinervaPagePermissions;
use MediaWiki\Permissions\PermissionManager; use MediaWiki\Permissions\PermissionManager;
@ -85,7 +86,7 @@ class ToolbarBuilder {
* @return Group * @return Group
* @throws MWException * @throws MWException
*/ */
public function getGroup( $doesPageHaveLanguages ) { public function getGroup( $doesPageHaveLanguages ): Group {
$group = new Group(); $group = new Group();
$permissions = $this->permissions; $permissions = $this->permissions;
@ -119,7 +120,7 @@ class ToolbarBuilder {
* @throws MWException * @throws MWException
* @throws \Exception * @throws \Exception
*/ */
protected function createEditPageAction() { protected function createEditPageAction(): IMenuEntry {
$title = $this->title; $title = $this->title;
$user = $this->user; $user = $this->user;
$pm = $this->permissionsManager; $pm = $this->permissionsManager;
@ -161,7 +162,7 @@ class ToolbarBuilder {
* @return PageActionMenuEntry An watch/unwatch page actions menu entry * @return PageActionMenuEntry An watch/unwatch page actions menu entry
* @throws MWException * @throws MWException
*/ */
protected function createWatchPageAction() { protected function createWatchPageAction(): IMenuEntry {
$title = $this->title; $title = $this->title;
$user = $this->user; $user = $this->user;
$isWatched = $title && $user->isLoggedIn() && $user->isWatched( $title ); $isWatched = $title && $user->isLoggedIn() && $user->isWatched( $title );
@ -200,7 +201,7 @@ class ToolbarBuilder {
* and a 'text' property to be used with the pageActionMenu.mustache template. * and a 'text' property to be used with the pageActionMenu.mustache template.
* @throws MWException * @throws MWException
*/ */
protected function getHistoryPageAction() { protected function getHistoryPageAction(): IMenuEntry {
return new PageActionMenuEntry( return new PageActionMenuEntry(
'page-actions-history', 'page-actions-history',
$this->getHistoryUrl( $this->title ), $this->getHistoryUrl( $this->title ),

View File

@ -55,7 +55,7 @@ class UserNamespaceOverflowBuilder implements IOverflowBuilder {
* @inheritDoc * @inheritDoc
* @throws MWException * @throws MWException
*/ */
public function getGroup( array $toolbox ) { public function getGroup( array $toolbox ): Group {
$group = new Group(); $group = new Group();
$group->insertEntry( $this->build( $group->insertEntry( $this->build(
'uploads', 'upload', SpecialPage::getTitleFor( 'Uploads', $this->pageUser )->getLocalURL() 'uploads', 'upload', SpecialPage::getTitleFor( 'Uploads', $this->pageUser )->getLocalURL()

View File

@ -66,7 +66,7 @@ class SkinMinerva extends SkinTemplate {
* object) * object)
* @return IMinervaPagePermissions * @return IMinervaPagePermissions
*/ */
private function getPermissions() { private function getPermissions(): IMinervaPagePermissions {
if ( $this->permissions === null ) { if ( $this->permissions === null ) {
$this->permissions = MediaWikiServices::getInstance() $this->permissions = MediaWikiServices::getInstance()
->getService( 'Minerva.Permissions' ); ->getService( 'Minerva.Permissions' );
@ -86,7 +86,7 @@ class SkinMinerva extends SkinTemplate {
* *
* @return MainMenuDirector * @return MainMenuDirector
*/ */
protected function getMainMenu() { protected function getMainMenu(): MainMenuDirector {
if ( !$this->mainMenu ) { if ( !$this->mainMenu ) {
$this->mainMenu = MediaWikiServices::getInstance()->getService( 'Minerva.Menu.MainDirector' ); $this->mainMenu = MediaWikiServices::getInstance()->getService( 'Minerva.Menu.MainDirector' );
} }