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 BaseCircle 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 BaseCircle, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class BaseCircle { |
||
| 34 | |||
| 35 | const CIRCLES_SETTINGS_DEFAULT = [ |
||
| 36 | 'password_enforcement' => 'false', |
||
| 37 | 'allow_links' => 'false', |
||
| 38 | 'allow_links_auto' => 'false', |
||
| 39 | 'allow_links_files' => 'false' |
||
| 40 | ]; |
||
| 41 | |||
| 42 | const CIRCLES_PERSONAL = 1; |
||
| 43 | const CIRCLES_SECRET = 2; |
||
| 44 | const CIRCLES_CLOSED = 4; |
||
| 45 | const CIRCLES_PUBLIC = 8; |
||
| 46 | |||
| 47 | const CIRCLES_ALL = 15; |
||
| 48 | |||
| 49 | const SHORT_UNIQUE_ID_LENGTH = 14; |
||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | private $id; |
||
| 53 | |||
| 54 | /** @var IL10N */ |
||
| 55 | protected $l10n; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | private $uniqueId = ''; |
||
| 59 | |||
| 60 | /** @var string */ |
||
| 61 | private $name; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | private $altName = ''; |
||
| 65 | |||
| 66 | /** @var Member */ |
||
| 67 | private $owner; |
||
| 68 | |||
| 69 | /** @var Member */ |
||
| 70 | private $viewer = null; |
||
| 71 | |||
| 72 | /** @var Member */ |
||
| 73 | private $viewerGroup; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | private $description = ''; |
||
| 77 | |||
| 78 | /** @var array */ |
||
| 79 | private $settings = []; |
||
| 80 | |||
| 81 | /** @var int */ |
||
| 82 | private $type; |
||
| 83 | |||
| 84 | /** @var string */ |
||
| 85 | private $contactGroupName = ''; |
||
| 86 | |||
| 87 | /** @var int */ |
||
| 88 | private $contactAddressBook = 0; |
||
| 89 | |||
| 90 | |||
| 91 | /** @var string */ |
||
| 92 | private $creation; |
||
| 93 | |||
| 94 | /** @var Member[] */ |
||
| 95 | private $members; |
||
| 96 | |||
| 97 | /** @var Member[] */ |
||
| 98 | private $groups; |
||
| 99 | |||
| 100 | /** @var FederatedLink[] */ |
||
| 101 | private $links; |
||
| 102 | |||
| 103 | public function __construct($type = -1, $name = '') { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param integer $id |
||
| 116 | * |
||
| 117 | * @return BaseCircle |
||
| 118 | */ |
||
| 119 | public function setId($id) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return integer |
||
| 127 | */ |
||
| 128 | public function getId() { |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $uniqueId |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function setUniqueId($uniqueId) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param bool $full |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function getUniqueId($full = false) { |
||
| 156 | |||
| 157 | public function generateUniqueId() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $name |
||
| 165 | * |
||
| 166 | * @return BaseCircle |
||
| 167 | */ |
||
| 168 | public function setName($name) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param bool $real |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getName(bool $real = false) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $name |
||
| 189 | * |
||
| 190 | * @return BaseCircle |
||
| 191 | */ |
||
| 192 | public function setAltName($name) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getAltName() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return Member |
||
| 207 | */ |
||
| 208 | public function getOwner() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param Member $owner |
||
| 214 | * |
||
| 215 | * @return BaseCircle |
||
| 216 | */ |
||
| 217 | public function setOwner($owner) { |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @return Member |
||
| 226 | */ |
||
| 227 | public function getViewer() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param Member $user |
||
| 233 | * |
||
| 234 | * @return BaseCircle |
||
| 235 | */ |
||
| 236 | public function setViewer($user) { |
||
| 241 | |||
| 242 | |||
| 243 | public function hasViewer(): bool { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @return Member |
||
| 250 | */ |
||
| 251 | public function getGroupViewer() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param Member $group |
||
| 257 | * |
||
| 258 | * @return BaseCircle |
||
| 259 | */ |
||
| 260 | public function setGroupViewer($group) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return Member |
||
| 268 | */ |
||
| 269 | public function getHigherViewer() { |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $description |
||
| 291 | * |
||
| 292 | * @return BaseCircle |
||
| 293 | */ |
||
| 294 | public function setDescription($description) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function getDescription() { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * @param int $contactAddressBook |
||
| 310 | * |
||
| 311 | * @return BaseCircle |
||
| 312 | */ |
||
| 313 | public function setContactAddressBook(int $contactAddressBook) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return int |
||
| 321 | */ |
||
| 322 | public function getContactAddressBook() { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $contactGroupName |
||
| 329 | * |
||
| 330 | * @return BaseCircle |
||
| 331 | */ |
||
| 332 | public function setContactGroupName($contactGroupName) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getContactGroupName() { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string|array $settings |
||
| 348 | * |
||
| 349 | * @return $this |
||
| 350 | */ |
||
| 351 | public function setSettings($settings) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param bool $json |
||
| 363 | * |
||
| 364 | * @return array|string |
||
| 365 | */ |
||
| 366 | public function getSettings($json = false) { |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $k |
||
| 390 | * @param mixed $v |
||
| 391 | */ |
||
| 392 | public function setSetting($k, $v) { |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $k |
||
| 415 | * |
||
| 416 | * @return string|null |
||
| 417 | */ |
||
| 418 | public function getSetting($k) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * |
||
| 431 | * @param string $type |
||
| 432 | * |
||
| 433 | * @return \OCA\Circles\Model\BaseCircle |
||
| 434 | */ |
||
| 435 | public function setType($type) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return string |
||
|
|
|||
| 443 | */ |
||
| 444 | public function getType() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $creation |
||
| 450 | * |
||
| 451 | * @return \OCA\Circles\Model\BaseCircle |
||
| 452 | */ |
||
| 453 | public function setCreation($creation) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getCreation() { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param array $members |
||
| 468 | * |
||
| 469 | * @return BaseCircle |
||
| 470 | */ |
||
| 471 | public function setMembers($members) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return Member[] |
||
| 479 | */ |
||
| 480 | public function getMembers() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param array $groups |
||
| 486 | * |
||
| 487 | * @return BaseCircle |
||
| 488 | */ |
||
| 489 | public function setGroups($groups) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | public function getGroups() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param array $links |
||
| 504 | * |
||
| 505 | * @return BaseCircle |
||
| 506 | */ |
||
| 507 | public function setLinks($links) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return array |
||
| 515 | */ |
||
| 516 | public function getLinks() { |
||
| 519 | |||
| 520 | |||
| 521 | // public function getRemote() { |
||
| 522 | // return $this->remote; |
||
| 523 | // } |
||
| 524 | // |
||
| 525 | // public function addRemote($link) { |
||
| 526 | // array_push($this->remote, $link); |
||
| 527 | // } |
||
| 528 | // |
||
| 529 | // public function getRemoteFromToken($token) { |
||
| 530 | // foreach ($this->links AS $link) { |
||
| 531 | // if ($link->getToken() === $token) { |
||
| 532 | // return $link; |
||
| 533 | // } |
||
| 534 | // } |
||
| 535 | // |
||
| 536 | // return null; |
||
| 537 | // } |
||
| 538 | // |
||
| 539 | // public function getRemoteFromAddressAndId($address, $id) { |
||
| 540 | // foreach ($this->links AS $link) { |
||
| 541 | // if ($link->getAddress() === $address && $link->getUniqueId() === $id) { |
||
| 542 | // return $link; |
||
| 543 | // } |
||
| 544 | // } |
||
| 545 | // |
||
| 546 | // return null; |
||
| 547 | // } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param integer|string $type |
||
| 551 | * |
||
| 552 | * @return integer |
||
| 553 | */ |
||
| 554 | public static function typeInt($type) { |
||
| 573 | } |
||
| 574 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.