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 | 'password_single' => '', |
||
| 38 | 'allow_links' => 'false', |
||
| 39 | 'allow_links_auto' => 'false', |
||
| 40 | 'allow_links_files' => 'false' |
||
| 41 | ]; |
||
| 42 | |||
| 43 | const CIRCLES_PERSONAL = 1; |
||
| 44 | const CIRCLES_SECRET = 2; |
||
| 45 | const CIRCLES_CLOSED = 4; |
||
| 46 | const CIRCLES_PUBLIC = 8; |
||
| 47 | |||
| 48 | const CIRCLES_ALL = 15; |
||
| 49 | |||
| 50 | const SHORT_UNIQUE_ID_LENGTH = 14; |
||
| 51 | |||
| 52 | /** @var int */ |
||
| 53 | private $id; |
||
| 54 | |||
| 55 | /** @var IL10N */ |
||
| 56 | protected $l10n; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | private $uniqueId = ''; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $name; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $altName = ''; |
||
| 66 | |||
| 67 | /** @var Member */ |
||
| 68 | private $owner; |
||
| 69 | |||
| 70 | /** @var Member */ |
||
| 71 | private $viewer = null; |
||
| 72 | |||
| 73 | /** @var Member */ |
||
| 74 | private $viewerGroup; |
||
| 75 | |||
| 76 | /** @var string */ |
||
| 77 | private $description = ''; |
||
| 78 | |||
| 79 | /** @var array */ |
||
| 80 | private $settings = []; |
||
| 81 | |||
| 82 | /** @var string */ |
||
| 83 | private $passwordSingle = ''; |
||
| 84 | |||
| 85 | /** @var int */ |
||
| 86 | private $type; |
||
| 87 | |||
| 88 | /** @var string */ |
||
| 89 | private $contactGroupName = ''; |
||
| 90 | |||
| 91 | /** @var int */ |
||
| 92 | private $contactAddressBook = 0; |
||
| 93 | |||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $creation; |
||
| 97 | |||
| 98 | /** @var Member[] */ |
||
| 99 | private $members; |
||
| 100 | |||
| 101 | /** @var Member[] */ |
||
| 102 | private $groups; |
||
| 103 | |||
| 104 | /** @var FederatedLink[] */ |
||
| 105 | private $links; |
||
| 106 | |||
| 107 | public function __construct($type = -1, $name = '') { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param integer $id |
||
| 120 | * |
||
| 121 | * @return BaseCircle |
||
| 122 | */ |
||
| 123 | public function setId($id) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return integer |
||
| 131 | */ |
||
| 132 | public function getId() { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $uniqueId |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function setUniqueId($uniqueId) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param bool $full |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getUniqueId($full = false) { |
||
| 160 | |||
| 161 | public function generateUniqueId() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $name |
||
| 169 | * |
||
| 170 | * @return BaseCircle |
||
| 171 | */ |
||
| 172 | public function setName($name) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param bool $real |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getName(bool $real = false) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $name |
||
| 193 | * |
||
| 194 | * @return BaseCircle |
||
| 195 | */ |
||
| 196 | public function setAltName($name) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getAltName() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return Member |
||
| 211 | */ |
||
| 212 | public function getOwner() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Member $owner |
||
| 218 | * |
||
| 219 | * @return BaseCircle |
||
| 220 | */ |
||
| 221 | public function setOwner($owner) { |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * @return Member |
||
| 230 | */ |
||
| 231 | public function getViewer() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param Member $user |
||
| 237 | * |
||
| 238 | * @return BaseCircle |
||
| 239 | */ |
||
| 240 | public function setViewer($user) { |
||
| 245 | |||
| 246 | |||
| 247 | public function hasViewer(): bool { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * @return Member |
||
| 254 | */ |
||
| 255 | public function getGroupViewer() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param Member $group |
||
| 261 | * |
||
| 262 | * @return BaseCircle |
||
| 263 | */ |
||
| 264 | public function setGroupViewer($group) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return Member |
||
| 272 | */ |
||
| 273 | public function getHigherViewer() { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $description |
||
| 295 | * |
||
| 296 | * @return BaseCircle |
||
| 297 | */ |
||
| 298 | public function setDescription($description) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function getDescription() { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * @param int $contactAddressBook |
||
| 314 | * |
||
| 315 | * @return BaseCircle |
||
| 316 | */ |
||
| 317 | public function setContactAddressBook(int $contactAddressBook) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return int |
||
| 325 | */ |
||
| 326 | public function getContactAddressBook() { |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $contactGroupName |
||
| 333 | * |
||
| 334 | * @return BaseCircle |
||
| 335 | */ |
||
| 336 | public function setContactGroupName($contactGroupName) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function getContactGroupName() { |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string|array $settings |
||
| 352 | * @param bool $all |
||
| 353 | * |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | public function setSettings($settings, bool $all = false) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param bool $json |
||
| 375 | * |
||
| 376 | * @return array|string |
||
| 377 | */ |
||
| 378 | public function getSettings($json = false) { |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $k |
||
| 402 | * @param mixed $v |
||
| 403 | */ |
||
| 404 | public function setSetting($k, $v) { |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $k |
||
| 427 | * |
||
| 428 | * @return string|null |
||
| 429 | */ |
||
| 430 | public function getSetting($k) { |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public function getPasswordSingle(): string { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param string $passwordSingle |
||
| 451 | */ |
||
| 452 | public function setPasswordSingle(string $passwordSingle): void { |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * |
||
| 459 | * @param string $type |
||
| 460 | * |
||
| 461 | * @return \OCA\Circles\Model\BaseCircle |
||
| 462 | */ |
||
| 463 | public function setType($type) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
|
|
|||
| 471 | */ |
||
| 472 | public function getType() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $creation |
||
| 478 | * |
||
| 479 | * @return \OCA\Circles\Model\BaseCircle |
||
| 480 | */ |
||
| 481 | public function setCreation($creation) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | public function getCreation() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param array $members |
||
| 496 | * |
||
| 497 | * @return BaseCircle |
||
| 498 | */ |
||
| 499 | public function setMembers($members) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return Member[] |
||
| 507 | */ |
||
| 508 | public function getMembers() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param array $groups |
||
| 514 | * |
||
| 515 | * @return BaseCircle |
||
| 516 | */ |
||
| 517 | public function setGroups($groups) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function getGroups() { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param array $links |
||
| 532 | * |
||
| 533 | * @return BaseCircle |
||
| 534 | */ |
||
| 535 | public function setLinks($links) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | public function getLinks() { |
||
| 547 | |||
| 548 | |||
| 549 | // public function getRemote() { |
||
| 550 | // return $this->remote; |
||
| 551 | // } |
||
| 552 | // |
||
| 553 | // public function addRemote($link) { |
||
| 554 | // array_push($this->remote, $link); |
||
| 555 | // } |
||
| 556 | // |
||
| 557 | // public function getRemoteFromToken($token) { |
||
| 558 | // foreach ($this->links AS $link) { |
||
| 559 | // if ($link->getToken() === $token) { |
||
| 560 | // return $link; |
||
| 561 | // } |
||
| 562 | // } |
||
| 563 | // |
||
| 564 | // return null; |
||
| 565 | // } |
||
| 566 | // |
||
| 567 | // public function getRemoteFromAddressAndId($address, $id) { |
||
| 568 | // foreach ($this->links AS $link) { |
||
| 569 | // if ($link->getAddress() === $address && $link->getUniqueId() === $id) { |
||
| 570 | // return $link; |
||
| 571 | // } |
||
| 572 | // } |
||
| 573 | // |
||
| 574 | // return null; |
||
| 575 | // } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param integer|string $type |
||
| 579 | * |
||
| 580 | * @return integer |
||
| 581 | */ |
||
| 582 | public static function typeInt($type) { |
||
| 601 | } |
||
| 602 |
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.