jenkins-bot has submitted this change and it was merged. Change subject: Shorten out in LanguageFallbackChain related code ......................................................................
Shorten out in LanguageFallbackChain related code I found this in my stashes. The idea is to shorten out as early as possible if there is no data. This patch also adds some type hints and makes type docs more specific. Change-Id: I38f34dec7fc2925e7256111b4ec54495ea6fd20a --- M lib/includes/LanguageFallbackChain.php M lib/includes/serializers/DescriptionSerializer.php M lib/includes/serializers/MultilingualSerializer.php M lib/includes/serializers/SerializationOptions.php M lib/tests/phpunit/LanguageFallbackChainTest.php 5 files changed, 27 insertions(+), 21 deletions(-) Approvals: Hoo man: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/includes/LanguageFallbackChain.php b/lib/includes/LanguageFallbackChain.php index 1736535..f6d2090 100644 --- a/lib/includes/LanguageFallbackChain.php +++ b/lib/includes/LanguageFallbackChain.php @@ -68,6 +68,10 @@ * ), or null when no "acceptable" data can be found. */ public function extractPreferredValue( array $data ) { + if ( empty( $data ) ) { + return null; + } + foreach ( $this->chain as $languageWithConversion ) { $languageCode = $languageWithConversion->getFetchLanguageCode(); diff --git a/lib/includes/serializers/DescriptionSerializer.php b/lib/includes/serializers/DescriptionSerializer.php index 73ad9c1..fb2e6e2 100644 --- a/lib/includes/serializers/DescriptionSerializer.php +++ b/lib/includes/serializers/DescriptionSerializer.php @@ -79,7 +79,7 @@ * @return array * @throws InvalidArgumentException */ - final public function getSerializedMultilingualValues( $descriptions ) { + final public function getSerializedMultilingualValues( array $descriptions ) { $descriptions = $this->multilingualSerializer->filterPreferredMultilingualValues( $descriptions ); return $this->getSerialized( $descriptions ); } diff --git a/lib/includes/serializers/MultilingualSerializer.php b/lib/includes/serializers/MultilingualSerializer.php index 6a885bc..1152d92 100644 --- a/lib/includes/serializers/MultilingualSerializer.php +++ b/lib/includes/serializers/MultilingualSerializer.php @@ -2,8 +2,6 @@ namespace Wikibase\Lib\Serializers; -use Wikibase\LanguageFallbackChain; - /** * Multilingual serializer, for serializer of labels and descriptions. * @@ -36,9 +34,9 @@ * * @since 0.4 * - * @param array $data + * @param string[]|array[] $data * - * @return array + * @return array[] */ public function serializeMultilingualValues( array $data ) { $values = array(); @@ -78,23 +76,22 @@ * * @param array $allData * - * @return array + * @return array[] */ public function filterPreferredMultilingualValues( array $allData ) { - $values = array(); - $languageFallbackChains = $this->options->getLanguageFallbackChains(); - if ( $languageFallbackChains ) { - /** @var LanguageFallbackChain[] $languageFallbackChains */ - foreach ( $languageFallbackChains as $languageCode => $languageFallbackChain ) { - $data = $languageFallbackChain->extractPreferredValue( $allData ); - if ( $data !== null ) { - $values[$languageCode] = $data; - } + if ( $languageFallbackChains === null || empty( $allData ) ) { + return $allData; + } + + $values = array(); + + foreach ( $languageFallbackChains as $languageCode => $languageFallbackChain ) { + $data = $languageFallbackChain->extractPreferredValue( $allData ); + if ( $data !== null ) { + $values[$languageCode] = $data; } - } else { - $values = $allData; } return $values; diff --git a/lib/includes/serializers/SerializationOptions.php b/lib/includes/serializers/SerializationOptions.php index 2fbbad6..56feb36 100644 --- a/lib/includes/serializers/SerializationOptions.php +++ b/lib/includes/serializers/SerializationOptions.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use RuntimeException; +use Wikibase\LanguageFallbackChain; use Wikibase\LanguageFallbackChainFactory; /** @@ -350,8 +351,9 @@ * * @since 0.2 * - * @param array|null $languages array of strings (back compat, as language codes) - * or LanguageFallbackChain objects (requested language codes as keys, to identify chains) + * @param LanguageFallbackChain[]|string[]|null $languages An array of LanguageFallbackChain + * objects (requested language codes as keys, to identify chains) or an array of language codes + * (for backwards compatibility). */ public function setLanguages( array $languages = null ) { if ( $languages === null ) { @@ -381,7 +383,7 @@ * * @since 0.2 * - * @return array|null + * @return string[]|null */ public function getLanguages() { $languages = $this->getLanguageFallbackChains(); @@ -398,7 +400,7 @@ * * @since 0.4 * - * @return array|null + * @return LanguageFallbackChain[]|null */ public function getLanguageFallbackChains() { return $this->getOption( self::OPT_LANGUAGES ); @@ -432,4 +434,5 @@ public function setLanguageFallbackChainFactory( LanguageFallbackChainFactory $factory ) { $this->setOption( self::OPT_LANGUAGE_FALLBACK_CHAIN_FACTORY, $factory ); } + } diff --git a/lib/tests/phpunit/LanguageFallbackChainTest.php b/lib/tests/phpunit/LanguageFallbackChainTest.php index a8cea58..eced4e8 100644 --- a/lib/tests/phpunit/LanguageFallbackChainTest.php +++ b/lib/tests/phpunit/LanguageFallbackChainTest.php @@ -172,6 +172,7 @@ 'language' => 'en', 'source' => null, ) ), + array( 'fr', LanguageFallbackChainFactory::FALLBACK_SELF, array( 'kk' => 'baz', ), array( @@ -190,6 +191,7 @@ array( 'sr', LanguageFallbackChainFactory::FALLBACK_SELF, array( ':' => 'qux', ), null ), + array( 'en', LanguageFallbackChainFactory::FALLBACK_ALL, array(), null ), array( 'ar', LanguageFallbackChainFactory::FALLBACK_SELF, array(), null ), array( 'de', LanguageFallbackChainFactory::FALLBACK_SELF, $entityInfoBuilderArray, array( -- To view, visit https://gerrit.wikimedia.org/r/202353 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I38f34dec7fc2925e7256111b4ec54495ea6fd20a Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits