From b611d49ac5cce4d772a4e75ffb39cd977229163c Mon Sep 17 00:00:00 2001 From: addshore Date: Thu, 17 Sep 2015 10:56:22 +0100 Subject: [PATCH] Refactor changeTypes in RecentChange This is split from: I03516bb34144d95e5f25c46ae98ab70ce699b31b Change-Id: I285cdbe3d1a62aa1071a5c174928136c3f59d557 --- includes/changes/RecentChange.php | 52 ++++++++++++++----------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/includes/changes/RecentChange.php b/includes/changes/RecentChange.php index 54ca2ab23a..87871f494d 100644 --- a/includes/changes/RecentChange.php +++ b/includes/changes/RecentChange.php @@ -89,6 +89,16 @@ class RecentChange { */ public $counter = -1; + /** + * @var array Array of change types + */ + private static $changeTypes = array( + 'edit' => RC_EDIT, + 'new' => RC_NEW, + 'log' => RC_LOG, + 'external' => RC_EXTERNAL, + ); + # Factory methods /** @@ -119,18 +129,10 @@ class RecentChange { return $retval; } - switch ( $type ) { - case 'edit': - return RC_EDIT; - case 'new': - return RC_NEW; - case 'log': - return RC_LOG; - case 'external': - return RC_EXTERNAL; - default: - throw new MWException( "Unknown type '$type'" ); + if ( !array_key_exists( $type, self::$changeTypes ) ) { + throw new MWException( "Unknown type '$type'" ); } + return self::$changeTypes[$type]; } /** @@ -140,24 +142,18 @@ class RecentChange { * @return string $type */ public static function parseFromRCType( $rcType ) { - switch ( $rcType ) { - case RC_EDIT: - $type = 'edit'; - break; - case RC_NEW: - $type = 'new'; - break; - case RC_LOG: - $type = 'log'; - break; - case RC_EXTERNAL: - $type = 'external'; - break; - default: - $type = "$rcType"; - } + return array_search( $rcType, self::$changeTypes, true ) ?: "$rcType"; + } - return $type; + /** + * Get an array of all change types + * + * @since 1.26 + * + * @return array + */ + public static function getChangeTypes() { + return array_keys( self::$changeTypes ); } /** -- 2.20.1