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 WikiRevision 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 WikiRevision, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class WikiRevision { |
||
| 34 | /** @todo Unused? */ |
||
| 35 | public $importer = null; |
||
| 36 | |||
| 37 | /** @var Title */ |
||
| 38 | public $title = null; |
||
| 39 | |||
| 40 | /** @var int */ |
||
| 41 | public $id = 0; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | public $timestamp = "20010115000000"; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */ |
||
| 49 | public $user = 0; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | public $user_text = ""; |
||
| 53 | |||
| 54 | /** @var User */ |
||
| 55 | public $userObj = null; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | public $model = null; |
||
| 59 | |||
| 60 | /** @var string */ |
||
| 61 | public $format = null; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | public $text = ""; |
||
| 65 | |||
| 66 | /** @var int */ |
||
| 67 | protected $size; |
||
| 68 | |||
| 69 | /** @var Content */ |
||
| 70 | public $content = null; |
||
| 71 | |||
| 72 | /** @var ContentHandler */ |
||
| 73 | protected $contentHandler = null; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | public $comment = ""; |
||
| 77 | |||
| 78 | /** @var bool */ |
||
| 79 | public $minor = false; |
||
| 80 | |||
| 81 | /** @var string */ |
||
| 82 | public $type = ""; |
||
| 83 | |||
| 84 | /** @var string */ |
||
| 85 | public $action = ""; |
||
| 86 | |||
| 87 | /** @var string */ |
||
| 88 | public $params = ""; |
||
| 89 | |||
| 90 | /** @var string */ |
||
| 91 | public $fileSrc = ''; |
||
| 92 | |||
| 93 | /** @var bool|string */ |
||
| 94 | public $sha1base36 = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var bool |
||
| 98 | * @todo Unused? |
||
| 99 | */ |
||
| 100 | public $isTemp = false; |
||
| 101 | |||
| 102 | /** @var string */ |
||
| 103 | public $archiveName = ''; |
||
| 104 | |||
| 105 | protected $filename; |
||
| 106 | |||
| 107 | /** @var mixed */ |
||
| 108 | protected $src; |
||
| 109 | |||
| 110 | /** @todo Unused? */ |
||
| 111 | public $fileIsTemp; |
||
| 112 | |||
| 113 | /** @var bool */ |
||
| 114 | private $mNoUpdates = false; |
||
| 115 | |||
| 116 | /** @var Config $config */ |
||
| 117 | private $config; |
||
| 118 | |||
| 119 | public function __construct( Config $config ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param Title $title |
||
| 125 | * @throws MWException |
||
| 126 | */ |
||
| 127 | function setTitle( $title ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param int $id |
||
| 140 | */ |
||
| 141 | function setID( $id ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $ts |
||
| 147 | */ |
||
| 148 | function setTimestamp( $ts ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $user |
||
| 155 | */ |
||
| 156 | function setUsername( $user ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param User $user |
||
| 162 | */ |
||
| 163 | function setUserObj( $user ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $ip |
||
| 169 | */ |
||
| 170 | function setUserIP( $ip ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $model |
||
| 176 | */ |
||
| 177 | function setModel( $model ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $format |
||
| 183 | */ |
||
| 184 | function setFormat( $format ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $text |
||
| 190 | */ |
||
| 191 | function setText( $text ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $text |
||
| 197 | */ |
||
| 198 | function setComment( $text ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param bool $minor |
||
| 204 | */ |
||
| 205 | function setMinor( $minor ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param mixed $src |
||
| 211 | */ |
||
| 212 | function setSrc( $src ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $src |
||
| 218 | * @param bool $isTemp |
||
| 219 | */ |
||
| 220 | function setFileSrc( $src, $isTemp ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $sha1base36 |
||
| 227 | */ |
||
| 228 | function setSha1Base36( $sha1base36 ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $filename |
||
| 234 | */ |
||
| 235 | function setFilename( $filename ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $archiveName |
||
| 241 | */ |
||
| 242 | function setArchiveName( $archiveName ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param int $size |
||
| 248 | */ |
||
| 249 | function setSize( $size ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $type |
||
| 255 | */ |
||
| 256 | function setType( $type ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $action |
||
| 262 | */ |
||
| 263 | function setAction( $action ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param array $params |
||
| 269 | */ |
||
| 270 | function setParams( $params ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param bool $noupdates |
||
| 276 | */ |
||
| 277 | public function setNoUpdates( $noupdates ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return Title |
||
| 283 | */ |
||
| 284 | function getTitle() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return int |
||
| 290 | */ |
||
| 291 | function getID() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | function getTimestamp() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | function getUser() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return User |
||
| 311 | */ |
||
| 312 | function getUserObj() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return string |
||
| 318 | * |
||
| 319 | * @deprecated Since 1.21, use getContent() instead. |
||
| 320 | */ |
||
| 321 | function getText() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return ContentHandler |
||
| 329 | */ |
||
| 330 | function getContentHandler() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return Content |
||
| 340 | */ |
||
| 341 | function getContent() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | function getModel() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | function getFormat() { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | function getComment() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | function getMinor() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return mixed |
||
| 388 | */ |
||
| 389 | function getSrc() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return bool|string |
||
| 395 | */ |
||
| 396 | function getSha1() { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | function getFileSrc() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | function isTempSrc() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return mixed |
||
| 419 | */ |
||
| 420 | function getFilename() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | function getArchiveName() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return mixed |
||
| 433 | */ |
||
| 434 | function getSize() { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | function getType() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | function getAction() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | function getParams() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | function importOldRevision() { |
||
| 560 | |||
| 561 | function importLogItem() { |
||
| 562 | $dbw = wfGetDB( DB_MASTER ); |
||
| 563 | |||
| 564 | $user = $this->getUserObj() ?: User::newFromName( $this->getUser() ); |
||
| 565 | View Code Duplication | if ( $user ) { |
|
| 566 | $userId = intval( $user->getId() ); |
||
| 567 | $userText = $user->getName(); |
||
| 568 | } else { |
||
| 569 | $userId = 0; |
||
| 570 | $userText = $this->getUser(); |
||
| 571 | } |
||
| 572 | |||
| 573 | # @todo FIXME: This will not record autoblocks |
||
| 574 | if ( !$this->getTitle() ) { |
||
| 575 | wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " . |
||
| 576 | $this->timestamp . "\n" ); |
||
| 577 | return false; |
||
| 578 | } |
||
| 579 | # Check if it exists already |
||
| 580 | // @todo FIXME: Use original log ID (better for backups) |
||
| 581 | $prior = $dbw->selectField( 'logging', '1', |
||
| 582 | [ 'log_type' => $this->getType(), |
||
| 583 | 'log_action' => $this->getAction(), |
||
| 584 | 'log_timestamp' => $dbw->timestamp( $this->timestamp ), |
||
| 585 | 'log_namespace' => $this->getTitle()->getNamespace(), |
||
| 586 | 'log_title' => $this->getTitle()->getDBkey(), |
||
| 587 | 'log_comment' => $this->getComment(), |
||
| 588 | # 'log_user_text' => $this->user_text, |
||
| 589 | 'log_params' => $this->params ], |
||
| 590 | __METHOD__ |
||
| 591 | ); |
||
| 592 | // @todo FIXME: This could fail slightly for multiple matches :P |
||
| 593 | if ( $prior ) { |
||
| 594 | wfDebug( __METHOD__ |
||
| 595 | . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " |
||
| 596 | . $this->timestamp . "\n" ); |
||
| 597 | return false; |
||
| 598 | } |
||
| 599 | $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' ); |
||
| 600 | $data = [ |
||
| 601 | 'log_id' => $log_id, |
||
| 602 | 'log_type' => $this->type, |
||
| 603 | 'log_action' => $this->action, |
||
| 604 | 'log_timestamp' => $dbw->timestamp( $this->timestamp ), |
||
| 605 | 'log_user' => $userId, |
||
| 606 | 'log_user_text' => $userText, |
||
| 607 | 'log_namespace' => $this->getTitle()->getNamespace(), |
||
| 608 | 'log_title' => $this->getTitle()->getDBkey(), |
||
| 609 | 'log_comment' => $this->getComment(), |
||
| 610 | 'log_params' => $this->params |
||
| 611 | ]; |
||
| 612 | $dbw->insert( 'logging', $data, __METHOD__ ); |
||
| 613 | |||
| 614 | return true; |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @return bool |
||
| 619 | */ |
||
| 620 | function importUpload() { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @return bool|string |
||
| 690 | */ |
||
| 691 | function downloadSource() { |
||
| 718 | |||
| 719 | } |
||
| 720 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.