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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@ class EmptyOverflowBuilder implements IOverflowBuilder {
/**
* @inheritDoc
*/
public function getGroup( array $toolbox ) {
public function getGroup( array $toolbox ): 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
* @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
* @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 );
}
@ -93,7 +93,7 @@ class PageActionMenuEntry implements IMenuEntry {
* @param Message $message Title message
* @return $this
*/
public function setTitle( Message $message ) {
public function setTitle( Message $message ): self {
$this->component['title'] = $message->escaped();
return $this;
}
@ -103,7 +103,7 @@ class PageActionMenuEntry implements IMenuEntry {
* @param string $nodeID
* @return $this
*/
public function setNodeID( $nodeID ) {
public function setNodeID( $nodeID ): self {
$this->component['id'] = $nodeID;
return $this;
}

View File

@ -23,6 +23,7 @@ namespace MediaWiki\Minerva\Menu\PageActions;
use ExtensionRegistry;
use Hooks;
use MediaWiki\Minerva\Menu\Group;
use MediaWiki\Minerva\Menu\IMenuEntry;
use MediaWiki\Minerva\Menu\LanguageSelectorEntry;
use MediaWiki\Minerva\Permissions\IMinervaPagePermissions;
use MediaWiki\Permissions\PermissionManager;
@ -85,7 +86,7 @@ class ToolbarBuilder {
* @return Group
* @throws MWException
*/
public function getGroup( $doesPageHaveLanguages ) {
public function getGroup( $doesPageHaveLanguages ): Group {
$group = new Group();
$permissions = $this->permissions;
@ -119,7 +120,7 @@ class ToolbarBuilder {
* @throws MWException
* @throws \Exception
*/
protected function createEditPageAction() {
protected function createEditPageAction(): IMenuEntry {
$title = $this->title;
$user = $this->user;
$pm = $this->permissionsManager;
@ -161,7 +162,7 @@ class ToolbarBuilder {
* @return PageActionMenuEntry An watch/unwatch page actions menu entry
* @throws MWException
*/
protected function createWatchPageAction() {
protected function createWatchPageAction(): IMenuEntry {
$title = $this->title;
$user = $this->user;
$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.
* @throws MWException
*/
protected function getHistoryPageAction() {
protected function getHistoryPageAction(): IMenuEntry {
return new PageActionMenuEntry(
'page-actions-history',
$this->getHistoryUrl( $this->title ),

View File

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

View File

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