Addshore has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/90878


Change subject: Refactor lib/SiteMatrixParser.php
......................................................................

Refactor lib/SiteMatrixParser.php

Small Functions!

Change-Id: I761952bd396d1a0ca40df4dc85a7749310f3705f
---
M lib/includes/sites/SiteMatrixParser.php
1 file changed, 53 insertions(+), 35 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/78/90878/1

diff --git a/lib/includes/sites/SiteMatrixParser.php 
b/lib/includes/sites/SiteMatrixParser.php
index ffdc559..0667419 100644
--- a/lib/includes/sites/SiteMatrixParser.php
+++ b/lib/includes/sites/SiteMatrixParser.php
@@ -4,7 +4,6 @@
  * Translates api sitematrix results json into an array of Site objects
  *
  * @licence GNU GPL v2+
- *
  * @author Katie Filbert < [email protected] >
  */
 class SiteMatrixParser {
@@ -45,61 +44,81 @@
        /**
         * @param string $json
         *
+        * @throws InvalidArgumentException
         * @return Site[]
         */
        public function sitesFromJson( $json ) {
-               $specials = null;
+               $data = $this->decodeJson( $json );
+               $specialSites = $this->getSpecialSites( $data );
 
-               $data = json_decode( $json, true );
-
-               if ( !is_array( $data ) || !array_key_exists( 'sitematrix', 
$data ) ) {
-                       throw new InvalidArgumentException( 'Cannot decode site 
matrix data.' );
-               }
-
-               if ( array_key_exists( 'specials', $data['sitematrix'] ) ) {
-                       $specials = $data['sitematrix']['specials'];
-                       unset( $data['sitematrix']['specials'] );
-               }
-
-               if ( array_key_exists( 'count', $data['sitematrix'] ) ) {
-                       unset( $data['sitematrix']['count'] );
-               }
-
-               $groups = $data['sitematrix'];
+               $data = $this->removeSpecialsFromData( $data );
+               $data = $this->removeCountFromData( $data );
 
                $sites = array();
-
-               foreach( $groups as $groupData ) {
+               foreach( $data['sitematrix'] as $groupData ) {
                        $sites = array_merge(
                                $sites,
                                $this->getSitesFromLangGroup( $groupData )
                        );
                }
 
-               $sites = array_merge(
-                       $sites,
-                       $this->getSpecialSites( $specials )
-               );
-
-               return $sites;
+               return array_merge( $sites, $specialSites );
        }
 
        /**
-        * @param array $specialSites
+        * @param string $json
+        * @return array
+        * @throws InvalidArgumentException
+        */
+       protected function decodeJson( $json ){
+               $data = json_decode( $json, true );
+
+               if ( !is_array( $data ) || !array_key_exists( 'sitematrix', 
$data ) ) {
+                       throw new InvalidArgumentException( 'Cannot decode site 
matrix data.' );
+               }
+               return $data;
+       }
+
+       /**
+        * @param array $data
+        * @return array
+        */
+       protected function removeCountFromData( $data ){
+               if ( array_key_exists( 'count', $data['sitematrix'] ) ) {
+                       unset( $data['sitematrix']['count'] );
+               }
+               return $data;
+       }
+
+       /**
+        * @param array $data
+        * @return array
+        */
+       protected function removeSpecialsFromData( $data ){
+               if ( array_key_exists( 'specials', $data['sitematrix'] ) ) {
+                       unset( $data['sitematrix']['specials'] );
+               }
+               return $data;
+       }
+
+       /**
+        * @param array $data
         *
         * @return Site[]
         */
-       protected function getSpecialSites( array $specialSites ) {
+       protected function getSpecialSites( array $data ) {
                $sites = array();
 
-               foreach( $specialSites as $specialSite ) {
-                       $site = $this->getSiteFromSiteData( $specialSite );
-                       $siteId = $site->getGlobalId();
+               if ( array_key_exists( 'specials', $data['sitematrix'] ) ) {
+                       foreach( $data['sitematrix'] as $specialSite ) {
+                               $site = $this->getSiteFromSiteData( 
$specialSite );
+                               $siteId = $site->getGlobalId();
 
-                       // todo: get this from $wgConf
-                       $site->setLanguageCode( 'en' );
+                               // todo: get this from $wgConf
+                               $site->setLanguageCode( 'en' );
 
-                       $sites[$siteId] = $site;
+                               $sites[$siteId] = $site;
+                       }
                }
 
                return $sites;
@@ -132,7 +151,6 @@
 
        /**
         * @param array $siteData
-        * @param string $langCode
         *
         * @return Site
         */

-- 
To view, visit https://gerrit.wikimedia.org/r/90878
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I761952bd396d1a0ca40df4dc85a7749310f3705f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to