Revisions   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 12.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 11
loc 91
ccs 18
cts 33
cp 0.5455
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addRevisions() 11 11 5
A addRevision() 0 3 1
A hasRevisionWithId() 0 3 1
A hasRevision() 0 3 1
A getLatest() 0 6 2
A get() 0 9 3
A toArray() 0 3 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\DataModel;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
/**
9
 * Represents a collection or revisions
10
 *
11
 * @author Addshore
12
 */
13
class Revisions {
14
15
	/**
16
	 * @var Revision[]
17
	 */
18
	private $revisions;
19
20
	/**
21
	 * @param Revisions[] $revisions
22 4
	 */
23 4
	public function __construct( $revisions = [] ) {
24 4
		$this->revisions = [];
25 4
		$this->addRevisions( $revisions );
0 ignored issues
show
Documentation introduced by
$revisions is of type array<integer,object<Med...i\DataModel\Revisions>>, but the function expects a array<integer,object<Med...ki\DataModel\Revisions>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
	}
27
28
	/**
29
	 * @param Revision[]|Revisions $revisions
30
	 *
31
	 * @throws InvalidArgumentException
32 4
	 */
33 4 View Code Duplication
	public function addRevisions( $revisions ) {
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...
34
		if ( !is_array( $revisions ) && !$revisions instanceof Revisions ) {
35
			throw new InvalidArgumentException( '$revisions needs to either be an array or a Revisions object' );
36 4
		}
37 1
		if ( $revisions instanceof Revisions ) {
38 1
			$revisions = $revisions->toArray();
39 4
		}
40 4
		foreach ( $revisions as $revision ) {
41 4
			$this->addRevision( $revision );
42 4
		}
43
	}
44
45
	/**
46
	 * @param Revision $revision
47 4
	 */
48 4
	public function addRevision( Revision $revision ) {
49 4
		$this->revisions[$revision->getId()] = $revision;
50
	}
51
52
	/**
53
	 * @param int $id
54
	 *
55
	 * @return bool
56
	 */
57
	public function hasRevisionWithId( $id ) {
58
		return array_key_exists( $id, $this->revisions );
59
	}
60
61
	/**
62
	 * @param Revision $revision
63
	 *
64
	 * @return bool
65
	 */
66
	public function hasRevision( Revision $revision ) {
67
		return array_key_exists( $revision->getId(), $this->revisions );
68
	}
69
70
	/**
71
	 * @return Revision|null Revision or null if there is no revision
72
	 */
73
	public function getLatest() {
74
		if ( empty( $this->revisions ) ) {
75
			return null;
76
		}
77
		return $this->revisions[ max( array_keys( $this->revisions ) ) ];
78
	}
79
80
	/**
81
	 * @param int $revid
82
	 *
83
	 * @throws RuntimeException
84
	 * @throws InvalidArgumentException
85
	 * @return Revision
86
	 */
87
	public function get( $revid ) {
88
		if ( !is_int( $revid ) ) {
89
			throw new InvalidArgumentException( '$revid needs to be an int' );
90
		}
91
		if ( $this->hasRevisionWithId( $revid ) ) {
92
			return $this->revisions[$revid];
93
		}
94
		throw new RuntimeException( 'No such revision loaded in Revisions object' );
95
	}
96
97
	/**
98
	 * @return Revision[]
99 4
	 */
100 4
	public function toArray() {
101
		return $this->revisions;
102
	}
103
}
104