[MediaWiki-commits] [Gerrit] Use Assert in ResultBuilder - change (mediawiki...Wikibase)
jenkins-bot has submitted this change and it was merged. Change subject: Use Assert in ResultBuilder .. Use Assert in ResultBuilder Change-Id: Id78c9077a7d02c5fe0c7fd4f8d63e7c9782451d9 --- M repo/includes/api/ResultBuilder.php M repo/tests/phpunit/includes/api/ResultBuilderTest.php 2 files changed, 26 insertions(+), 84 deletions(-) Approvals: Jonas Kress (WMDE): Looks good to me, approved jenkins-bot: Verified diff --git a/repo/includes/api/ResultBuilder.php b/repo/includes/api/ResultBuilder.php index 0b8a611..b81fdaa 100644 --- a/repo/includes/api/ResultBuilder.php +++ b/repo/includes/api/ResultBuilder.php @@ -3,7 +3,6 @@ namespace Wikibase\Repo\Api; use ApiResult; -use InvalidArgumentException; use Revision; use SiteStore; use Status; @@ -21,6 +20,7 @@ use Wikibase\LanguageFallbackChain; use Wikibase\Lib\Serializers\SerializationOptions; use Wikibase\Lib\Store\EntityTitleLookup; +use Wikimedia\Assert\Assert; /** * Builder for Api Results @@ -85,21 +85,15 @@ * @param SiteStore $siteStore * @param PropertyDataTypeLookup $dataTypeLookup * @param bool $isRawMode when special elements such as '_element' are needed by the formatter. -* -* @throws InvalidArgumentException */ public function __construct( - $result, + ApiResult $result, EntityTitleLookup $entityTitleLookup, SerializerFactory $serializerFactory, SiteStore $siteStore, PropertyDataTypeLookup $dataTypeLookup, $isRawMode ) { - if ( !$result instanceof ApiResult ) { - throw new InvalidArgumentException( 'Result builder must be constructed with an ApiResult' ); - } - $this->result = $result; $this->entityTitleLookup = $entityTitleLookup; $this->serializerFactory = $serializerFactory; @@ -114,17 +108,15 @@ * @since 0.5 * * @param $success bool|int|null -* -* @throws InvalidArgumentException */ public function markSuccess( $success = true ) { $value = (int)$success; - if ( $value !== 1 && $value !== 0 ) { - throw new InvalidArgumentException( - '$success must evaluate to either 1 or 0 when casted to integer' - ); - } + Assert::parameter( + $value == 1 || $value == 0, + '$success', + '$success must evaluate to either 1 or 0 when casted to integer' + ); $this->result->addValue( null, 'success', $value ); } @@ -146,13 +138,11 @@ * @param $name string * @param $values array * @param string $tag tag name to use for elements of $values -* -* @throws InvalidArgumentException */ public function setList( $path, $name, array $values, $tag ) { $this->checkPathType( $path ); - $this->checkNameIsString( $name ); - $this->checkTagIsString( $tag ); + Assert::parameterType( 'string', $name, '$name' ); + Assert::parameterType( 'string', $tag, '$tag' ); if ( $this->isRawMode ) { // Unset first, so we don't make the tag name an actual value. @@ -180,12 +170,10 @@ * @param $path array|string|null * @param $name string * @param $value mixed -* -* @throws InvalidArgumentException */ public function setValue( $path, $name, $value ) { $this->checkPathType( $path ); - $this->checkNameIsString( $name ); + Assert::parameterType( 'string', $name, '$name' ); $this->checkValueIsNotList( $value ); $this->result->addValue( $path, $name, $value ); @@ -210,14 +198,11 @@ * May be ignored even if given, based on $this->result->getIsRawMode(). * @param $value mixed * @param string $tag tag name to use for $value in indexed mode -* -* @throws InvalidArgumentException */ public function appendValue( $path, $key, $value, $tag ) { $this->checkPathType( $path ); $this->checkKeyType( $key ); - $this->checkTagIsString( $tag ); - + Assert::parameterType( 'string', $tag, '$tag' ); $this->checkValueIsNotList( $value ); if ( $this->isRawMode ) { @@ -230,61 +215,35 @@ /** * @param array|string|null $path -* -* @throws InvalidArgumentException */ private function checkPathType( $path ) { - if ( is_string( $path ) ) { -
[MediaWiki-commits] [Gerrit] Use Assert in ResultBuilder - change (mediawiki...Wikibase)
Addshore has uploaded a new change for review. https://gerrit.wikimedia.org/r/227250 Change subject: Use Assert in ResultBuilder .. Use Assert in ResultBuilder Change-Id: Id78c9077a7d02c5fe0c7fd4f8d63e7c9782451d9 --- M repo/includes/api/ResultBuilder.php M repo/tests/phpunit/includes/api/ResultBuilderTest.php 2 files changed, 26 insertions(+), 84 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/50/227250/1 diff --git a/repo/includes/api/ResultBuilder.php b/repo/includes/api/ResultBuilder.php index 407214b..dbeba62 100644 --- a/repo/includes/api/ResultBuilder.php +++ b/repo/includes/api/ResultBuilder.php @@ -3,7 +3,6 @@ namespace Wikibase\Repo\Api; use ApiResult; -use InvalidArgumentException; use Revision; use SiteStore; use Status; @@ -21,6 +20,7 @@ use Wikibase\LanguageFallbackChain; use Wikibase\Lib\Serializers\SerializationOptions; use Wikibase\Lib\Store\EntityTitleLookup; +use Wikimedia\Assert\Assert; /** * Builder for Api Results @@ -85,21 +85,15 @@ * @param SiteStore $siteStore * @param PropertyDataTypeLookup $dataTypeLookup * @param bool $isRawMode when special elements such as '_element' are needed by the formatter. -* -* @throws InvalidArgumentException */ public function __construct( - $result, + ApiResult $result, EntityTitleLookup $entityTitleLookup, SerializerFactory $serializerFactory, SiteStore $siteStore, PropertyDataTypeLookup $dataTypeLookup, $isRawMode ) { - if ( !$result instanceof ApiResult ) { - throw new InvalidArgumentException( 'Result builder must be constructed with an ApiResult' ); - } - $this->result = $result; $this->entityTitleLookup = $entityTitleLookup; $this->serializerFactory = $serializerFactory; @@ -114,17 +108,15 @@ * @since 0.5 * * @param $success bool|int|null -* -* @throws InvalidArgumentException */ public function markSuccess( $success = true ) { $value = (int)$success; - if ( $value !== 1 && $value !== 0 ) { - throw new InvalidArgumentException( - '$success must evaluate to either 1 or 0 when casted to integer' - ); - } + Assert::parameter( + $value == 1 || $value == 0, + '$success', + '$success must evaluate to either 1 or 0 when casted to integer' + ); $this->result->addValue( null, 'success', $value ); } @@ -146,13 +138,11 @@ * @param $name string * @param $values array * @param string $tag tag name to use for elements of $values -* -* @throws InvalidArgumentException */ public function setList( $path, $name, array $values, $tag ) { $this->checkPathType( $path ); - $this->checkNameIsString( $name ); - $this->checkTagIsString( $tag ); + Assert::parameterType( 'string', $name, '$name' ); + Assert::parameterType( 'string', $tag, '$tag' ); if ( $this->isRawMode ) { // Unset first, so we don't make the tag name an actual value. @@ -180,12 +170,10 @@ * @param $path array|string|null * @param $name string * @param $value mixed -* -* @throws InvalidArgumentException */ public function setValue( $path, $name, $value ) { $this->checkPathType( $path ); - $this->checkNameIsString( $name ); + Assert::parameterType( 'string', $name, '$name' ); $this->checkValueIsNotList( $value ); $this->result->addValue( $path, $name, $value ); @@ -210,14 +198,11 @@ * May be ignored even if given, based on $this->result->getIsRawMode(). * @param $value mixed * @param string $tag tag name to use for $value in indexed mode -* -* @throws InvalidArgumentException */ public function appendValue( $path, $key, $value, $tag ) { $this->checkPathType( $path ); $this->checkKeyType( $key ); - $this->checkTagIsString( $tag ); - + Assert::parameterType( 'string', $tag, '$tag' ); $this->checkValueIsNotList( $value ); if ( $this->isRawMode ) { @@ -230,61 +215,35 @@ /** * @param array|string|null $path -* -* @throws InvalidArgumentException */ private function checkPathType( $path ) { -