Addshore has submitted this change and it was merged.
Change subject: Reduce code duplication in ResultBuilder
......................................................................
Reduce code duplication in ResultBuilder
Add methods to check type etc. of method inputs.
Some checks and code had been duplicated in multiple places
in ResultBuilder.
Change-Id: I890a2d1484e8335c438564354363074a302b99a3
---
M repo/includes/api/ResultBuilder.php
1 file changed, 69 insertions(+), 48 deletions(-)
Approvals:
WikidataJenkins: Verified
Addshore: Looks good to me, approved
diff --git a/repo/includes/api/ResultBuilder.php
b/repo/includes/api/ResultBuilder.php
index a6d009a..70dcd85 100644
--- a/repo/includes/api/ResultBuilder.php
+++ b/repo/includes/api/ResultBuilder.php
@@ -128,21 +128,9 @@
* @throws InvalidArgumentException
*/
public function setList( $path, $name, array $values, $tag ){
- if ( is_string( $path ) ) {
- $path = array( $path );
- }
-
- if ( !is_array( $path ) && $path !== null ) {
- throw new InvalidArgumentException( '$path must be an
array (or null)' );
- }
-
- if ( !is_string( $name ) ) {
- throw new InvalidArgumentException( '$name must be a
string' );
- }
-
- if ( !is_string( $tag ) ) {
- throw new InvalidArgumentException( '$tag must be a
string' );
- }
+ $this->checkPathType( $path );
+ $this->checkNameIsString( $name );
+ $this->checkTagIsString( $tag );
if ( $this->options->shouldIndexTags() ) {
$values = array_values( $values );
@@ -171,21 +159,9 @@
* @throws InvalidArgumentException
*/
public function setValue( $path, $name, $value ){
- if ( is_string( $path ) ) {
- $path = array( $path );
- }
-
- if ( !is_array( $path ) && $path !== null ) {
- throw new InvalidArgumentException( '$path must be an
array (or null)' );
- }
-
- if ( !is_string( $name ) ) {
- throw new InvalidArgumentException( '$name must be a
string' );
- }
-
- if ( is_array( $value ) && isset( $value[0] ) ) {
- throw new InvalidArgumentException( '$value must not be
a list' );
- }
+ $this->checkPathType( $path );
+ $this->checkNameIsString( $name );
+ $this->checkValueIsNotList( $value );
$this->getResult()->addValue( $path, $name, $value );
}
@@ -211,25 +187,11 @@
* @throws InvalidArgumentException
*/
public function appendValue( $path, $key, $value, $tag ){
- if ( is_string( $path ) ) {
- $path = array( $path );
- }
+ $this->checkPathType( $path );
+ $this->checkKeyType( $key );
+ $this->checkTagIsString( $tag );
- if ( !is_array( $path ) && $path !== null ) {
- throw new InvalidArgumentException( '$path must be an
array (or null)' );
- }
-
- if ( $key !== null && !is_string( $key ) && !is_int( $key ) ) {
- throw new InvalidArgumentException( '$key must be a
string, int, or null' );
- }
-
- if ( !is_string( $tag ) ) {
- throw new InvalidArgumentException( '$tag must be a
string' );
- }
-
- if ( is_array( $value ) && isset( $value[0] ) ) {
- throw new InvalidArgumentException( '$value must not be
a list' );
- }
+ $this->checkValueIsNotList( $value );
if ( $this->options->shouldIndexTags() ) {
$key = null;
@@ -243,6 +205,65 @@
}
/**
+ * @param array|string|null $path
+ *
+ * @throws InvalidArgumentException
+ */
+ private function checkPathType( $path ) {
+ if ( is_string( $path ) ) {
+ $path = array( $path );
+ }
+
+ if ( !is_array( $path ) && $path !== null ) {
+ throw new InvalidArgumentException( '$path must be an
array (or null)' );
+ }
+ }
+
+ /**
+ * @param string $name
+ *
+ * @throws InvalidArgumentException
+ */
+ private function checkNameIsString( $name ) {
+ if ( !is_string( $name ) ) {
+ throw new InvalidArgumentException( '$name must be a
string' );
+ }
+ }
+
+ /**
+ * @param $key int|string|null the key to use when appending, or null
for automatic.
+ *
+ * @throws InvalidArgumentException
+ */
+ private function checkKeyType( $key ) {
+ if ( $key !== null && !is_string( $key ) && !is_int( $key ) ) {
+ throw new InvalidArgumentException( '$key must be a
string, int, or null' );
+ }
+ }
+
+ /**
+ * @param string $tag tag name to use for elements of $values
+ *
+ * @throws InvalidArgumentException
+ */
+ private function checkTagIsString( $tag ) {
+ if ( !is_string( $tag ) ) {
+ throw new InvalidArgumentException( '$tag must be a
string' );
+ }
+ }
+
+ /**
+ * @param mixed $value
+ *
+ * @throws InvalidArgumentException
+ */
+ private function checkValueIsNotList( $value ) {
+ if ( is_array( $value ) && isset( $value[0] ) ) {
+ throw new InvalidArgumentException( '$value must not be
a list' );
+ }
+ }
+
+ /**
* Get serialized entity for the EntityRevision and add it to the result
*
* @param EntityRevision $entityRevision
--
To view, visit https://gerrit.wikimedia.org/r/119718
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I890a2d1484e8335c438564354363074a302b99a3
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: WikidataJenkins <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits