Completed
Pull Request — master (#6)
by
unknown
02:47
created

User::__construct()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 21
cts 21
cp 1
rs 4.8196
cc 10
eloc 20
nc 7
nop 7
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mediawiki\DataModel;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Represents a mediawiki user
9
 * @author Addshore
10
 */
11
class User {
12
13
	/**
14
	 * @var string
15
	 */
16
	private $name;
17
18
	/**
19
	 * @var int
20
	 */
21
	private $id;
22
23
	/**
24
	 * @var int
25
	 */
26
	private $editcount;
27
28
	/**
29
	 * @var string
30
	 */
31
	private $registration;
32
33
	/**
34
	 * @var array
35
	 */
36
	private $groups;
37
38
	/**
39
	 * @var array
40
	 */
41
	private $rights;
42
43
	/**
44
	 * @var string
45
	 */
46
	private $gender;
47
48
	/**
49
	 * @param string $name
50
	 * @param int $id
51
	 * @param int $editcount
52
	 * @param string $registration
53
	 * @param array[] $groups groups grouped by type.
54
	 *          Keys to use are 'groups' and 'implicitgroups' as returned by the api.
55
	 * @param array $rights
56
	 * @param string $gender
57
	 *
58
	 * @throws InvalidArgumentException
59
	 */
60 12
	public function __construct( $name, $id, $editcount, $registration, $groups, $rights, $gender ) {
61 12
		if( !is_string( $name ) || empty( $name ) ) {
62 1
			throw new InvalidArgumentException( '$name must be a string and can not be empty' );
63
		}
64 11
		if( !is_int( $id ) ) {
65 1
			throw new InvalidArgumentException( '$id must be an int' );
66
		}
67 10
		if( !is_int( $editcount ) ) {
68 1
			throw new InvalidArgumentException( '$editcount must be an int' );
69
		}
70 9
		if( !is_array( $groups ) || !array_key_exists( 'groups', $groups ) || !array_key_exists( 'implicitgroups', $groups ) ) {
71 4
			throw new InvalidArgumentException( '$groups must be an array or arrays with keys "groups" and "implicitgroups"' );
72
		}
73 5
		if( !is_array( $rights ) ) {
74 1
			throw new InvalidArgumentException( '$rights must be an array' );
75
		}
76 4
		if( !is_string( $gender ) ) {
77 1
			throw new InvalidArgumentException( '$gender must be a string' );
78
		}
79
80 3
		$this->editcount = $editcount;
81 3
		$this->gender = $gender;
82 3
		$this->groups = $groups;
83 3
		$this->id = $id;
84 3
		$this->name = $name;
85 3
		$this->registration = $registration;
86 3
		$this->rights = $rights;
87 3
	}
88
89
	/**
90
	 * @return int
91
	 */
92 3
	public function getEditcount() {
93 3
		return $this->editcount;
94
	}
95
96
	/**
97
	 * @return string
98
	 */
99 3
	public function getGender() {
100 3
		return $this->gender;
101
	}
102
103
	/**
104
	 * @param string $type 'groups' or 'implicitgroups'
105
	 *
106
	 * @return array
107
	 */
108 3
	public function getGroups( $type = 'groups' ) {
109 3
		return $this->groups[$type];
110
	}
111
112
	/**
113
	 * @return int
114
	 */
115 3
	public function getId() {
116 3
		return $this->id;
117
	}
118
119
	/**
120
	 * @return string
121
	 */
122 3
	public function getName() {
123 3
		return $this->name;
124
	}
125
126
	/**
127
	 * @return string
128
	 */
129 3
	public function getRegistration() {
130 3
		return $this->registration;
131
	}
132
133
	/**
134
	 * @return array
135
	 */
136 3
	public function getRights() {
137 3
		return $this->rights;
138
	}
139
140
} 
141