Completed
Pull Request — master (#5)
by Sam
01:46
created

Revisions::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Mediawiki\DataModel;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
/**
9
 * Represents a collection or revisions
10
 * @author Addshore
11
 */
12
class Revisions {
13
14
	/**
15
	 * @var Revision[]
16
	 */
17
	private $revisions;
18
19
	/**
20
	 * @param Revisions[] $revisions
21
	 */
22 4
	public function __construct( $revisions = array() ) {
23 4
		$this->revisions = array();
24 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...
25 4
	}
26
27
	/**
28
	 * @param Revision[]|Revisions $revisions
29
	 *
30
	 * @throws InvalidArgumentException
31
	 */
32 4
	public function addRevisions( $revisions ) {
33 4
		if( !is_array( $revisions ) && !$revisions instanceof Revisions ) {
34
			throw new InvalidArgumentException( '$revisions needs to either be an array or a Revisions object' );
35
		}
36 4
		if( $revisions instanceof Revisions ) {
37 1
			$revisions = $revisions->toArray();
38
		}
39 4
		foreach( $revisions as $revision ) {
40 4
			$this->addRevision( $revision );
41
		}
42 4
	}
43
44
	/**
45
	 * @param Revision $revision
46
	 */
47 4
	public function addRevision( Revision $revision ) {
48 4
		$this->revisions[$revision->getId()] = $revision;
49 4
	}
50
51
	/**
52
	 * @param int $id
53
	 *
54
	 * @return bool
55
	 */
56
	public function hasRevisionWithId( $id ){
57
		return array_key_exists( $id, $this->revisions );
58
	}
59
60
	/**
61
	 * @param Revision $revision
62
	 *
63
	 * @return bool
64
	 */
65
	public function hasRevision( Revision $revision ){
66
		return array_key_exists( $revision->getId(), $this->revisions );
67
	}
68
69
	/**
70
	 * @return Revision|null Revision or null if there is no revision
71
	 */
72
	public function getLatest() {
73
		if( empty( $this->revisions ) ) {
74
			return null;
75
		}
76
		return $this->revisions[ max( array_keys( $this->revisions ) ) ];
77
	}
78
79
	/**
80
	 * @param int $revid
81
	 *
82
	 * @throws RuntimeException
83
	 * @throws InvalidArgumentException
84
	 * @return Revision
85
	 */
86
	public function get( $revid ){
87
		if( !is_int( $revid ) ) {
88
			throw new InvalidArgumentException( '$revid needs to be an int' );
89
		}
90
		if( $this->hasRevisionWithId( $revid ) ){
91
			return $this->revisions[$revid];
92
		}
93
		throw new RuntimeException( 'No such revision loaded in Revisions object' );
94
	}
95
96
	/**
97
	 * @return Revision[]
98
	 */
99 4
	public function toArray() {
100 4
		return $this->revisions;
101
	}
102
}