GlobalUserInfo::get_merged()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of Peachy MediaWiki Bot API
5
 *
6
 * Peachy is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
class GlobalUserInfo {
21
22
	/**
23
	 * Wiki class
24
	 *
25
	 * @var Wiki
26
	 * @access private
27
	 */
28
	private $wiki;
29
30
	/**
31
	 * Username
32
	 *
33
	 * @var string
34
	 * @access private
35
	 */
36
	private $pgUsername;
0 ignored issues
show
Unused Code introduced by
The property $pgUsername is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
	/**
39
	 * Global groups member is a part of
40
	 *
41
	 * @var array
42
	 * @access private
43
	 */
44
	private $groups = array();
45
46
	/**
47
	 * Accounts that user has merged on other wikis
48
	 *
49
	 * @var array
50
	 * @access private
51
	 */
52
	private $merged = array();
53
54
	/**
55
	 * Accounts that are not attached to the global account
56
	 *
57
	 * @var array
58
	 * @access private
59
	 */
60
	private $unattached = array();
61
62
	/**
63
	 * Whether or not global account exists
64
	 *
65
	 * @var bool
66
	 * @access private
67
	 */
68
	private $exists = true;
69
70
	/**
71
	 * Date that global account was created
72
	 *
73
	 * @var string
74
	 * @access private
75
	 */
76
	private $registration;
77
78
	/**
79
	 * Global account ID
80
	 *
81
	 * @var int
82
	 * @access private
83
	 */
84
	private $id;
85
86
	/**
87
	 * Construction method for the GlobalUserInfo class
88
	 *
89
	 * @access public
90
	 * @param Wiki &$wikiClass The Wiki class object
91
	 * @param mixed $pgUsername Username
92
	 * @throws APIError
93
	 * @throws AssertFailure
94
	 * @throws DependencyError
95
	 * @throws LoggedOut
96
	 * @throws MWAPIError
97
	 */
98
	function __construct( Wiki &$wikiClass, $pgUsername ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __construct.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
99
100
		if( !array_key_exists( 'Central Auth', $wikiClass->get_extensions() ) ) {
101
			throw new DependencyError( "CentralAuth", "http://www.mediawiki.org/wiki/Extension:CentralAuth" );
0 ignored issues
show
Documentation introduced by
'http://www.mediawiki.or.../Extension:CentralAuth' is of type string, but the function expects a boolean.

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...
102
		}
103
104
		$this->username = ucfirst( $pgUsername );
0 ignored issues
show
Bug introduced by
The property username does not seem to exist. Did you mean pgUsername?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
		$this->wiki = $wikiClass;
106
107
		$guiRes = $this->wiki->apiQuery(
108
			array(
109
				'action'  => 'query',
110
				'meta'    => 'globaluserinfo',
111
				'guiuser' => ucfirst( $pgUsername ),
112
				'guiprop' => 'groups|merged|unattached',
113
			), false, false
114
		);
115
116
		if( !isset( $guiRes['query']['globaluserinfo'] ) ) {
117
			$this->exists = false;
118
			if( isset( $guiRes['error'] ) && $guiRes['error']['code'] != 'guinosuchuser' ) {
119
				throw new MWAPIError($guiRes['error']);
120
			} elseif( @$guiRes['error']['code'] != 'guinosuchuser' ) {
121
				throw new MWAPIError(array('code' => 'UnknownError', 'info' => 'Unknown API Error'));
122
			}
123
		} else {
124
			$this->groups = $guiRes['query']['globaluserinfo']['groups'];
125
			$this->merged = $guiRes['query']['globaluserinfo']['merged'];
126
			$this->merged = $guiRes['query']['globaluserinfo']['unattached'];
127
			$this->id = $guiRes['query']['globaluserinfo']['id'];
128
			$this->registration = $guiRes['query']['globaluserinfo']['registration'];
129
		}
130
	}
131
132
	/**
133
	 * Returns the global account ID
134
	 *
135
	 * @return int
136
	 * @access public
137
	 */
138
	public function get_id() {
139
		return $this->id;
140
	}
141
142
	/**
143
	 * Returns the date that global account was created
144
	 *
145
	 * @return string
146
	 * @access public
147
	 */
148
	public function get_registration() {
149
		return $this->registration;
150
	}
151
152
	/**
153
	 * Returns the global groups member is a part of
154
	 *
155
	 * @return array
156
	 * @access public
157
	 */
158
	public function get_groups() {
159
		return $this->groups;
160
	}
161
162
	/**
163
	 * Returns the accounts that user has merged on other wikis
164
	 *
165
	 * @return array
166
	 * @access public
167
	 */
168
	public function get_merged() {
169
		return $this->merged;
170
	}
171
172
	/**
173
	 * Returns the accounts that are not attached to the global account
174
	 *
175
	 * @return array
176
	 * @access public
177
	 */
178
	public function get_unattached() {
179
		return $this->unattached;
180
	}
181
182
	/**
183
	 * Returns whether or not global account exists
184
	 *
185
	 * @return bool
186
	 * @access public
187
	 */
188
	public function get_exists() {
189
		return $this->unattached;
190
	}
191
192
}