DerivativeContext   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 305
Duplicated Lines 3.61 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 11
loc 305
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 1
cbo 8

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setConfig() 0 3 1
A getConfig() 0 7 2
A getStats() 0 3 1
A getTiming() 0 7 2
A setRequest() 0 3 1
A getRequest() 0 7 2
A setTitle() 0 3 1
A getTitle() 0 7 2
A canUseWikiPage() 0 9 3
A setWikiPage() 0 3 1
A getWikiPage() 0 7 2
A setOutput() 0 3 1
A getOutput() 0 7 2
A setUser() 0 3 1
A getUser() 0 7 2
A setLanguage() 11 11 3
A getLanguage() 0 7 2
A setSkin() 0 4 1
A getSkin() 0 7 2
A msg() 0 5 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
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 * http://www.gnu.org/copyleft/gpl.html
17
 *
18
 * @author Daniel Friesen
19
 * @file
20
 */
21
use Liuggio\StatsdClient\Factory\StatsdDataFactory;
22
use MediaWiki\MediaWikiServices;
23
24
/**
25
 * An IContextSource implementation which will inherit context from another source
26
 * but allow individual pieces of context to be changed locally
27
 * eg: A ContextSource that can inherit from the main RequestContext but have
28
 *     a different Title instance set on it.
29
 * @since 1.19
30
 */
31
class DerivativeContext extends ContextSource implements MutableContext {
32
	/**
33
	 * @var WebRequest
34
	 */
35
	private $request;
36
37
	/**
38
	 * @var Title
39
	 */
40
	private $title;
41
42
	/**
43
	 * @var WikiPage
44
	 */
45
	private $wikipage;
46
47
	/**
48
	 * @var OutputPage
49
	 */
50
	private $output;
51
52
	/**
53
	 * @var User
54
	 */
55
	private $user;
56
57
	/**
58
	 * @var Language
59
	 */
60
	private $lang;
61
62
	/**
63
	 * @var Skin
64
	 */
65
	private $skin;
66
67
	/**
68
	 * @var Config
69
	 */
70
	private $config;
71
72
	/**
73
	 * @var Timing
74
	 */
75
	private $timing;
76
77
	/**
78
	 * Constructor
79
	 * @param IContextSource $context Context to inherit from
80
	 */
81
	public function __construct( IContextSource $context ) {
82
		$this->setContext( $context );
83
	}
84
85
	/**
86
	 * Set the SiteConfiguration object
87
	 *
88
	 * @param Config $s
89
	 */
90
	public function setConfig( Config $s ) {
91
		$this->config = $s;
92
	}
93
94
	/**
95
	 * Get the Config object
96
	 *
97
	 * @return Config
98
	 */
99
	public function getConfig() {
100
		if ( !is_null( $this->config ) ) {
101
			return $this->config;
102
		} else {
103
			return $this->getContext()->getConfig();
104
		}
105
	}
106
107
	/**
108
	 * Get the stats object
109
	 *
110
	 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
111
	 *
112
	 * @return StatsdDataFactory
113
	 */
114
	public function getStats() {
115
		return MediaWikiServices::getInstance()->getStatsdDataFactory();
116
	}
117
118
	/**
119
	 * Get the timing object
120
	 *
121
	 * @return Timing
122
	 */
123
	public function getTiming() {
124
		if ( !is_null( $this->timing ) ) {
125
			return $this->timing;
126
		} else {
127
			return $this->getContext()->getTiming();
128
		}
129
	}
130
131
	/**
132
	 * Set the WebRequest object
133
	 *
134
	 * @param WebRequest $r
135
	 */
136
	public function setRequest( WebRequest $r ) {
137
		$this->request = $r;
138
	}
139
140
	/**
141
	 * Get the WebRequest object
142
	 *
143
	 * @return WebRequest
144
	 */
145
	public function getRequest() {
146
		if ( !is_null( $this->request ) ) {
147
			return $this->request;
148
		} else {
149
			return $this->getContext()->getRequest();
150
		}
151
	}
152
153
	/**
154
	 * Set the Title object
155
	 *
156
	 * @param Title $t
157
	 */
158
	public function setTitle( Title $t ) {
159
		$this->title = $t;
160
	}
161
162
	/**
163
	 * Get the Title object
164
	 *
165
	 * @return Title|null
166
	 */
167
	public function getTitle() {
168
		if ( !is_null( $this->title ) ) {
169
			return $this->title;
170
		} else {
171
			return $this->getContext()->getTitle();
172
		}
173
	}
174
175
	/**
176
	 * Check whether a WikiPage object can be get with getWikiPage().
177
	 * Callers should expect that an exception is thrown from getWikiPage()
178
	 * if this method returns false.
179
	 *
180
	 * @since 1.19
181
	 * @return bool
182
	 */
183
	public function canUseWikiPage() {
184
		if ( $this->wikipage !== null ) {
185
			return true;
186
		} elseif ( $this->title !== null ) {
187
			return $this->title->canExist();
188
		} else {
189
			return $this->getContext()->canUseWikiPage();
190
		}
191
	}
192
193
	/**
194
	 * Set the WikiPage object
195
	 *
196
	 * @since 1.19
197
	 * @param WikiPage $p
198
	 */
199
	public function setWikiPage( WikiPage $p ) {
200
		$this->wikipage = $p;
201
	}
202
203
	/**
204
	 * Get the WikiPage object.
205
	 * May throw an exception if there's no Title object set or the Title object
206
	 * belongs to a special namespace that doesn't have WikiPage, so use first
207
	 * canUseWikiPage() to check whether this method can be called safely.
208
	 *
209
	 * @since 1.19
210
	 * @return WikiPage
211
	 */
212
	public function getWikiPage() {
213
		if ( !is_null( $this->wikipage ) ) {
214
			return $this->wikipage;
215
		} else {
216
			return $this->getContext()->getWikiPage();
217
		}
218
	}
219
220
	/**
221
	 * Set the OutputPage object
222
	 *
223
	 * @param OutputPage $o
224
	 */
225
	public function setOutput( OutputPage $o ) {
226
		$this->output = $o;
227
	}
228
229
	/**
230
	 * Get the OutputPage object
231
	 *
232
	 * @return OutputPage
233
	 */
234
	public function getOutput() {
235
		if ( !is_null( $this->output ) ) {
236
			return $this->output;
237
		} else {
238
			return $this->getContext()->getOutput();
239
		}
240
	}
241
242
	/**
243
	 * Set the User object
244
	 *
245
	 * @param User $u
246
	 */
247
	public function setUser( User $u ) {
248
		$this->user = $u;
249
	}
250
251
	/**
252
	 * Get the User object
253
	 *
254
	 * @return User
255
	 */
256
	public function getUser() {
257
		if ( !is_null( $this->user ) ) {
258
			return $this->user;
259
		} else {
260
			return $this->getContext()->getUser();
261
		}
262
	}
263
264
	/**
265
	 * Set the Language object
266
	 *
267
	 * @param Language|string $l Language instance or language code
268
	 * @throws MWException
269
	 * @since 1.19
270
	 */
271 View Code Duplication
	public function setLanguage( $l ) {
272
		if ( $l instanceof Language ) {
273
			$this->lang = $l;
274
		} elseif ( is_string( $l ) ) {
275
			$l = RequestContext::sanitizeLangCode( $l );
276
			$obj = Language::factory( $l );
277
			$this->lang = $obj;
278
		} else {
279
			throw new MWException( __METHOD__ . " was passed an invalid type of data." );
280
		}
281
	}
282
283
	/**
284
	 * Get the Language object
285
	 *
286
	 * @return Language
287
	 * @since 1.19
288
	 */
289
	public function getLanguage() {
290
		if ( !is_null( $this->lang ) ) {
291
			return $this->lang;
292
		} else {
293
			return $this->getContext()->getLanguage();
294
		}
295
	}
296
297
	/**
298
	 * Set the Skin object
299
	 *
300
	 * @param Skin $s
301
	 */
302
	public function setSkin( Skin $s ) {
303
		$this->skin = clone $s;
304
		$this->skin->setContext( $this );
305
	}
306
307
	/**
308
	 * Get the Skin object
309
	 *
310
	 * @return Skin
311
	 */
312
	public function getSkin() {
313
		if ( !is_null( $this->skin ) ) {
314
			return $this->skin;
315
		} else {
316
			return $this->getContext()->getSkin();
317
		}
318
	}
319
320
	/**
321
	 * Get a message using the current context.
322
	 *
323
	 * This can't just inherit from ContextSource, since then
324
	 * it would set only the original context, and not take
325
	 * into account any changes.
326
	 *
327
	 * @param mixed $args,... Arguments to wfMessage
0 ignored issues
show
Bug introduced by
There is no parameter named $args,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
328
	 * @return Message
329
	 */
330
	public function msg() {
331
		$args = func_get_args();
332
333
		return call_user_func_array( 'wfMessage', $args )->setContext( $this );
334
	}
335
}
336