Thiemo Mättig (WMDE) has uploaded a new change for review. https://gerrit.wikimedia.org/r/129168
Change subject: Refactor type checks in EntityId formatters ...................................................................... Refactor type checks in EntityId formatters I just realized it can very, very easily split this (hopefully) very, very simple to understand chunk from my bigger patch I9bbbe8055d68b274fa6141eecff4a8d9414f89d9. Please help me move forward and merge this quick. :-) Change-Id: I242ba0b0c0b6924cb876a07b82f8a9a4976f65b0 --- M lib/includes/formatters/EntityIdFormatter.php M lib/includes/formatters/EntityIdHtmlLinkFormatter.php M lib/includes/formatters/EntityIdLabelFormatter.php M lib/includes/formatters/EntityIdLinkFormatter.php M lib/includes/formatters/EntityIdTitleFormatter.php 5 files changed, 41 insertions(+), 81 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/68/129168/1 diff --git a/lib/includes/formatters/EntityIdFormatter.php b/lib/includes/formatters/EntityIdFormatter.php index 0eaac06..3571ee6 100644 --- a/lib/includes/formatters/EntityIdFormatter.php +++ b/lib/includes/formatters/EntityIdFormatter.php @@ -3,7 +3,6 @@ namespace Wikibase\Lib; use InvalidArgumentException; -use OutOfBoundsException; use ValueFormatters\ValueFormatterBase; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; @@ -13,6 +12,7 @@ * * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroended...@gmail.com > + * @author Thiemo Mättig */ class EntityIdFormatter extends ValueFormatterBase { @@ -21,11 +21,10 @@ * * @since 0.4 * - * @param EntityId|EntityIdValue $value The ID to format + * @param EntityId|EntityIdValue $value * - * @return string * @throws InvalidArgumentException - * @throws OutOfBoundsException + * @return string */ public function format( $value ) { if ( $value instanceof EntityIdValue ) { @@ -33,10 +32,21 @@ } if ( !( $value instanceof EntityId ) ) { - throw new InvalidArgumentException( 'Data value type mismatch. Expected an EntityId.' ); + throw new InvalidArgumentException( 'Data value type mismatch. Expected an EntityId or EntityIdValue.' ); } - return $value->getSerialization(); + return $this->formatEntityId( $value ); + } + + /** + * @since 0.5 + * + * @param EntityId $entityId + * + * @return string + */ + protected function formatEntityId( EntityId $entityId ) { + return $entityId->getSerialization(); } } diff --git a/lib/includes/formatters/EntityIdHtmlLinkFormatter.php b/lib/includes/formatters/EntityIdHtmlLinkFormatter.php index 6094013..e5aa1fb 100644 --- a/lib/includes/formatters/EntityIdHtmlLinkFormatter.php +++ b/lib/includes/formatters/EntityIdHtmlLinkFormatter.php @@ -3,7 +3,6 @@ namespace Wikibase\Lib; use Html; -use InvalidArgumentException; use OutOfBoundsException; use Title; use ValueFormatters\FormatterOptions; @@ -44,32 +43,28 @@ } /** - * Format an EntityId data value + * @see EntityIdFormatter::formatEntityId * - * @param EntityId|EntityIdValue $value The value to format + * @param EntityId $entityId * * @return string - * - * @throws InvalidArgumentException */ - public function format( $value ) { - $value = $this->unwrapEntityId( $value ); - + protected function formatEntityId( EntityId $entityId ) { if ( isset( $this->entityTitleLookup ) ) { - $title = $this->entityTitleLookup->getTitleForId( $value ); + $title = $this->entityTitleLookup->getTitleForId( $entityId ); } else { - $title = Title::newFromText( $value->getPrefixedId() ); + $title = Title::newFromText( $entityId->getPrefixedId() ); } $attributes = array( 'title' => $title->getPrefixedText(), 'href' => $title->getLocalURL() ); - $label = $value->getPrefixedId(); + $label = $entityId->getPrefixedId(); if ( $this->getOption( self::OPT_LOOKUP_LABEL ) ) { try { - $itemLabel = $this->lookupItemLabel( $value ); + $itemLabel = $this->lookupEntityLabel( $entityId ); if ( is_string( $itemLabel ) ) { $label = $itemLabel; } diff --git a/lib/includes/formatters/EntityIdLabelFormatter.php b/lib/includes/formatters/EntityIdLabelFormatter.php index 83fc3a7..7845209 100644 --- a/lib/includes/formatters/EntityIdLabelFormatter.php +++ b/lib/includes/formatters/EntityIdLabelFormatter.php @@ -4,7 +4,6 @@ use InvalidArgumentException; use OutOfBoundsException; -use RuntimeException; use ValueFormatters\FormatterOptions; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; @@ -78,25 +77,19 @@ } /** - * Format an EntityId data value + * @see EntityIdFormatter::formatEntityId * - * @since 0.4 + * @param EntityId $entityId * - * @param EntityId|EntityIdValue $value The value to format - * + * @throws FormattingException * @return string - * - * @throws RuntimeException - * @throws InvalidArgumentException */ - public function format( $value ) { - $value = $this->unwrapEntityId( $value ); - + protected function formatEntityId( EntityId $entityId ) { $label = null; if ( $this->getOption( self::OPT_LOOKUP_LABEL ) ) { try { - $label = $this->lookupItemLabel( $value ); + $label = $this->lookupEntityLabel( $entityId ); } catch ( OutOfBoundsException $ex ) { /* Use fallbacks below */ } @@ -104,41 +97,19 @@ if ( !is_string( $label ) ) { switch ( $this->getOption( self::OPT_LABEL_FALLBACK ) ) { + case self::FALLBACK_PREFIXED_ID: + $label = $entityId->getPrefixedId(); + break; case self::FALLBACK_EMPTY_STRING: $label = ''; break; - case self::FALLBACK_PREFIXED_ID: - $label = $value->getPrefixedId(); - break; default: - throw new FormattingException( 'No label found for ' . $value ); + throw new FormattingException( 'No label found for ' . $entityId ); } } assert( is_string( $label ) ); return $label; - } - - /** - * Unwrap an EntityId value which might be wrapped in an EntityIdValue - * - * @param EntityId|EntityIdValue $value The value to format - * - * @return EntityId - * - * @throws InvalidArgumentException - */ - - protected function unwrapEntityId( $value ) { - if ( $value instanceof EntityIdValue ) { - $value = $value->getEntityId(); - } - - if ( !( $value instanceof EntityId ) ) { - throw new InvalidArgumentException( 'Data value type mismatch. Expected an EntityId or EntityIdValue.' ); - } - - return $value; } /** @@ -151,7 +122,7 @@ * @throws OutOfBoundsException If an entity with that ID does not exist. * @return string|bool False if no label was found in the language or language fallback chain. */ - protected function lookupItemLabel( EntityId $entityId ) { + protected function lookupEntityLabel( EntityId $entityId ) { $entity = $this->entityLookup->getEntity( $entityId ); if ( $entity === null ) { diff --git a/lib/includes/formatters/EntityIdLinkFormatter.php b/lib/includes/formatters/EntityIdLinkFormatter.php index 45a6edf..c4bf39d 100644 --- a/lib/includes/formatters/EntityIdLinkFormatter.php +++ b/lib/includes/formatters/EntityIdLinkFormatter.php @@ -1,7 +1,6 @@ <?php namespace Wikibase\Lib; -use InvalidArgumentException; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; @@ -16,19 +15,16 @@ class EntityIdLinkFormatter extends EntityIdTitleFormatter { /** - * Format an EntityId data value + * @see EntityIdFormatter::formatEntityId * - * @param EntityId|EntityIdValue $value The value to format + * @param EntityId $entityId * * @return string - * - * @throws InvalidArgumentException */ - public function format( $value ) { - $title = parent::format( $value ); + protected function formatEntityId( EntityId $entityId ) { + $title = parent::formatEntityId( $entityId ); return "[[$title]]"; } } - diff --git a/lib/includes/formatters/EntityIdTitleFormatter.php b/lib/includes/formatters/EntityIdTitleFormatter.php index fa3fe93..f1e47a7 100644 --- a/lib/includes/formatters/EntityIdTitleFormatter.php +++ b/lib/includes/formatters/EntityIdTitleFormatter.php @@ -2,7 +2,6 @@ namespace Wikibase\Lib; -use InvalidArgumentException; use ValueFormatters\FormatterOptions; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; @@ -34,26 +33,15 @@ } /** - * Format an EntityId data value + * @see EntityIdFormatter::formatEntityId * - * @param EntityId|EntityIdValue $value The value to format + * @param EntityId $entityId * * @return string - * - * @throws InvalidArgumentException */ - public function format( $value ) { - if ( $value instanceof EntityIdValue ) { - $value = $value->getEntityId(); - } - - if ( !( $value instanceof EntityId ) ) { - throw new InvalidArgumentException( 'Data value type mismatch. Expected an EntityId or EntityIdValue.' ); - } - - $title = $this->titleLookup->getTitleForId( $value ); + protected function formatEntityId( EntityId $entityId ) { + $title = $this->titleLookup->getTitleForId( $entityId ); return $title->getFullText(); } } - -- To view, visit https://gerrit.wikimedia.org/r/129168 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I242ba0b0c0b6924cb876a07b82f8a9a4976f65b0 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits