PageDeleter::getDeleteParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 8
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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