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 whether the operation completed and didn't have any error or |
||
| 120 | * warnings |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function isGood() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns whether the operation completed |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function isOK() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Add a new warning |
||
| 139 | * |
||
| 140 | * @param string|Message $message Message name or object |
||
| 141 | */ |
||
| 142 | public function warning( $message /*, parameters... */ ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add an error, do not set fatal flag |
||
| 148 | * This can be used for non-fatal errors |
||
| 149 | * |
||
| 150 | * @param string|Message $message Message name or object |
||
| 151 | */ |
||
| 152 | public function error( $message /*, parameters... */ ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add an error and set OK to false, indicating that the operation |
||
| 158 | * as a whole was fatal |
||
| 159 | * |
||
| 160 | * @param string|Message $message Message name or object |
||
| 161 | */ |
||
| 162 | public function fatal( $message /*, parameters... */ ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param array $params |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function cleanParams( array $params ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string|Language|null $lang Language to use for processing |
||
| 183 | * messages, or null to default to the user language. |
||
| 184 | * @return Language |
||
| 185 | */ |
||
| 186 | protected function languageFromParam( $lang ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the error list as a wikitext formatted list |
||
| 201 | * |
||
| 202 | * @param string|bool $shortContext A short enclosing context message name, to |
||
| 203 | * be used when there is a single error |
||
| 204 | * @param string|bool $longContext A long enclosing context message name, for a list |
||
| 205 | * @param string|Language $lang Language to use for processing messages |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the error list as a Message object |
||
| 246 | * |
||
| 247 | * @param string|string[] $shortContext A short enclosing context message name (or an array of |
||
| 248 | * message names), to be used when there is a single error. |
||
| 249 | * @param string|string[] $longContext A long enclosing context message name (or an array of |
||
| 250 | * message names), for a list. |
||
| 251 | * @param string|Language $lang Language to use for processing messages |
||
| 252 | * @return Message |
||
| 253 | */ |
||
| 254 | public function getMessage( $shortContext = false, $longContext = false, $lang = null ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return the message for a single error. |
||
| 302 | * @param mixed $error With an array & two values keyed by |
||
| 303 | * 'message' and 'params', use those keys-value pairs. |
||
| 304 | * Otherwise, if its an array, just use the first value as the |
||
| 305 | * message and the remaining items as the params. |
||
| 306 | * @param string|Language $lang Language to use for processing messages |
||
| 307 | * @return Message |
||
| 308 | */ |
||
| 309 | protected function getErrorMessage( $error, $lang = null ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the error message as HTML. This is done by parsing the wikitext error |
||
| 331 | * message. |
||
| 332 | * @param string $shortContext A short enclosing context message name, to |
||
| 333 | * be used when there is a single error |
||
| 334 | * @param string $longContext A long enclosing context message name, for a list |
||
| 335 | * @param string|Language $lang Language to use for processing messages |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getHTML( $shortContext = false, $longContext = false, $lang = null ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Return an array with a Message object for each error. |
||
| 347 | * @param array $errors |
||
| 348 | * @param string|Language $lang Language to use for processing messages |
||
| 349 | * @return Message[] |
||
| 350 | */ |
||
| 351 | protected function getErrorMessageArray( $errors, $lang = null ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Merge another status object into this one |
||
| 360 | * |
||
| 361 | * @param Status $other Other Status object |
||
| 362 | * @param bool $overwriteValue Whether to override the "value" member |
||
| 363 | */ |
||
| 364 | public function merge( $other, $overwriteValue = false ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get the list of errors (but not warnings) |
||
| 370 | * |
||
| 371 | * @return array A list in which each entry is an array with a message key as its first element. |
||
| 372 | * The remaining array elements are the message parameters. |
||
| 373 | * @deprecated 1.25 |
||
| 374 | */ |
||
| 375 | public function getErrorsArray() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the list of warnings (but not errors) |
||
| 381 | * |
||
| 382 | * @return array A list in which each entry is an array with a message key as its first element. |
||
| 383 | * The remaining array elements are the message parameters. |
||
| 384 | * @deprecated 1.25 |
||
| 385 | */ |
||
| 386 | public function getWarningsArray() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns a list of status messages of the given type (or all if false) |
||
| 392 | * |
||
| 393 | * @note: this handles RawMessage poorly |
||
| 394 | * |
||
| 395 | * @param string|bool $type |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | protected function getStatusArray( $type = false ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Returns a list of status messages of the given type, with message and |
||
| 421 | * params left untouched, like a sane version of getStatusArray |
||
| 422 | * |
||
| 423 | * @param string $type |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | public function getErrorsByType( $type ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Returns true if the specified message is present as a warning or error |
||
| 433 | * |
||
| 434 | * @param string|Message $message Message key or object to search for |
||
| 435 | * |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function hasMessage( $message ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * If the specified source message exists, replace it with the specified |
||
| 444 | * destination message, but keep the same parameters as in the original error. |
||
| 445 | * |
||
| 446 | * Note, due to the lack of tools for comparing Message objects, this |
||
| 447 | * function will not work when using a Message object as the search parameter. |
||
| 448 | * |
||
| 449 | * @param Message|string $source Message key or object to search for |
||
| 450 | * @param Message|string $dest Replacement message key or object |
||
| 451 | * @return bool Return true if the replacement was done, false otherwise. |
||
| 452 | */ |
||
| 453 | public function replaceMessage( $source, $dest ) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return mixed |
||
| 459 | */ |
||
| 460 | public function getValue() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Backwards compatibility logic |
||
| 466 | * |
||
| 467 | * @param string $name |
||
| 468 | */ |
||
| 469 | function __get( $name ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Backwards compatibility logic |
||
| 480 | * |
||
| 481 | * @param string $name |
||
| 482 | * @param mixed $value |
||
| 483 | */ |
||
| 484 | function __set( $name, $value ) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function __toString() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Don't save the callback when serializing, because Closures can't be |
||
| 504 | * serialized and we're going to clear it in __wakeup anyway. |
||
| 505 | */ |
||
| 506 | function __sleep() { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Sanitize the callback parameter on wakeup, to avoid arbitrary execution. |
||
| 513 | */ |
||
| 514 | function __wakeup() { |
||
| 517 | } |
||
| 518 |
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.