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 |
||
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 ) { |
||
84 | |||
85 | /** |
||
86 | * Set the SiteConfiguration object |
||
87 | * |
||
88 | * @param Config $s |
||
89 | */ |
||
90 | public function setConfig( Config $s ) { |
||
93 | |||
94 | /** |
||
95 | * Get the Config object |
||
96 | * |
||
97 | * @return Config |
||
98 | */ |
||
99 | public function getConfig() { |
||
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() { |
||
117 | |||
118 | /** |
||
119 | * Get the timing object |
||
120 | * |
||
121 | * @return Timing |
||
122 | */ |
||
123 | public function getTiming() { |
||
130 | |||
131 | /** |
||
132 | * Set the WebRequest object |
||
133 | * |
||
134 | * @param WebRequest $r |
||
135 | */ |
||
136 | public function setRequest( WebRequest $r ) { |
||
139 | |||
140 | /** |
||
141 | * Get the WebRequest object |
||
142 | * |
||
143 | * @return WebRequest |
||
144 | */ |
||
145 | public function getRequest() { |
||
152 | |||
153 | /** |
||
154 | * Set the Title object |
||
155 | * |
||
156 | * @param Title $t |
||
157 | */ |
||
158 | public function setTitle( Title $t ) { |
||
161 | |||
162 | /** |
||
163 | * Get the Title object |
||
164 | * |
||
165 | * @return Title|null |
||
166 | */ |
||
167 | public function getTitle() { |
||
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() { |
||
192 | |||
193 | /** |
||
194 | * Set the WikiPage object |
||
195 | * |
||
196 | * @since 1.19 |
||
197 | * @param WikiPage $p |
||
198 | */ |
||
199 | public function setWikiPage( WikiPage $p ) { |
||
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() { |
||
219 | |||
220 | /** |
||
221 | * Set the OutputPage object |
||
222 | * |
||
223 | * @param OutputPage $o |
||
224 | */ |
||
225 | public function setOutput( OutputPage $o ) { |
||
228 | |||
229 | /** |
||
230 | * Get the OutputPage object |
||
231 | * |
||
232 | * @return OutputPage |
||
233 | */ |
||
234 | public function getOutput() { |
||
241 | |||
242 | /** |
||
243 | * Set the User object |
||
244 | * |
||
245 | * @param User $u |
||
246 | */ |
||
247 | public function setUser( User $u ) { |
||
250 | |||
251 | /** |
||
252 | * Get the User object |
||
253 | * |
||
254 | * @return User |
||
255 | */ |
||
256 | public function getUser() { |
||
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 ) { |
|
282 | |||
283 | /** |
||
284 | * Get the Language object |
||
285 | * |
||
286 | * @return Language |
||
287 | * @since 1.19 |
||
288 | */ |
||
289 | public function getLanguage() { |
||
296 | |||
297 | /** |
||
298 | * Set the Skin object |
||
299 | * |
||
300 | * @param Skin $s |
||
301 | */ |
||
302 | public function setSkin( Skin $s ) { |
||
306 | |||
307 | /** |
||
308 | * Get the Skin object |
||
309 | * |
||
310 | * @return Skin |
||
311 | */ |
||
312 | public function getSkin() { |
||
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 |
||
|
|||
328 | * @return Message |
||
329 | */ |
||
330 | public function msg() { |
||
335 | } |
||
336 |
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.