RevisionRollbacker   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 23.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rollback() 0 7 1
A getRollbackParams() 0 14 2
A getTokenForRevision() 14 14 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\Revision;
7
use Mediawiki\DataModel\Title;
8
9
/**
10
 * @access private
11
 *
12
 * @author Addshore
13
 */
14
class RevisionRollbacker extends Service {
15
16
	/**
17
	 * @since 0.3
18
	 *
19
	 * @param Revision $revision
20
	 * @param Title|null $title if using MW 1.24 of lower (https://gerrit.wikimedia.org/r/#/c/133063/)
21
	 *
22
	 * @return bool
23
	 */
24
	public function rollback( Revision $revision, Title $title = null ) {
25
		$this->api->postRequest(
26
			new SimpleRequest( 'rollback', $this->getRollbackParams( $revision, $title ) )
27
		);
28
29
		return true;
30
	}
31
32
	/**
33
	 * @param Revision $revision
34
	 * @param Title|null $title
35
	 *
36
	 * @return array
37
	 */
38
	private function getRollbackParams( Revision $revision, $title ) {
39
		$params = [];
40
		if ( $title !== null ) {
41
			// This is needed prior to https://gerrit.wikimedia.org/r/#/c/133063/
42
			$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...
43
		} else {
44
			// This will work after https://gerrit.wikimedia.org/r/#/c/133063/
45
			$params['pageid'] = $revision->getPageId();
0 ignored issues
show
Bug introduced by
The method getPageId() does not exist on Mediawiki\DataModel\Revision. Did you maybe mean getPageIdentifier()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
		}
47
		$params['user'] = $revision->getUser();
48
		$params['token'] = $this->getTokenForRevision( $revision );
49
50
		return $params;
51
	}
52
53
	/**
54
	 * @param Revision $revision
55
	 *
56
	 * @return string
57
	 */
58 View Code Duplication
	private function getTokenForRevision( Revision $revision ) {
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...
59
		$result = $this->api->postRequest(
60
			new SimpleRequest(
61
				'query', [
62
				'prop' => 'revisions',
63
				'revids' => $revision->getId(),
64
				'rvtoken' => 'rollback',
65
			]
66
			)
67
		);
68
		$result = array_shift( $result['query']['pages'] );
69
70
		return $result['revisions'][0]['rollbacktoken'];
71
	}
72
73
}
74