From c5be1214420a58be67e42950dd8c9e1e8315a934 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 10 Feb 2016 10:40:15 -0500 Subject: [PATCH] ApiMessage: Use a trait to avoid code duplication Change-Id: I19eb63bdc4c4cbd137f4d2101d37c81ce91aa6b3 --- includes/api/ApiMessage.php | 120 +++++++----------- tests/phpunit/includes/api/ApiMessageTest.php | 2 + 2 files changed, 48 insertions(+), 74 deletions(-) diff --git a/includes/api/ApiMessage.php b/includes/api/ApiMessage.php index 5c3434f95c..f24a7cc470 100644 --- a/includes/api/ApiMessage.php +++ b/includes/api/ApiMessage.php @@ -63,15 +63,57 @@ interface IApiMessage extends MessageSpecifier { public function setApiData( array $data ); } +/** + * Trait to implement the IApiMessage interface for Message subclasses + * @since 1.27 + * @ingroup API + */ +trait ApiMessageTrait { + protected $apiCode = null; + protected $apiData = array(); + + public function getApiCode() { + return $this->apiCode === null ? $this->getKey() : $this->apiCode; + } + + public function setApiCode( $code, array $data = null ) { + $this->apiCode = $code; + if ( $data !== null ) { + $this->setApiData( $data ); + } + } + + public function getApiData() { + return $this->apiData; + } + + public function setApiData( array $data ) { + $this->apiData = $data; + } + + public function serialize() { + return serialize( array( + 'parent' => parent::serialize(), + 'apiCode' => $this->apiCode, + 'apiData' => $this->apiData, + ) ); + } + + public function unserialize( $serialized ) { + $data = unserialize( $serialized ); + parent::unserialize( $data['parent'] ); + $this->apiCode = $data['apiCode']; + $this->apiData = $data['apiData']; + } +} + /** * Extension of Message implementing IApiMessage * @since 1.25 * @ingroup API - * @todo: Would be nice to use a Trait here to avoid code duplication */ class ApiMessage extends Message implements IApiMessage { - protected $apiCode = null; - protected $apiData = array(); + use ApiMessageTrait; /** * Create an IApiMessage for the message @@ -119,51 +161,15 @@ class ApiMessage extends Message implements IApiMessage { $this->apiCode = $code; $this->apiData = (array)$data; } - - public function getApiCode() { - return $this->apiCode === null ? $this->getKey() : $this->apiCode; - } - - public function setApiCode( $code, array $data = null ) { - $this->apiCode = $code; - if ( $data !== null ) { - $this->setApiData( $data ); - } - } - - public function getApiData() { - return $this->apiData; - } - - public function setApiData( array $data ) { - $this->apiData = $data; - } - - public function serialize() { - return serialize( array( - 'parent' => parent::serialize(), - 'apiCode' => $this->apiCode, - 'apiData' => $this->apiData, - ) ); - } - - public function unserialize( $serialized ) { - $data = unserialize( $serialized ); - parent::unserialize( $data['parent'] ); - $this->apiCode = $data['apiCode']; - $this->apiData = $data['apiData']; - } } /** * Extension of RawMessage implementing IApiMessage * @since 1.25 * @ingroup API - * @todo: Would be nice to use a Trait here to avoid code duplication */ class ApiRawMessage extends RawMessage implements IApiMessage { - protected $apiCode = null; - protected $apiData = array(); + use ApiMessageTrait; /** * @param RawMessage|string|array $msg @@ -189,38 +195,4 @@ class ApiRawMessage extends RawMessage implements IApiMessage { $this->apiCode = $code; $this->apiData = (array)$data; } - - public function getApiCode() { - return $this->apiCode === null ? $this->getKey() : $this->apiCode; - } - - public function setApiCode( $code, array $data = null ) { - $this->apiCode = $code; - if ( $data !== null ) { - $this->setApiData( $data ); - } - } - - public function getApiData() { - return $this->apiData; - } - - public function setApiData( array $data ) { - $this->apiData = $data; - } - - public function serialize() { - return serialize( array( - 'parent' => parent::serialize(), - 'apiCode' => $this->apiCode, - 'apiData' => $this->apiData, - ) ); - } - - public function unserialize( $serialized ) { - $data = unserialize( $serialized ); - parent::unserialize( $data['parent'] ); - $this->apiCode = $data['apiCode']; - $this->apiData = $data['apiData']; - } } diff --git a/tests/phpunit/includes/api/ApiMessageTest.php b/tests/phpunit/includes/api/ApiMessageTest.php index 08a984eb25..56bbd064c5 100644 --- a/tests/phpunit/includes/api/ApiMessageTest.php +++ b/tests/phpunit/includes/api/ApiMessageTest.php @@ -25,6 +25,7 @@ class ApiMessageTest extends MediaWikiTestCase { /** * @covers ApiMessage + * @covers ApiMessageTrait */ public function testApiMessage() { $msg = new Message( array( 'foo', 'bar' ), array( 'baz' ) ); @@ -63,6 +64,7 @@ class ApiMessageTest extends MediaWikiTestCase { /** * @covers ApiRawMessage + * @covers ApiMessageTrait */ public function testApiRawMessage() { $msg = new RawMessage( 'foo', array( 'baz' ) ); -- 2.20.1