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 |
||
| 16 | class StatementRemover { |
||
| 17 | |||
| 18 | private WikibaseApi $api; |
||
|
|
|||
| 19 | |||
| 20 | /** |
||
| 21 | * @param WikibaseApi $api |
||
| 22 | */ |
||
| 23 | public function __construct( WikibaseApi $api ) { |
||
| 24 | $this->api = $api; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @since 0.2 |
||
| 29 | * |
||
| 30 | * @param Statement|StatementGuid|string $statement Statement object or GUID |
||
| 31 | * @param EditInfo|null $editInfo |
||
| 32 | * |
||
| 33 | * @throws UnexpectedValueException |
||
| 34 | */ |
||
| 35 | public function remove( $statement, EditInfo $editInfo = null ): bool { |
||
| 36 | if ( is_string( $statement ) ) { |
||
| 37 | $guid = $statement; |
||
| 38 | } elseif ( $statement instanceof StatementGuid ) { |
||
| 39 | $guid = $statement->getSerialization(); |
||
| 40 | } elseif ( $statement instanceof Statement ) { |
||
| 41 | $guid = $statement->getGuid(); |
||
| 42 | } else { |
||
| 43 | throw new UnexpectedValueException( 'Could not get statement guid from $statement' ); |
||
| 44 | } |
||
| 45 | if ( !is_string( $guid ) ) { |
||
| 46 | throw new UnexpectedValueException( 'Unexpected statement guid got from $statement' ); |
||
| 47 | } |
||
| 48 | |||
| 49 | $params = [ |
||
| 50 | 'claim' => $guid, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | $this->api->postRequest( 'wbremoveclaims', $params, $editInfo ); |
||
| 54 | return true; |
||
| 55 | } |
||
| 56 | |||
| 57 | } |
||
| 58 |