RevisionSaver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 6.17 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 69.05%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 5
loc 81
ccs 29
cts 42
cp 0.6905
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 8 2
A addEditInfoParams() 0 12 4
B getEditParams() 5 36 6

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\EditInfo;
7
use Mediawiki\DataModel\Revision;
8
use RuntimeException;
9
10
/**
11
 * @access private
12
 *
13
 * @author Addshore
14
 * @author DFelten (EditInfo fix)
15
 */
16
class RevisionSaver extends Service {
17
18
	/**
19
	 * @since 0.2
20
	 *
21
	 * @param Revision $revision
22
	 * @param EditInfo|null $editInfo
23
	 *
24
	 * @return bool success
25
	 */
26 10
	public function save( Revision $revision, EditInfo $editInfo = null ) {
27 10
		$editInfo = $editInfo ? $editInfo : $revision->getEditInfo();
28
29 10
		$result = $this->api->postRequest(
30 10
			new SimpleRequest( 'edit', $this->getEditParams( $revision, $editInfo ) )
31 10
		);
32 10
		return ( $result['edit']['result'] == 'Success' );
33
	}
34
35
	/**
36
	 * @param Revision $revision
37
	 * @param EditInfo|null $editInfo
38
	 *
39
	 * @throws RuntimeException
40
	 * @return array
41
	 */
42 10
	private function getEditParams( Revision $revision, EditInfo $editInfo = null ) {
43 10
		if ( !$revision->getPageIdentifier()->identifiesPage() ) {
44
			throw new RuntimeException( '$revision PageIdentifier does not identify a page' );
45
		}
46
47 10
		$params = [];
48
49 10
		$content = $revision->getContent();
50 10
		$data = $content->getData();
51 10
		if ( !is_string( $data ) ) {
52
			throw new RuntimeException( 'Dont know how to save content of this model.' );
53
		}
54 10
		$params['text'] = $content->getData();
55 10
		$params['md5'] = md5( $content->getData() );
56
57 10
		$timestamp = $revision->getTimestamp();
58 10
		if ( $timestamp !== null ) {
59
			$params['basetimestamp'] = $timestamp;
60
		}
61
62 10 View Code Duplication
		if ( $revision->getPageIdentifier()->getId() !== null ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
			$params['pageid'] = $revision->getPageIdentifier()->getId();
64
		} else {
65 10
			$params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle();
66
		}
67
68 10
		$params['token'] = $this->api->getToken();
69
70 10
		if ( $this->api->isLoggedin() ) {
71
			$params['assert'] = 'user';
72
		}
73
74 10
		$this->addEditInfoParams( $editInfo, $params );
75
76 10
		return $params;
77
	}
78
79
	/**
80
	 * @param null|EditInfo $editInfo
81
	 * @param array &$params
82
	 */
83 10
	private function addEditInfoParams( $editInfo, &$params ) {
84 10
		if ( $editInfo !== null ) {
85 10
			$params['summary'] = $editInfo->getSummary();
86 10
			if ( $editInfo->getMinor() ) {
87
				$params['minor'] = true;
88
			}
89 10
			if ( $editInfo->getBot() ) {
90
				$params['bot'] = true;
91
				$params['assert'] = 'bot';
92
			}
93 10
		}
94 10
	}
95
96
}
97