[MediaWiki-commits] [Gerrit] Refactoring of ChangeOpSiteLink - change (mediawiki...Wikibase)
jenkins-bot has submitted this change and it was merged. Change subject: Refactoring of ChangeOpSiteLink .. Refactoring of ChangeOpSiteLink Change-Id: I94006953ede551aa9b301fa4f02a2afe8383b223 --- M repo/includes/ChangeOp/ChangeOpSiteLink.php M repo/tests/phpunit/includes/ChangeOp/ChangeOpSiteLinkTest.php 2 files changed, 59 insertions(+), 107 deletions(-) Approvals: Hoo man: Looks good to me, approved jenkins-bot: Verified diff --git a/repo/includes/ChangeOp/ChangeOpSiteLink.php b/repo/includes/ChangeOp/ChangeOpSiteLink.php index 0cbe137..7edd3d9 100644 --- a/repo/includes/ChangeOp/ChangeOpSiteLink.php +++ b/repo/includes/ChangeOp/ChangeOpSiteLink.php @@ -7,7 +7,7 @@ use Wikibase\DataModel\Entity\Entity; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; -use Wikibase\DataModel\SiteLink; +use Wikibase\DataModel\SiteLinkList; use Wikibase\Repo\WikibaseRepo; use Wikibase\Summary; @@ -19,40 +19,35 @@ * @author Tobias Gritschacher < tobias.gritschac...@wikimedia.de > * @author Michał Łazowik * @author Bene* < benestar.wikime...@gmail.com > + * @author Thiemo Mättig */ class ChangeOpSiteLink extends ChangeOpBase { /** -* @since 0.4 -* * @var string */ - protected $siteId; + private $siteId; /** -* @since 0.4 -* * @var string|null */ - protected $pageName; + private $pageName; /** -* @since 0.5 -* * @var ItemId[]|null */ -protected $badges; + private $badges; /** * @since 0.4 * * @param string $siteId * @param string|null $pageName Null to remove the sitelink (if $badges are also null) -* @param array|null $badges Null for no-op +* @param ItemId[]|null $badges Null for no-op * * @throws InvalidArgumentException */ - public function __construct( $siteId, $pageName, $badges = null ) { + public function __construct( $siteId, $pageName, array $badges = null ) { if ( !is_string( $siteId ) ) { throw new InvalidArgumentException( '$siteId needs to be a string' ); } @@ -61,14 +56,8 @@ throw new InvalidArgumentException( '$linkPage needs to be a string or null' ); } - if ( !is_array( $badges ) && $badges !== null ) { - throw new InvalidArgumentException( '$badges need to be an array of ItemIds or null' ); - } - if ( $badges !== null ) { - $this->validateBadges( $badges ); - - $badges = $this->removeDuplicateBadges( $badges ); + $badges = $this->validateBadges( $badges ); } $this->siteId = $siteId; @@ -77,116 +66,80 @@ } /** -* @param array $badges +* @param ItemId[] $badges * * @throws InvalidArgumentException +* @return ItemId[] */ private function validateBadges( array $badges ) { $badgeItems = WikibaseRepo::getDefaultInstance()->getSettings()->getSetting( 'badgeItems' ); + $uniqueBadges = array(); - foreach ( $badges as $badge ) { - if ( !( $badge instanceof ItemId ) ) { - throw new InvalidArgumentException( '$badge needs to be an ItemId' ); + foreach ( $badges as $id ) { + if ( !( $id instanceof ItemId ) ) { + throw new InvalidArgumentException( '$badges needs to be an array of ItemId instances' ); } - if ( !array_key_exists( $badge->getSerialization(), $badgeItems ) ) { - throw new InvalidArgumentException( 'Only items specified in the config can be badges' ); + if ( !array_key_exists( $id->getSerialization(), $badgeItems ) ) { + throw new InvalidArgumentException( 'Only items specified in the badgeItems setting can be badges' ); } + + $uniqueBadges[$id->getSerialization()] = $id; } + + return array_values( $uniqueBadges ); } /** -* Removes duplicate badges from the array and returns the new list of badges. -* -* @param ItemId[] $badges -* -* @return ItemId[] -*/ - private function removeDuplicateBadges( array $badges ) { - $final = array(); - foreach ( $badges as $badge ) { - if ( !$this->containsBadge( $final, $badge ) ) { - $final[] = $badge; - } -
[MediaWiki-commits] [Gerrit] Refactoring of ChangeOpSiteLink - change (mediawiki...Wikibase)
Thiemo Mättig (WMDE) has uploaded a new change for review. https://gerrit.wikimedia.org/r/180762 Change subject: Refactoring of ChangeOpSiteLink .. Refactoring of ChangeOpSiteLink Change-Id: I94006953ede551aa9b301fa4f02a2afe8383b223 --- M repo/includes/ChangeOp/ChangeOpSiteLink.php 1 file changed, 56 insertions(+), 104 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/62/180762/1 diff --git a/repo/includes/ChangeOp/ChangeOpSiteLink.php b/repo/includes/ChangeOp/ChangeOpSiteLink.php index 0cbe137..0375186 100644 --- a/repo/includes/ChangeOp/ChangeOpSiteLink.php +++ b/repo/includes/ChangeOp/ChangeOpSiteLink.php @@ -7,7 +7,7 @@ use Wikibase\DataModel\Entity\Entity; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; -use Wikibase\DataModel\SiteLink; +use Wikibase\DataModel\SiteLinkList; use Wikibase\Repo\WikibaseRepo; use Wikibase\Summary; @@ -19,40 +19,35 @@ * @author Tobias Gritschacher < tobias.gritschac...@wikimedia.de > * @author Michał Łazowik * @author Bene* < benestar.wikime...@gmail.com > + * @author Thiemo Mättig */ class ChangeOpSiteLink extends ChangeOpBase { /** -* @since 0.4 -* * @var string */ - protected $siteId; + private $siteId; /** -* @since 0.4 -* * @var string|null */ - protected $pageName; + private $pageName; /** -* @since 0.5 -* * @var ItemId[]|null */ -protected $badges; + private $badges; /** * @since 0.4 * * @param string $siteId * @param string|null $pageName Null to remove the sitelink (if $badges are also null) -* @param array|null $badges Null for no-op +* @param ItemId[]|null $badges Null for no-op * * @throws InvalidArgumentException */ - public function __construct( $siteId, $pageName, $badges = null ) { + public function __construct( $siteId, $pageName, array $badges = null ) { if ( !is_string( $siteId ) ) { throw new InvalidArgumentException( '$siteId needs to be a string' ); } @@ -61,14 +56,8 @@ throw new InvalidArgumentException( '$linkPage needs to be a string or null' ); } - if ( !is_array( $badges ) && $badges !== null ) { - throw new InvalidArgumentException( '$badges need to be an array of ItemIds or null' ); - } - if ( $badges !== null ) { - $this->validateBadges( $badges ); - - $badges = $this->removeDuplicateBadges( $badges ); + $badges = $this->validateBadges( $badges ); } $this->siteId = $siteId; @@ -77,116 +66,80 @@ } /** -* @param array $badges +* @param ItemId[] $badges * * @throws InvalidArgumentException +* @return ItemId[] */ private function validateBadges( array $badges ) { $badgeItems = WikibaseRepo::getDefaultInstance()->getSettings()->getSetting( 'badgeItems' ); + $uniqueBadges = array(); - foreach ( $badges as $badge ) { - if ( !( $badge instanceof ItemId ) ) { - throw new InvalidArgumentException( '$badge needs to be an ItemId' ); + foreach ( $badges as $id ) { + if ( !( $id instanceof ItemId ) ) { + throw new InvalidArgumentException( '$badges needs to be an array of ItemId instances' ); } - if ( !array_key_exists( $badge->getSerialization(), $badgeItems ) ) { - throw new InvalidArgumentException( 'Only items specified in the config can be badges' ); + if ( !array_key_exists( $id->getSerialization(), $badgeItems ) ) { + throw new InvalidArgumentException( 'Only items specified in the badgeItems setting can be badges' ); } + + $uniqueBadges[$id->getSerialization()] = $id; } + + return array_values( $uniqueBadges ); } /** -* Removes duplicate badges from the array and returns the new list of badges. -* -* @param ItemId[] $badges -* -* @return ItemId[] -*/ - private function removeDuplicateBadges( array $badges ) { - $final = array(); - foreach ( $badges as $badge ) { - if ( !$this->containsBadge( $final, $badge ) ) { - $final[] = $badge; -