Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class SiteLinkSetter { |
||
| 19 | |||
| 20 | private WikibaseApi $api; |
||
|
|
|||
| 21 | |||
| 22 | /** |
||
| 23 | * @param WikibaseApi $api |
||
| 24 | */ |
||
| 25 | public function __construct( WikibaseApi $api ) { |
||
| 26 | $this->api = $api; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @since 0.2 |
||
| 31 | * |
||
| 32 | * @param SiteLink $siteLink |
||
| 33 | * @param EntityId|Item|Property|SiteLink $target |
||
| 34 | * @param EditInfo|null $editInfo |
||
| 35 | */ |
||
| 36 | public function set( SiteLink $siteLink, $target, EditInfo $editInfo = null ): bool { |
||
| 37 | $this->throwExceptionsOnBadTarget( $target ); |
||
| 38 | |||
| 39 | $params = $this->getTargetParamsFromTarget( |
||
| 40 | $this->getEntityIdentifierFromTarget( $target ) |
||
| 41 | ); |
||
| 42 | |||
| 43 | $params['linksite'] = $siteLink->getSiteId(); |
||
| 44 | $params['linktitle'] = $siteLink->getPageName(); |
||
| 45 | $params['badges'] = implode( '|', $siteLink->getBadges() ); |
||
| 46 | |||
| 47 | $this->api->postRequest( 'wbsetsitelink', $params, $editInfo ); |
||
| 48 | return true; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param mixed $target |
||
| 53 | * |
||
| 54 | * @throws UnexpectedValueException |
||
| 55 | * |
||
| 56 | * @todo Fix duplicated code |
||
| 57 | */ |
||
| 58 | private function throwExceptionsOnBadTarget( $target ): void { |
||
| 59 | if ( !$target instanceof EntityId && !$target instanceof Item && !$target instanceof Property && !$target instanceof SiteLink ) { |
||
| 60 | throw new UnexpectedValueException( '$target needs to be an EntityId, Item, Property or SiteLink' ); |
||
| 61 | } |
||
| 62 | if ( ( $target instanceof Item || $target instanceof Property ) && $target->getId() === null ) { |
||
| 63 | throw new UnexpectedValueException( '$target Entity object needs to have an Id set' ); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param EntityId|Item|Property $target |
||
| 69 | * |
||
| 70 | * @throws UnexpectedValueException |
||
| 71 | * @return EntityId|SiteLink |
||
| 72 | * |
||
| 73 | * @todo Fix duplicated code |
||
| 74 | */ |
||
| 75 | private function getEntityIdentifierFromTarget( $target ) { |
||
| 76 | if ( $target instanceof Item || $target instanceof Property ) { |
||
| 77 | return $target->getId(); |
||
| 78 | } else { |
||
| 79 | return $target; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param EntityId|SiteLink $target |
||
| 85 | * |
||
| 86 | * @throws UnexpectedValueException |
||
| 87 | * @return array |
||
| 88 | * |
||
| 89 | * @todo Fix duplicated code |
||
| 90 | */ |
||
| 91 | private function getTargetParamsFromTarget( $target ) { |
||
| 92 | if ( $target instanceof EntityId ) { |
||
| 93 | return [ 'id' => $target->getSerialization() ]; |
||
| 94 | } elseif ( $target instanceof SiteLink ) { |
||
| 95 | return [ |
||
| 96 | 'site' => $target->getSiteId(), |
||
| 97 | 'title' => $target->getPageName(), |
||
| 98 | ]; |
||
| 99 | } else { |
||
| 100 | throw new UnexpectedValueException( '$target needs to be an EntityId or SiteLink' ); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 |