Completed
Push — main ( 2daa48...b5d932 )
by
unknown
08:38
created

PageRestorer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 8
loc 58
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A restore() 0 10 1
A getUndeleteParams() 8 8 1
A getUndeleteToken() 0 18 2

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\Mediawiki\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
6
use Addwiki\Mediawiki\DataModel\Page;
7
use Addwiki\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
	public function restore( Page $page, array $extraParams = [] ): bool {
24
		$this->api->postRequest(
25
			new SimpleRequest(
26
				'undelete',
27
				$this->getUndeleteParams( $page->getTitle(), $extraParams )
28
			)
29
		);
30
31
		return true;
32
	}
33
34
	/**
35
	 *
36
	 * @return mixed[]
37
	 */
38 View Code Duplication
	private function getUndeleteParams( Title $title, array $extraParams ): array {
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...
39
		$params = [];
40
41
		$params['title'] = $title->getTitle();
42
		$params['token'] = $this->getUndeleteToken( $title );
43
44
		return array_merge( $extraParams, $params );
45
	}
46
47
	/**
48
	 * @param Title $title
49
	 *
50
	 * @throws OutOfBoundsException
51
	 * @return mixed|void
52
	 */
53
	private function getUndeleteToken( Title $title ) {
54
		$response = $this->api->postRequest(
55
			new SimpleRequest(
56
				'query', [
57
				'list' => 'deletedrevs',
58
				'titles' => $title->getTitle(),
59
				'drprop' => 'token',
60
			]
61
			)
62
		);
63
		if ( array_key_exists( 'token', $response['query']['deletedrevs'][0] ) ) {
64
			return $response['query']['deletedrevs'][0]['token'];
65
		} else {
66
			throw new OutOfBoundsException(
67
				'Could not get page undelete token from list=deletedrevs query'
68
			);
69
		}
70
	}
71
72
}
73