Completed
Push — main ( b55b92...e8d442 )
by
unknown
05:41
created

StatementRemover   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 19.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 9
loc 46
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Addwiki\Wikibase\Api\Service;
4
5
use Addwiki\Mediawiki\DataModel\EditInfo;
6
use Addwiki\Wikibase\Api\WikibaseApi;
7
use UnexpectedValueException;
8
use Wikibase\DataModel\Statement\Statement;
9
use Wikibase\DataModel\Statement\StatementGuid;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 */
16
class StatementRemover {
17
18
	private WikibaseApi $api;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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