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:
Complex classes like Status 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 Status, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Status extends StatusValue { |
||
41 | /** @var callable */ |
||
42 | public $cleanCallback = false; |
||
43 | |||
44 | /** |
||
45 | * Succinct helper method to wrap a StatusValue |
||
46 | * |
||
47 | * This is is useful when formatting StatusValue objects: |
||
48 | * @code |
||
49 | * $this->getOutput()->addHtml( Status::wrap( $sv )->getHTML() ); |
||
50 | * @endcode |
||
51 | * |
||
52 | * @param StatusValue|Status $sv |
||
53 | * @return Status |
||
54 | */ |
||
55 | public static function wrap( $sv ) { |
||
70 | |||
71 | /** |
||
72 | * Backwards compatibility logic |
||
73 | * |
||
74 | * @param string $name |
||
75 | * @return mixed |
||
76 | * @throws RuntimeException |
||
77 | */ |
||
78 | function __get( $name ) { |
||
87 | |||
88 | /** |
||
89 | * Change operation result |
||
90 | * Backwards compatibility logic |
||
91 | * |
||
92 | * @param string $name |
||
93 | * @param mixed $value |
||
94 | * @throws RuntimeException |
||
95 | */ |
||
96 | function __set( $name, $value ) { |
||
106 | |||
107 | /** |
||
108 | * Splits this Status object into two new Status objects, one which contains only |
||
109 | * the error messages, and one that contains the warnings, only. The returned array is |
||
110 | * defined as: |
||
111 | * [ |
||
112 | * 0 => object(Status) # the Status with error messages, only |
||
113 | * 1 => object(Status) # The Status with warning messages, only |
||
114 | * ] |
||
115 | * |
||
116 | * @return Status[] |
||
117 | */ |
||
118 | public function splitByErrorType() { |
||
125 | |||
126 | /** |
||
127 | * Returns the wrapped StatusValue object |
||
128 | * @return StatusValue |
||
129 | * @since 1.27 |
||
130 | */ |
||
131 | public function getStatusValue() { |
||
134 | |||
135 | /** |
||
136 | * @param array $params |
||
137 | * @return array |
||
138 | */ |
||
139 | protected function cleanParams( array $params ) { |
||
149 | |||
150 | /** |
||
151 | * @param string|Language|null $lang Language to use for processing |
||
152 | * messages, or null to default to the user language. |
||
153 | * @return Language |
||
154 | */ |
||
155 | protected function languageFromParam( $lang ) { |
||
167 | |||
168 | /** |
||
169 | * Get the error list as a wikitext formatted list |
||
170 | * |
||
171 | * @param string|bool $shortContext A short enclosing context message name, to |
||
172 | * be used when there is a single error |
||
173 | * @param string|bool $longContext A long enclosing context message name, for a list |
||
174 | * @param string|Language $lang Language to use for processing messages |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) { |
||
212 | |||
213 | /** |
||
214 | * Get a bullet list of the errors as a Message object. |
||
215 | * |
||
216 | * $shortContext and $longContext can be used to wrap the error list in some text. |
||
217 | * $shortContext will be preferred when there is a single error; $longContext will be |
||
218 | * preferred when there are multiple ones. In either case, $1 will be replaced with |
||
219 | * the list of errors. |
||
220 | * |
||
221 | * $shortContext is assumed to use $1 as an inline parameter: if there is a single item, |
||
222 | * it will not be made into a list; if there are multiple items, newlines will be inserted |
||
223 | * around the list. |
||
224 | * $longContext is assumed to use $1 as a standalone parameter; it will always receive a list. |
||
225 | * |
||
226 | * If both parameters are missing, and there is only one error, no bullet will be added. |
||
227 | * |
||
228 | * @param string|string[]|bool $shortContext A message name or an array of message names. |
||
229 | * @param string|string[]|bool $longContext A message name or an array of message names. |
||
230 | * @param string|Language $lang Language to use for processing messages |
||
231 | * @return Message |
||
232 | */ |
||
233 | public function getMessage( $shortContext = false, $longContext = false, $lang = null ) { |
||
274 | |||
275 | /** |
||
276 | * Return the message for a single error |
||
277 | * |
||
278 | * The code string can be used a message key with per-language versions. |
||
279 | * If $error is an array, the "params" field is a list of parameters for the message. |
||
280 | * |
||
281 | * @param array|string $error Code string or (key: code string, params: string[]) map |
||
282 | * @param string|Language $lang Language to use for processing messages |
||
283 | * @return Message |
||
284 | */ |
||
285 | protected function getErrorMessage( $error, $lang = null ) { |
||
306 | |||
307 | /** |
||
308 | * Get the error message as HTML. This is done by parsing the wikitext error message |
||
309 | * @param string|bool $shortContext A short enclosing context message name, to |
||
310 | * be used when there is a single error |
||
311 | * @param string|bool $longContext A long enclosing context message name, for a list |
||
312 | * @param string|Language|null $lang Language to use for processing messages |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getHTML( $shortContext = false, $longContext = false, $lang = null ) { |
||
321 | |||
322 | /** |
||
323 | * Return an array with a Message object for each error. |
||
324 | * @param array $errors |
||
325 | * @param string|Language $lang Language to use for processing messages |
||
326 | * @return Message[] |
||
327 | */ |
||
328 | protected function getErrorMessageArray( $errors, $lang = null ) { |
||
334 | |||
335 | /** |
||
336 | * Get the list of errors (but not warnings) |
||
337 | * |
||
338 | * @return array A list in which each entry is an array with a message key as its first element. |
||
339 | * The remaining array elements are the message parameters. |
||
340 | * @deprecated since 1.25 |
||
341 | */ |
||
342 | public function getErrorsArray() { |
||
345 | |||
346 | /** |
||
347 | * Get the list of warnings (but not errors) |
||
348 | * |
||
349 | * @return array A list in which each entry is an array with a message key as its first element. |
||
350 | * The remaining array elements are the message parameters. |
||
351 | * @deprecated since 1.25 |
||
352 | */ |
||
353 | public function getWarningsArray() { |
||
356 | |||
357 | /** |
||
358 | * Returns a list of status messages of the given type (or all if false) |
||
359 | * |
||
360 | * @note: this handles RawMessage poorly |
||
361 | * |
||
362 | * @param string|bool $type |
||
363 | * @return array |
||
364 | */ |
||
365 | protected function getStatusArray( $type = false ) { |
||
385 | |||
386 | /** |
||
387 | * Don't save the callback when serializing, because Closures can't be |
||
388 | * serialized and we're going to clear it in __wakeup anyway. |
||
389 | */ |
||
390 | function __sleep() { |
||
394 | |||
395 | /** |
||
396 | * Sanitize the callback parameter on wakeup, to avoid arbitrary execution. |
||
397 | */ |
||
398 | function __wakeup() { |
||
401 | } |
||
402 |
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.