PageDeleter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 15.56 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 14
loc 90
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteFromPageId() 0 7 1
A deleteFromPageTitle() 0 10 2
A getDeleteParams() 0 13 2
A delete() 7 7 1
A deleteFromRevision() 7 7 1

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 Mediawiki\Api\Service;
4
5
use Mediawiki\Api\SimpleRequest;
6
use Mediawiki\DataModel\Page;
7
use Mediawiki\DataModel\PageIdentifier;
8
use Mediawiki\DataModel\Revision;
9
use Mediawiki\DataModel\Title;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 */
16
class PageDeleter extends Service {
17
18
	/**
19
	 * @since 0.2
20
	 *
21
	 * @param Page $page
22
	 * @param array $extraParams
23
	 *
24
	 * @return bool
25
	 */
26 View Code Duplication
	public function delete( Page $page, array $extraParams = [] ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
		$this->api->postRequest( new SimpleRequest(
28
			'delete',
29
			$this->getDeleteParams( $page->getPageIdentifier(), $extraParams )
30
		) );
31
		return true;
32
	}
33
34
	/**
35
	 * @since 0.2
36
	 *
37
	 * @param Revision $revision
38
	 * @param array $extraParams
39
	 *
40
	 * @return bool
41
	 */
42 View Code Duplication
	public function deleteFromRevision( Revision $revision, array $extraParams = [] ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
		$this->api->postRequest( new SimpleRequest(
44
			'delete',
45
			$this->getDeleteParams( $revision->getPageIdentifier(), $extraParams )
0 ignored issues
show
Bug introduced by
It seems like $revision->getPageIdentifier() can be null; however, getDeleteParams() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
46
		) );
47
		return true;
48
	}
49
50
	/**
51
	 * @since 0.2
52
	 *
53
	 * @param int $pageid
54
	 * @param array $extraParams
55
	 *
56
	 * @return bool
57
	 */
58
	public function deleteFromPageId( $pageid, array $extraParams = [] ) {
59
		$this->api->postRequest( new SimpleRequest(
60
			'delete',
61
			$this->getDeleteParams( new PageIdentifier( null, $pageid ), $extraParams )
62
		) );
63
		return true;
64
	}
65
66
	/**
67
	 * @since 0.5
68
	 *
69
	 * @param Title|string $title
70
	 * @param array $extraParams
71
	 *
72
	 * @return bool
73
	 */
74
	public function deleteFromPageTitle( $title, array $extraParams = [] ) {
75
		if ( is_string( $title ) ) {
76
			$title = new Title( $title );
77
		}
78
		$this->api->postRequest( new SimpleRequest(
79
			'delete',
80
			$this->getDeleteParams( new PageIdentifier( $title ), $extraParams )
81
		) );
82
		return true;
83
	}
84
85
	/**
86
	 * @param PageIdentifier $identifier
87
	 * @param array $extraParams
88
	 *
89
	 * @return array
90
	 */
91
	private function getDeleteParams( PageIdentifier $identifier, $extraParams ) {
92
		$params = [];
93
94
		if ( $identifier->getId() !== null ) {
95
			$params['pageid'] = $identifier->getId();
96
		} else {
97
			$params['title'] = $identifier->getTitle()->getTitle();
98
		}
99
100
		$params['token'] = $this->api->getToken( 'delete' );
101
102
		return array_merge( $extraParams, $params );
103
	}
104
105
}
106