diff options
Diffstat (limited to 'includes/libs/Message')
-rw-r--r-- | includes/libs/Message/DataMessageValue.php | 32 | ||||
-rw-r--r-- | includes/libs/Message/IMessageFormatterFactory.php | 2 | ||||
-rw-r--r-- | includes/libs/Message/ITextFormatter.php | 2 | ||||
-rw-r--r-- | includes/libs/Message/ListParam.php | 13 | ||||
-rw-r--r-- | includes/libs/Message/MessageParam.php | 7 | ||||
-rw-r--r-- | includes/libs/Message/MessageSpecifier.php | 4 | ||||
-rw-r--r-- | includes/libs/Message/MessageValue.php | 59 | ||||
-rw-r--r-- | includes/libs/Message/ScalarParam.php | 17 |
8 files changed, 69 insertions, 67 deletions
diff --git a/includes/libs/Message/DataMessageValue.php b/includes/libs/Message/DataMessageValue.php index 710af0a349c6..79d716af8f67 100644 --- a/includes/libs/Message/DataMessageValue.php +++ b/includes/libs/Message/DataMessageValue.php @@ -25,11 +25,8 @@ use Wikimedia\JsonCodec\JsonCodecableTrait; class DataMessageValue extends MessageValue { use JsonCodecableTrait; - /** @var string */ - private $code; - - /** @var array|null */ - private $data; + private string $code; + private ?array $data; /** * @stable to call @@ -41,7 +38,7 @@ class DataMessageValue extends MessageValue { * @param array|null $data Structured data representing the concept * behind this message. */ - public function __construct( $key, $params = [], $code = null, ?array $data = null ) { + public function __construct( string $key, array $params = [], ?string $code = null, ?array $data = null ) { parent::__construct( $key, $params ); $this->code = $code ?? $key; @@ -50,13 +47,20 @@ class DataMessageValue extends MessageValue { /** * Static constructor for easier chaining of `->params()` methods + * * @param string $key * @param (MessageParam|MessageValue|string|int|float)[] $params * @param string|null $code * @param array|null $data + * * @return DataMessageValue */ - public static function new( $key, $params = [], $code = null, ?array $data = null ) { + public static function new( + string $key, + array $params = [], + ?string $code = null, + ?array $data = null + ): DataMessageValue { return new DataMessageValue( $key, $params, $code, $data ); } @@ -64,7 +68,7 @@ class DataMessageValue extends MessageValue { * Get the message code * @return string */ - public function getCode() { + public function getCode(): string { return $this->code; } @@ -72,11 +76,11 @@ class DataMessageValue extends MessageValue { * Get the message's structured data * @return array|null */ - public function getData() { + public function getData(): ?array { return $this->data; } - public function dump() { + public function dump(): string { $contents = ''; if ( $this->getParams() ) { $contents = '<params>'; @@ -100,12 +104,12 @@ class DataMessageValue extends MessageValue { // WARNING: When changing how this class is serialized, follow the instructions // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! return parent::toJsonArray() + [ - 'code' => $this->code, - 'data' => $this->data, - ]; + 'code' => $this->code, + 'data' => $this->data, + ]; } - public static function newFromJsonArray( array $json ) { + public static function newFromJsonArray( array $json ): DataMessageValue { // WARNING: When changing how this class is serialized, follow the instructions // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! return new self( $json['key'], $json['params'], $json['code'], $json['data'] ); diff --git a/includes/libs/Message/IMessageFormatterFactory.php b/includes/libs/Message/IMessageFormatterFactory.php index 337ea82436d4..59ed05cef55d 100644 --- a/includes/libs/Message/IMessageFormatterFactory.php +++ b/includes/libs/Message/IMessageFormatterFactory.php @@ -14,5 +14,5 @@ interface IMessageFormatterFactory { * @param string $langCode The language code * @return ITextFormatter */ - public function getTextFormatter( $langCode ): ITextFormatter; + public function getTextFormatter( string $langCode ): ITextFormatter; } diff --git a/includes/libs/Message/ITextFormatter.php b/includes/libs/Message/ITextFormatter.php index fa71a26a95f6..9d6ac9ea597e 100644 --- a/includes/libs/Message/ITextFormatter.php +++ b/includes/libs/Message/ITextFormatter.php @@ -20,7 +20,7 @@ interface ITextFormatter { * Get the internal language code in which format() is * @return string */ - public function getLangCode(); + public function getLangCode(): string; /** * Convert a MessageSpecifier to text. diff --git a/includes/libs/Message/ListParam.php b/includes/libs/Message/ListParam.php index 058bc8525938..27435d7ec641 100644 --- a/includes/libs/Message/ListParam.php +++ b/includes/libs/Message/ListParam.php @@ -15,8 +15,7 @@ use Wikimedia\JsonCodec\JsonCodecableTrait; class ListParam extends MessageParam { use JsonCodecableTrait; - /** @var string */ - private $listType; + private string $listType; /** * @stable to call. @@ -25,7 +24,7 @@ class ListParam extends MessageParam { * @param (MessageParam|MessageSpecifier|string|int|float)[] $elements Values in the list. * Values that are not instances of MessageParam are wrapped using ParamType::TEXT. */ - public function __construct( $listType, array $elements ) { + public function __construct( string $listType, array $elements ) { if ( !in_array( $listType, ListType::cases() ) ) { throw new InvalidArgumentException( '$listType must be one of the ListType constants' ); } @@ -46,16 +45,16 @@ class ListParam extends MessageParam { * * @return string One of the ListType constants */ - public function getListType() { + public function getListType(): string { return $this->listType; } - public function dump() { + public function dump(): string { $contents = ''; foreach ( $this->value as $element ) { $contents .= $element->dump(); } - return "<{$this->type} listType=\"{$this->listType}\">$contents</{$this->type}>"; + return "<$this->type listType=\"$this->listType\">$contents</$this->type>"; } public function toJsonArray(): array { @@ -67,7 +66,7 @@ class ListParam extends MessageParam { ]; } - public static function newFromJsonArray( array $json ) { + public static function newFromJsonArray( array $json ): ListParam { // WARNING: When changing how this class is serialized, follow the instructions // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! if ( count( $json ) !== 2 || !isset( $json[ParamType::LIST] ) || !isset( $json['type'] ) ) { diff --git a/includes/libs/Message/MessageParam.php b/includes/libs/Message/MessageParam.php index 2292b6181db6..fc26a318c4e2 100644 --- a/includes/libs/Message/MessageParam.php +++ b/includes/libs/Message/MessageParam.php @@ -11,8 +11,7 @@ use Wikimedia\JsonCodec\JsonCodecable; */ abstract class MessageParam implements JsonCodecable { - /** @var string */ - protected $type; + protected string $type; /** @var mixed */ protected $value; @@ -21,7 +20,7 @@ abstract class MessageParam implements JsonCodecable { * * @return string One of the ParamType constants */ - public function getType() { + public function getType(): string { return $this->type; } @@ -39,5 +38,5 @@ abstract class MessageParam implements JsonCodecable { * * @return string */ - abstract public function dump(); + abstract public function dump(): string; } diff --git a/includes/libs/Message/MessageSpecifier.php b/includes/libs/Message/MessageSpecifier.php index 843e431a8b18..dea4b975df0b 100644 --- a/includes/libs/Message/MessageSpecifier.php +++ b/includes/libs/Message/MessageSpecifier.php @@ -33,14 +33,14 @@ interface MessageSpecifier { * * @return string */ - public function getKey(); + public function getKey(): string; /** * Returns the message parameters * * @return (MessageParam|MessageSpecifier|string|int|float)[] */ - public function getParams(); + public function getParams(): array; } /** diff --git a/includes/libs/Message/MessageValue.php b/includes/libs/Message/MessageValue.php index ca25cca2c3c9..79ad579a8a60 100644 --- a/includes/libs/Message/MessageValue.php +++ b/includes/libs/Message/MessageValue.php @@ -19,11 +19,10 @@ use Wikimedia\JsonCodec\JsonCodecableTrait; class MessageValue implements MessageSpecifier, JsonCodecable { use JsonCodecableTrait; - /** @var string */ - private $key; + private string $key; /** @var MessageParam[] */ - private $params; + private array $params; /** * @stable to call @@ -32,7 +31,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param (MessageParam|MessageSpecifier|string|int|float)[] $params Values that are not instances * of MessageParam are wrapped using ParamType::TEXT. */ - public function __construct( $key, $params = [] ) { + public function __construct( string $key, array $params = [] ) { $this->key = $key; $this->params = []; $this->params( ...$params ); @@ -44,7 +43,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param (MessageParam|MessageSpecifier|string|int|float)[] $params * @return MessageValue */ - public static function new( $key, $params = [] ) { + public static function new( string $key, array $params = [] ): MessageValue { return new MessageValue( $key, $params ); } @@ -57,7 +56,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param MessageSpecifier $spec * @return MessageValue */ - public static function newFromSpecifier( MessageSpecifier $spec ) { + public static function newFromSpecifier( MessageSpecifier $spec ): MessageValue { if ( $spec instanceof MessageValue ) { return $spec; } @@ -69,7 +68,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * * @return string */ - public function getKey() { + public function getKey(): string { return $this->key; } @@ -78,7 +77,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * * @return MessageParam[] */ - public function getParams() { + public function getParams(): array { return $this->params; } @@ -88,7 +87,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param MessageParam|MessageSpecifier|string|int|float ...$values * @return $this */ - public function params( ...$values ) { + public function params( ...$values ): MessageValue { foreach ( $values as $value ) { if ( $value instanceof MessageParam ) { $this->params[] = $value; @@ -106,7 +105,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param MessageSpecifier|string|int|float ...$values Scalar values * @return $this */ - public function textParamsOfType( $type, ...$values ) { + public function textParamsOfType( string $type, ...$values ): MessageValue { foreach ( $values as $value ) { $this->params[] = new ScalarParam( $type, $value ); } @@ -121,7 +120,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * is an array of items suitable to pass as $params to ListParam::__construct() * @return $this */ - public function listParamsOfType( $listType, ...$values ) { + public function listParamsOfType( string $listType, ...$values ): MessageValue { foreach ( $values as $value ) { $this->params[] = new ListParam( $listType, $value ); } @@ -134,7 +133,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param MessageSpecifier|string|int|float ...$values * @return $this */ - public function textParams( ...$values ) { + public function textParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::TEXT, ...$values ); } @@ -144,7 +143,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param int|float ...$values * @return $this */ - public function numParams( ...$values ) { + public function numParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::NUM, ...$values ); } @@ -158,7 +157,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param int|float ...$values * @return $this */ - public function longDurationParams( ...$values ) { + public function longDurationParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::DURATION_LONG, ...$values ); } @@ -172,7 +171,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param int|float ...$values * @return $this */ - public function shortDurationParams( ...$values ) { + public function shortDurationParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::DURATION_SHORT, ...$values ); } @@ -183,7 +182,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * or "infinity" * @return $this */ - public function expiryParams( ...$values ) { + public function expiryParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::EXPIRY, ...$values ); } @@ -194,7 +193,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param string ...$values Timestamp as accepted by the Wikimedia\Timestamp library. * @return $this */ - public function dateTimeParams( ...$values ) { + public function dateTimeParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::DATETIME, ...$values ); } @@ -205,7 +204,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param string ...$values Timestamp as accepted by the Wikimedia\Timestamp library. * @return $this */ - public function dateParams( ...$values ) { + public function dateParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::DATE, ...$values ); } @@ -216,7 +215,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param string ...$values Timestamp as accepted by the Wikimedia\Timestamp library. * @return $this */ - public function timeParams( ...$values ) { + public function timeParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::TIME, ...$values ); } @@ -227,7 +226,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param string ...$values User Groups * @return $this */ - public function userGroupParams( ...$values ) { + public function userGroupParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::GROUP, ...$values ); } @@ -237,7 +236,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param int ...$values * @return $this */ - public function sizeParams( ...$values ) { + public function sizeParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::SIZE, ...$values ); } @@ -248,7 +247,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param int|float ...$values * @return $this */ - public function bitrateParams( ...$values ) { + public function bitrateParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::BITRATE, ...$values ); } @@ -262,7 +261,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param string ...$values * @return $this */ - public function rawParams( ...$values ) { + public function rawParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::RAW, ...$values ); } @@ -276,7 +275,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param string ...$values * @return $this */ - public function plaintextParams( ...$values ) { + public function plaintextParams( ...$values ): MessageValue { return $this->textParamsOfType( ParamType::PLAINTEXT, ...$values ); } @@ -290,7 +289,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * is an array of items suitable to pass as $params to ListParam::__construct() * @return $this */ - public function commaListParams( ...$values ) { + public function commaListParams( ...$values ): MessageValue { return $this->listParamsOfType( ListType::COMMA, ...$values ); } @@ -304,7 +303,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * is an array of items suitable to pass as $params to ListParam::__construct() * @return $this */ - public function semicolonListParams( ...$values ) { + public function semicolonListParams( ...$values ): MessageValue { return $this->listParamsOfType( ListType::SEMICOLON, ...$values ); } @@ -318,7 +317,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * is an array of items suitable to pass as $params to ListParam::__construct() * @return $this */ - public function pipeListParams( ...$values ) { + public function pipeListParams( ...$values ): MessageValue { return $this->listParamsOfType( ListType::PIPE, ...$values ); } @@ -332,7 +331,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * @param (MessageParam|string)[] ...$values * @return $this */ - public function textListParams( ...$values ) { + public function textListParams( ...$values ): MessageValue { return $this->listParamsOfType( ListType::AND, ...$values ); } @@ -341,7 +340,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { * * @return string */ - public function dump() { + public function dump(): string { $contents = ''; foreach ( $this->params as $param ) { $contents .= $param->dump(); @@ -359,7 +358,7 @@ class MessageValue implements MessageSpecifier, JsonCodecable { ]; } - public static function newFromJsonArray( array $json ) { + public static function newFromJsonArray( array $json ): MessageValue { // WARNING: When changing how this class is serialized, follow the instructions // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! return new self( $json['key'], $json['params'] ); diff --git a/includes/libs/Message/ScalarParam.php b/includes/libs/Message/ScalarParam.php index 82f6eb4dc56f..e698367ab405 100644 --- a/includes/libs/Message/ScalarParam.php +++ b/includes/libs/Message/ScalarParam.php @@ -24,7 +24,7 @@ class ScalarParam extends MessageParam { * @param string $type One of the ParamType constants. * @param string|int|float|MessageSpecifier|Stringable $value */ - public function __construct( $type, $value ) { + public function __construct( string $type, $value ) { if ( !in_array( $type, ParamType::cases() ) ) { throw new InvalidArgumentException( '$type must be one of the ParamType constants' ); } @@ -57,13 +57,13 @@ class ScalarParam extends MessageParam { $this->value = $value; } - public function dump() { + public function dump(): string { if ( $this->value instanceof MessageValue ) { $contents = $this->value->dump(); } else { $contents = htmlspecialchars( (string)$this->value ); } - return "<{$this->type}>" . $contents . "</{$this->type}>"; + return "<$this->type>" . $contents . "</$this->type>"; } public function toJsonArray(): array { @@ -74,15 +74,16 @@ class ScalarParam extends MessageParam { ]; } - public static function newFromJsonArray( array $json ) { + public static function newFromJsonArray( array $json ): ScalarParam { // WARNING: When changing how this class is serialized, follow the instructions // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! if ( count( $json ) !== 1 ) { throw new InvalidArgumentException( 'Invalid format' ); } - // Use a dummy loop to get the first (and only) key/value pair in the array. - foreach ( $json as $type => $value ) { - return new self( $type, $value ); - } + + $type = key( $json ); + $value = current( $json ); + + return new self( $type, $value ); } } |