Complex classes like MWNamespace often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MWNamespace, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class MWNamespace { |
||
34 | |||
35 | /** |
||
36 | * These namespaces should always be first-letter capitalized, now and |
||
37 | * forevermore. Historically, they could've probably been lowercased too, |
||
38 | * but some things are just too ingrained now. :) |
||
39 | */ |
||
40 | private static $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ]; |
||
41 | |||
42 | /** |
||
43 | * Throw an exception when trying to get the subject or talk page |
||
44 | * for a given namespace where it does not make sense. |
||
45 | * Special namespaces are defined in includes/Defines.php and have |
||
46 | * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2) |
||
47 | * |
||
48 | * @param int $index |
||
49 | * @param string $method |
||
50 | * |
||
51 | * @throws MWException |
||
52 | * @return bool |
||
53 | */ |
||
54 | private static function isMethodValidFor( $index, $method ) { |
||
60 | |||
61 | /** |
||
62 | * Can pages in the given namespace be moved? |
||
63 | * |
||
64 | * @param int $index Namespace index |
||
65 | * @return bool |
||
66 | */ |
||
67 | public static function isMovable( $index ) { |
||
79 | |||
80 | /** |
||
81 | * Is the given namespace is a subject (non-talk) namespace? |
||
82 | * |
||
83 | * @param int $index Namespace index |
||
84 | * @return bool |
||
85 | * @since 1.19 |
||
86 | */ |
||
87 | public static function isSubject( $index ) { |
||
90 | |||
91 | /** |
||
92 | * Is the given namespace a talk namespace? |
||
93 | * |
||
94 | * @param int $index Namespace index |
||
95 | * @return bool |
||
96 | */ |
||
97 | public static function isTalk( $index ) { |
||
101 | |||
102 | /** |
||
103 | * Get the talk namespace index for a given namespace |
||
104 | * |
||
105 | * @param int $index Namespace index |
||
106 | * @return int |
||
107 | */ |
||
108 | public static function getTalk( $index ) { |
||
114 | |||
115 | /** |
||
116 | * Get the subject namespace index for a given namespace |
||
117 | * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject. |
||
118 | * |
||
119 | * @param int $index Namespace index |
||
120 | * @return int |
||
121 | */ |
||
122 | public static function getSubject( $index ) { |
||
132 | |||
133 | /** |
||
134 | * Get the associated namespace. |
||
135 | * For talk namespaces, returns the subject (non-talk) namespace |
||
136 | * For subject (non-talk) namespaces, returns the talk namespace |
||
137 | * |
||
138 | * @param int $index Namespace index |
||
139 | * @return int|null If no associated namespace could be found |
||
140 | */ |
||
141 | public static function getAssociated( $index ) { |
||
152 | |||
153 | /** |
||
154 | * Returns whether the specified namespace exists |
||
155 | * |
||
156 | * @param int $index |
||
157 | * |
||
158 | * @return bool |
||
159 | * @since 1.19 |
||
160 | */ |
||
161 | public static function exists( $index ) { |
||
165 | |||
166 | /** |
||
167 | * Returns whether the specified namespaces are the same namespace |
||
168 | * |
||
169 | * @note It's possible that in the future we may start using something |
||
170 | * other than just namespace indexes. Under that circumstance making use |
||
171 | * of this function rather than directly doing comparison will make |
||
172 | * sure that code will not potentially break. |
||
173 | * |
||
174 | * @param int $ns1 The first namespace index |
||
175 | * @param int $ns2 The second namespace index |
||
176 | * |
||
177 | * @return bool |
||
178 | * @since 1.19 |
||
179 | */ |
||
180 | public static function equals( $ns1, $ns2 ) { |
||
183 | |||
184 | /** |
||
185 | * Returns whether the specified namespaces share the same subject. |
||
186 | * eg: NS_USER and NS_USER wil return true, as well |
||
187 | * NS_USER and NS_USER_TALK will return true. |
||
188 | * |
||
189 | * @param int $ns1 The first namespace index |
||
190 | * @param int $ns2 The second namespace index |
||
191 | * |
||
192 | * @return bool |
||
193 | * @since 1.19 |
||
194 | */ |
||
195 | public static function subjectEquals( $ns1, $ns2 ) { |
||
198 | |||
199 | /** |
||
200 | * Returns array of all defined namespaces with their canonical |
||
201 | * (English) names. |
||
202 | * |
||
203 | * @param bool $rebuild Rebuild namespace list (default = false). Used for testing. |
||
204 | * |
||
205 | * @return array |
||
206 | * @since 1.17 |
||
207 | */ |
||
208 | public static function getCanonicalNamespaces( $rebuild = false ) { |
||
222 | |||
223 | /** |
||
224 | * Returns the canonical (English) name for a given index |
||
225 | * |
||
226 | * @param int $index Namespace index |
||
227 | * @return string|bool If no canonical definition. |
||
228 | */ |
||
229 | public static function getCanonicalName( $index ) { |
||
237 | |||
238 | /** |
||
239 | * Returns the index for a given canonical name, or NULL |
||
240 | * The input *must* be converted to lower case first |
||
241 | * |
||
242 | * @param string $name Namespace name |
||
243 | * @return int |
||
244 | */ |
||
245 | public static function getCanonicalIndex( $name ) { |
||
259 | |||
260 | /** |
||
261 | * Returns an array of the namespaces (by integer id) that exist on the |
||
262 | * wiki. Used primarily by the api in help documentation. |
||
263 | * @return array |
||
264 | */ |
||
265 | public static function getValidNamespaces() { |
||
280 | |||
281 | /** |
||
282 | * Can this namespace ever have a talk namespace? |
||
283 | * |
||
284 | * @param int $index Namespace index |
||
285 | * @return bool |
||
286 | */ |
||
287 | public static function canTalk( $index ) { |
||
290 | |||
291 | /** |
||
292 | * Does this namespace contain content, for the purposes of calculating |
||
293 | * statistics, etc? |
||
294 | * |
||
295 | * @param int $index Index to check |
||
296 | * @return bool |
||
297 | */ |
||
298 | public static function isContent( $index ) { |
||
302 | |||
303 | /** |
||
304 | * Might pages in this namespace require the use of the Signature button on |
||
305 | * the edit toolbar? |
||
306 | * |
||
307 | * @param int $index Index to check |
||
308 | * @return bool |
||
309 | */ |
||
310 | public static function wantSignatures( $index ) { |
||
314 | |||
315 | /** |
||
316 | * Can pages in a namespace be watched? |
||
317 | * |
||
318 | * @param int $index |
||
319 | * @return bool |
||
320 | */ |
||
321 | public static function isWatchable( $index ) { |
||
324 | |||
325 | /** |
||
326 | * Does the namespace allow subpages? |
||
327 | * |
||
328 | * @param int $index Index to check |
||
329 | * @return bool |
||
330 | */ |
||
331 | public static function hasSubpages( $index ) { |
||
335 | |||
336 | /** |
||
337 | * Get a list of all namespace indices which are considered to contain content |
||
338 | * @return array Array of namespace indices |
||
339 | */ |
||
340 | public static function getContentNamespaces() { |
||
351 | |||
352 | /** |
||
353 | * List all namespace indices which are considered subject, aka not a talk |
||
354 | * or special namespace. See also MWNamespace::isSubject |
||
355 | * |
||
356 | * @return array Array of namespace indices |
||
357 | */ |
||
358 | public static function getSubjectNamespaces() { |
||
364 | |||
365 | /** |
||
366 | * List all namespace indices which are considered talks, aka not a subject |
||
367 | * or special namespace. See also MWNamespace::isTalk |
||
368 | * |
||
369 | * @return array Array of namespace indices |
||
370 | */ |
||
371 | public static function getTalkNamespaces() { |
||
377 | |||
378 | /** |
||
379 | * Is the namespace first-letter capitalized? |
||
380 | * |
||
381 | * @param int $index Index to check |
||
382 | * @return bool |
||
383 | */ |
||
384 | public static function isCapitalized( $index ) { |
||
403 | |||
404 | /** |
||
405 | * Does the namespace (potentially) have different aliases for different |
||
406 | * genders. Not all languages make a distinction here. |
||
407 | * |
||
408 | * @since 1.18 |
||
409 | * @param int $index Index to check |
||
410 | * @return bool |
||
411 | */ |
||
412 | public static function hasGenderDistinction( $index ) { |
||
415 | |||
416 | /** |
||
417 | * It is not possible to use pages from this namespace as template? |
||
418 | * |
||
419 | * @since 1.20 |
||
420 | * @param int $index Index to check |
||
421 | * @return bool |
||
422 | */ |
||
423 | public static function isNonincludable( $index ) { |
||
427 | |||
428 | /** |
||
429 | * Get the default content model for a namespace |
||
430 | * This does not mean that all pages in that namespace have the model |
||
431 | * |
||
432 | * @since 1.21 |
||
433 | * @param int $index Index to check |
||
434 | * @return null|string Default model name for the given namespace, if set |
||
435 | */ |
||
436 | public static function getNamespaceContentModel( $index ) { |
||
442 | |||
443 | /** |
||
444 | * Determine which restriction levels it makes sense to use in a namespace, |
||
445 | * optionally filtered by a user's rights. |
||
446 | * |
||
447 | * @since 1.23 |
||
448 | * @param int $index Index to check |
||
449 | * @param User $user User to check |
||
450 | * @return array |
||
451 | */ |
||
452 | public static function getRestrictionLevels( $index, User $user = null ) { |
||
512 | } |
||
513 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.