PageRestorer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 62
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A restore() 0 10 1
A getUndeleteParams() 0 8 1
A getUndeleteToken() 0 18 2
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use Mediawiki\Api\SimpleRequest;
6
use Mediawiki\DataModel\Page;
7
use Mediawiki\DataModel\Title;
8
use OutOfBoundsException;
9
10
/**
11
 * @access private
12
 *
13
 * @author Addshore
14
 */
15
class PageRestorer extends Service {
16
17
	/**
18
	 * @since 0.3
19
	 *
20
	 * @param Page $page
21
	 * @param array $extraParams
22
	 *
23
	 * @return bool
24
	 */
25
	public function restore( Page $page, array $extraParams = [] ) {
26
		$this->api->postRequest(
27
			new SimpleRequest(
28
				'undelete',
29
				$this->getUndeleteParams( $page->getTitle(), $extraParams )
0 ignored issues
show
Bug introduced by
It seems like $page->getTitle() can be null; however, getUndeleteParams() 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...
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getTitle() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
30
			)
31
		);
32
33
		return true;
34
	}
35
36
	/**
37
	 * @param Title $title
38
	 * @param array $extraParams
39
	 *
40
	 * @return array
41
	 */
42
	private function getUndeleteParams( Title $title, $extraParams ) {
43
		$params = [];
44
45
		$params['title'] = $title->getTitle();
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Title::getTitle() has been deprecated with message: in 0.6 use getText (makes things look cleaner)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
46
		$params['token'] = $this->getUndeleteToken( $title );
47
48
		return array_merge( $extraParams, $params );
49
	}
50
51
	/**
52
	 * @param Title $title
53
	 *
54
	 * @throws OutOfBoundsException
55
	 * @return string
56
	 */
57
	private function getUndeleteToken( Title $title ) {
58
		$response = $this->api->postRequest(
59
			new SimpleRequest(
60
				'query', [
61
				'list' => 'deletedrevs',
62
				'titles' => $title->getTitle(),
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Title::getTitle() has been deprecated with message: in 0.6 use getText (makes things look cleaner)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
63
				'drprop' => 'token',
64
			]
65
			)
66
		);
67
		if ( array_key_exists( 'token', $response['query']['deletedrevs'][0] ) ) {
68
			return $response['query']['deletedrevs'][0]['token'];
69
		} else {
70
			throw new OutOfBoundsException(
71
				'Could not get page undelete token from list=deletedrevs query'
72
			);
73
		}
74
	}
75
76
}
77