From aea6cbdc4217310735169c08646f42928a048823 Mon Sep 17 00:00:00 2001 From: Derick Alangi Date: Sun, 3 Mar 2019 21:10:12 +0100 Subject: [PATCH] Status: Make various improvements to the Status class These improvements touches areas such as; - strict comparison as count() returns in int. - use of single quotes for string literals. - use of RequestContext to get the user's Language object. - fix minor typo. The todo for using RequestContext was added in 7c543bb6c25. It used `$wgLang` at the time for consistency with Message.php, which was changed in 2016 with 0beb5ca992c. Hence, it's now fine here as well. Change-Id: I943dfd761a8fca7e8b6658e3c9510ddc6694f042 --- includes/Status.php | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/includes/Status.php b/includes/Status.php index f880663a06..76b905eca4 100644 --- a/includes/Status.php +++ b/includes/Status.php @@ -38,7 +38,7 @@ * so that a lack of error-handling will be explicit. */ class Status extends StatusValue { - /** @var callable */ + /** @var callable|false */ public $cleanCallback = false; /** @@ -78,7 +78,8 @@ class Status extends StatusValue { function __get( $name ) { if ( $name === 'ok' ) { return $this->isOK(); - } elseif ( $name === 'errors' ) { + } + if ( $name === 'errors' ) { return $this->getErrors(); } @@ -109,7 +110,7 @@ class Status extends StatusValue { * the error messages, and one that contains the warnings, only. The returned array is * defined as: * [ - * 0 => object(Status) # the Status with error messages, only + * 0 => object(Status) # The Status with error messages, only * 1 => object(Status) # The Status with warning messages, only * ] * @@ -148,21 +149,18 @@ class Status extends StatusValue { } /** - * @param string|Language|null $lang Language to use for processing + * @param string|Language|null|StubUserLang $lang Language to use for processing * messages, or null to default to the user language. - * @return Language + * @return Language|StubUserLang */ protected function languageFromParam( $lang ) { - global $wgLang; - if ( $lang === null ) { - // @todo: Use RequestContext::getMain()->getLanguage() instead - return $wgLang; - } elseif ( $lang instanceof Language || $lang instanceof StubUserLang ) { + return RequestContext::getMain()->getLanguage(); + } + if ( $lang instanceof Language || $lang instanceof StubUserLang ) { return $lang; - } else { - return Language::factory( $lang ); } + return Language::factory( $lang ); } /** @@ -171,14 +169,14 @@ class Status extends StatusValue { * @param string|bool $shortContext A short enclosing context message name, to * be used when there is a single error * @param string|bool $longContext A long enclosing context message name, for a list - * @param string|Language|null $lang Language to use for processing messages + * @param string|Language|null|StubUserLang $lang Language to use for processing messages * @return string */ public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) { $lang = $this->languageFromParam( $lang ); $rawErrors = $this->getErrors(); - if ( count( $rawErrors ) == 0 ) { + if ( count( $rawErrors ) === 0 ) { if ( $this->isOK() ) { $this->fatal( 'internalerror_info', __METHOD__ . " called for a good result, this is incorrect\n" ); @@ -188,7 +186,7 @@ class Status extends StatusValue { } $rawErrors = $this->getErrors(); // just added a fatal } - if ( count( $rawErrors ) == 1 ) { + if ( count( $rawErrors ) === 1 ) { $s = $this->getErrorMessage( $rawErrors[0], $lang )->plain(); if ( $shortContext ) { $s = wfMessage( $shortContext, $s )->inLanguage( $lang )->plain(); @@ -234,7 +232,7 @@ class Status extends StatusValue { $lang = $this->languageFromParam( $lang ); $rawErrors = $this->getErrors(); - if ( count( $rawErrors ) == 0 ) { + if ( count( $rawErrors ) === 0 ) { if ( $this->isOK() ) { $this->fatal( 'internalerror_info', __METHOD__ . " called for a good result, this is incorrect\n" ); @@ -244,7 +242,7 @@ class Status extends StatusValue { } $rawErrors = $this->getErrors(); // just added a fatal } - if ( count( $rawErrors ) == 1 ) { + if ( count( $rawErrors ) === 1 ) { $s = $this->getErrorMessage( $rawErrors[0], $lang ); if ( $shortContext ) { $s = wfMessage( $shortContext, $s )->inLanguage( $lang ); @@ -297,7 +295,7 @@ class Status extends StatusValue { } elseif ( is_string( $error ) ) { $msg = wfMessage( $error ); } else { - throw new UnexpectedValueException( "Got " . get_class( $error ) . " for key." ); + throw new UnexpectedValueException( 'Got ' . get_class( $error ) . ' for key.' ); } $msg->inLanguage( $this->languageFromParam( $lang ) ); -- 2.20.1