Completed
Push — master ( 11966d...5d848f )
by Sam
13s
created

PageDeleter::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
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
	public function delete( Page $page, array $extraParams = [] ) {
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
	public function deleteFromRevision( Revision $revision, array $extraParams = [] ) {
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 ( !is_null( $identifier->getId() ) ) {
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