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 { |
||
| 41 | /** @var StatusValue */ |
||
| 42 | protected $sv; |
||
| 43 | |||
| 44 | /** @var mixed */ |
||
| 45 | public $value; |
||
| 46 | /** @var array Map of (key => bool) to indicate success of each part of batch operations */ |
||
| 47 | public $success = []; |
||
| 48 | /** @var int Counter for batch operations */ |
||
| 49 | public $successCount = 0; |
||
| 50 | /** @var int Counter for batch operations */ |
||
| 51 | public $failCount = 0; |
||
| 52 | |||
| 53 | /** @var callable */ |
||
| 54 | public $cleanCallback = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param StatusValue $sv [optional] |
||
| 58 | */ |
||
| 59 | public function __construct( StatusValue $sv = null ) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Succinct helper method to wrap a StatusValue |
||
| 70 | * |
||
| 71 | * This is is useful when formatting StatusValue objects: |
||
| 72 | * @code |
||
| 73 | * $this->getOutput()->addHtml( Status::wrap( $sv )->getHTML() ); |
||
| 74 | * @endcode |
||
| 75 | * |
||
| 76 | * @param StatusValue|Status $sv |
||
| 77 | * @return Status |
||
| 78 | */ |
||
| 79 | public static function wrap( $sv ) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Factory function for fatal errors |
||
| 85 | * |
||
| 86 | * @param string|Message $message Message name or object |
||
| 87 | * @return Status |
||
| 88 | */ |
||
| 89 | public static function newFatal( $message /*, parameters...*/ ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Factory function for good results |
||
| 97 | * |
||
| 98 | * @param mixed $value |
||
| 99 | * @return Status |
||
| 100 | */ |
||
| 101 | public static function newGood( $value = null ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Change operation result |
||
| 110 | * |
||
| 111 | * @param bool $ok Whether the operation completed |
||
| 112 | * @param mixed $value |
||
| 113 | */ |
||
| 114 | public function setResult( $ok, $value = null ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns the wrapped StatusValue object |
||
| 120 | * @return StatusValue |
||
| 121 | * @since 1.27 |
||
| 122 | */ |
||
| 123 | public function getStatusValue() { |
||
| 124 | return $this->sv; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns whether the operation completed and didn't have any error or |
||
| 129 | * warnings |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function isGood() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns whether the operation completed |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function isOK() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add a new warning |
||
| 148 | * |
||
| 149 | * @param string|Message $message Message name or object |
||
| 150 | */ |
||
| 151 | public function warning( $message /*, parameters... */ ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Add an error, do not set fatal flag |
||
| 157 | * This can be used for non-fatal errors |
||
| 158 | * |
||
| 159 | * @param string|Message $message Message name or object |
||
| 160 | */ |
||
| 161 | public function error( $message /*, parameters... */ ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add an error and set OK to false, indicating that the operation |
||
| 167 | * as a whole was fatal |
||
| 168 | * |
||
| 169 | * @param string|Message $message Message name or object |
||
| 170 | */ |
||
| 171 | public function fatal( $message /*, parameters... */ ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $params |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | protected function cleanParams( array $params ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string|Language|null $lang Language to use for processing |
||
| 192 | * messages, or null to default to the user language. |
||
| 193 | * @return Language |
||
| 194 | */ |
||
| 195 | protected function languageFromParam( $lang ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the error list as a wikitext formatted list |
||
| 210 | * |
||
| 211 | * @param string|bool $shortContext A short enclosing context message name, to |
||
| 212 | * be used when there is a single error |
||
| 213 | * @param string|bool $longContext A long enclosing context message name, for a list |
||
| 214 | * @param string|Language $lang Language to use for processing messages |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get a bullet list of the errors as a Message object. |
||
| 255 | * |
||
| 256 | * $shortContext and $longContext can be used to wrap the error list in some text. |
||
| 257 | * $shortContext will be preferred when there is a single error; $longContext will be |
||
| 258 | * preferred when there are multiple ones. In either case, $1 will be replaced with |
||
| 259 | * the list of errors. |
||
| 260 | * |
||
| 261 | * $shortContext is assumed to use $1 as an inline parameter: if there is a single item, |
||
| 262 | * it will not be made into a list; if there are multiple items, newlines will be inserted |
||
| 263 | * around the list. |
||
| 264 | * $longContext is assumed to use $1 as a standalone parameter; it will always receive a list. |
||
| 265 | * |
||
| 266 | * If both parameters are missing, and there is only one error, no bullet will be added. |
||
| 267 | * |
||
| 268 | * @param string|string[] $shortContext A message name or an array of message names. |
||
| 269 | * @param string|string[] $longContext A message name or an array of message names. |
||
| 270 | * @param string|Language $lang Language to use for processing messages |
||
| 271 | * @return Message |
||
| 272 | */ |
||
| 273 | public function getMessage( $shortContext = false, $longContext = false, $lang = null ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return the message for a single error. |
||
| 317 | * @param mixed $error With an array & two values keyed by |
||
| 318 | * 'message' and 'params', use those keys-value pairs. |
||
| 319 | * Otherwise, if its an array, just use the first value as the |
||
| 320 | * message and the remaining items as the params. |
||
| 321 | * @param string|Language $lang Language to use for processing messages |
||
| 322 | * @return Message |
||
| 323 | */ |
||
| 324 | protected function getErrorMessage( $error, $lang = null ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the error message as HTML. This is done by parsing the wikitext error |
||
| 346 | * message. |
||
| 347 | * @param string $shortContext A short enclosing context message name, to |
||
| 348 | * be used when there is a single error |
||
| 349 | * @param string $longContext A long enclosing context message name, for a list |
||
| 350 | * @param string|Language $lang Language to use for processing messages |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getHTML( $shortContext = false, $longContext = false, $lang = null ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Return an array with a Message object for each error. |
||
| 362 | * @param array $errors |
||
| 363 | * @param string|Language $lang Language to use for processing messages |
||
| 364 | * @return Message[] |
||
| 365 | */ |
||
| 366 | protected function getErrorMessageArray( $errors, $lang = null ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Merge another status object into this one |
||
| 375 | * |
||
| 376 | * @param Status $other Other Status object |
||
| 377 | * @param bool $overwriteValue Whether to override the "value" member |
||
| 378 | */ |
||
| 379 | public function merge( $other, $overwriteValue = false ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get the list of errors (but not warnings) |
||
| 385 | * |
||
| 386 | * @return array A list in which each entry is an array with a message key as its first element. |
||
| 387 | * The remaining array elements are the message parameters. |
||
| 388 | * @deprecated 1.25 |
||
| 389 | */ |
||
| 390 | public function getErrorsArray() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get the list of warnings (but not errors) |
||
| 396 | * |
||
| 397 | * @return array A list in which each entry is an array with a message key as its first element. |
||
| 398 | * The remaining array elements are the message parameters. |
||
| 399 | * @deprecated 1.25 |
||
| 400 | */ |
||
| 401 | public function getWarningsArray() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns a list of status messages of the given type (or all if false) |
||
| 407 | * |
||
| 408 | * @note: this handles RawMessage poorly |
||
| 409 | * |
||
| 410 | * @param string|bool $type |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | protected function getStatusArray( $type = false ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Returns a list of status messages of the given type, with message and |
||
| 436 | * params left untouched, like a sane version of getStatusArray |
||
| 437 | * |
||
| 438 | * Each entry is a map of: |
||
| 439 | * - message: string message key or MessageSpecifier |
||
| 440 | * - params: array list of parameters |
||
| 441 | * |
||
| 442 | * @param string $type |
||
| 443 | * @return array |
||
| 444 | */ |
||
| 445 | public function getErrorsByType( $type ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Returns true if the specified message is present as a warning or error |
||
| 451 | * |
||
| 452 | * @param string|Message $message Message key or object to search for |
||
| 453 | * |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | public function hasMessage( $message ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * If the specified source message exists, replace it with the specified |
||
| 462 | * destination message, but keep the same parameters as in the original error. |
||
| 463 | * |
||
| 464 | * Note, due to the lack of tools for comparing Message objects, this |
||
| 465 | * function will not work when using a Message object as the search parameter. |
||
| 466 | * |
||
| 467 | * @param Message|string $source Message key or object to search for |
||
| 468 | * @param Message|string $dest Replacement message key or object |
||
| 469 | * @return bool Return true if the replacement was done, false otherwise. |
||
| 470 | */ |
||
| 471 | public function replaceMessage( $source, $dest ) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return mixed |
||
| 477 | */ |
||
| 478 | public function getValue() { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Backwards compatibility logic |
||
| 484 | * |
||
| 485 | * @param string $name |
||
| 486 | */ |
||
| 487 | function __get( $name ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Backwards compatibility logic |
||
| 498 | * |
||
| 499 | * @param string $name |
||
| 500 | * @param mixed $value |
||
| 501 | */ |
||
| 502 | function __set( $name, $value ) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | public function __toString() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Don't save the callback when serializing, because Closures can't be |
||
| 522 | * serialized and we're going to clear it in __wakeup anyway. |
||
| 523 | */ |
||
| 524 | function __sleep() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Sanitize the callback parameter on wakeup, to avoid arbitrary execution. |
||
| 531 | */ |
||
| 532 | function __wakeup() { |
||
| 535 | } |
||
| 536 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.