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 |
||
| 32 | class BaseCircle { |
||
| 33 | |||
| 34 | const CIRCLES_SETTINGS_DEFAULT = [ |
||
| 35 | 'allow_links' => 'false', |
||
| 36 | 'allow_links_auto' => 'false', |
||
| 37 | 'allow_links_files' => 'false' |
||
| 38 | ]; |
||
| 39 | |||
| 40 | const CIRCLES_PERSONAL = 1; |
||
| 41 | const CIRCLES_SECRET = 2; |
||
| 42 | const CIRCLES_CLOSED = 4; |
||
| 43 | const CIRCLES_PUBLIC = 8; |
||
| 44 | |||
| 45 | const CIRCLES_ALL = 15; |
||
| 46 | |||
| 47 | const SHORT_UNIQUE_ID_LENGTH = 14; |
||
| 48 | |||
| 49 | /** @var int */ |
||
| 50 | private $id; |
||
| 51 | |||
| 52 | /** @var IL10N */ |
||
| 53 | protected $l10n; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $uniqueId = ''; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | private $name; |
||
| 60 | |||
| 61 | /** @var Member */ |
||
| 62 | private $owner; |
||
| 63 | |||
| 64 | /** @var Member */ |
||
| 65 | private $viewer = null; |
||
| 66 | |||
| 67 | /** @var Member */ |
||
| 68 | private $viewerGroup; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | private $description = ''; |
||
| 72 | |||
| 73 | /** @var array */ |
||
| 74 | private $settings = []; |
||
| 75 | |||
| 76 | /** @var int */ |
||
| 77 | private $type; |
||
| 78 | |||
| 79 | /** @var string */ |
||
| 80 | private $contactGroupName = ''; |
||
| 81 | |||
| 82 | /** @var int */ |
||
| 83 | private $contactAddressBook = 0; |
||
| 84 | |||
| 85 | |||
| 86 | /** @var string */ |
||
| 87 | private $creation; |
||
| 88 | |||
| 89 | /** @var Member[] */ |
||
| 90 | private $members; |
||
| 91 | |||
| 92 | /** @var Member[] */ |
||
| 93 | private $groups; |
||
| 94 | |||
| 95 | /** @var FederatedLink[] */ |
||
| 96 | private $links; |
||
| 97 | |||
| 98 | public function __construct($type = -1, $name = '') { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param integer $id |
||
| 111 | * |
||
| 112 | * @return BaseCircle |
||
| 113 | */ |
||
| 114 | public function setId($id) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return integer |
||
| 122 | */ |
||
| 123 | public function getId() { |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $uniqueId |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function setUniqueId($uniqueId) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param bool $full |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getUniqueId($full = false) { |
||
| 151 | |||
| 152 | public function generateUniqueId() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $name |
||
| 160 | * |
||
| 161 | * @return BaseCircle |
||
| 162 | */ |
||
| 163 | public function setName($name) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getName() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return Member |
||
| 178 | */ |
||
| 179 | public function getOwner() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param Member $owner |
||
| 185 | * |
||
| 186 | * @return BaseCircle |
||
| 187 | */ |
||
| 188 | public function setOwner($owner) { |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @return Member |
||
| 197 | */ |
||
| 198 | public function getViewer() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param Member $user |
||
| 204 | * |
||
| 205 | * @return BaseCircle |
||
| 206 | */ |
||
| 207 | public function setViewer($user) { |
||
| 212 | |||
| 213 | |||
| 214 | public function hasViewer(): bool { |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * @return Member |
||
| 221 | */ |
||
| 222 | public function getGroupViewer() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Member $group |
||
| 228 | * |
||
| 229 | * @return BaseCircle |
||
| 230 | */ |
||
| 231 | public function setGroupViewer($group) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return Member |
||
| 239 | */ |
||
| 240 | public function getHigherViewer() { |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $description |
||
| 262 | * |
||
| 263 | * @return BaseCircle |
||
| 264 | */ |
||
| 265 | public function setDescription($description) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getDescription() { |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * @param int $contactAddressBook |
||
| 281 | * |
||
| 282 | * @return BaseCircle |
||
| 283 | */ |
||
| 284 | public function setContactAddressBook(int $contactAddressBook) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return int |
||
| 292 | */ |
||
| 293 | public function getContactAddressBook() { |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $contactGroupName |
||
| 300 | * |
||
| 301 | * @return BaseCircle |
||
| 302 | */ |
||
| 303 | public function setContactGroupName($contactGroupName) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getContactGroupName() { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string|array $settings |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function setSettings($settings) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param bool $json |
||
| 334 | * |
||
| 335 | * @return array|string |
||
| 336 | */ |
||
| 337 | public function getSettings($json = false) { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $k |
||
| 361 | * @param mixed $v |
||
| 362 | */ |
||
| 363 | public function setSetting($k, $v) { |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $k |
||
| 382 | * |
||
| 383 | * @return string|null |
||
| 384 | */ |
||
| 385 | public function getSetting($k) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * |
||
| 398 | * @param string $type |
||
| 399 | * |
||
| 400 | * @return \OCA\Circles\Model\BaseCircle |
||
| 401 | */ |
||
| 402 | public function setType($type) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return string |
||
|
|
|||
| 410 | */ |
||
| 411 | public function getType() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $creation |
||
| 417 | * |
||
| 418 | * @return \OCA\Circles\Model\BaseCircle |
||
| 419 | */ |
||
| 420 | public function setCreation($creation) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getCreation() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param array $members |
||
| 435 | * |
||
| 436 | * @return BaseCircle |
||
| 437 | */ |
||
| 438 | public function setMembers($members) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return Member[] |
||
| 446 | */ |
||
| 447 | public function getMembers() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param array $groups |
||
| 453 | * |
||
| 454 | * @return BaseCircle |
||
| 455 | */ |
||
| 456 | public function setGroups($groups) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | public function getGroups() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param array $links |
||
| 471 | * |
||
| 472 | * @return BaseCircle |
||
| 473 | */ |
||
| 474 | public function setLinks($links) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | public function getLinks() { |
||
| 486 | |||
| 487 | |||
| 488 | // public function getRemote() { |
||
| 489 | // return $this->remote; |
||
| 490 | // } |
||
| 491 | // |
||
| 492 | // public function addRemote($link) { |
||
| 493 | // array_push($this->remote, $link); |
||
| 494 | // } |
||
| 495 | // |
||
| 496 | // public function getRemoteFromToken($token) { |
||
| 497 | // foreach ($this->links AS $link) { |
||
| 498 | // if ($link->getToken() === $token) { |
||
| 499 | // return $link; |
||
| 500 | // } |
||
| 501 | // } |
||
| 502 | // |
||
| 503 | // return null; |
||
| 504 | // } |
||
| 505 | // |
||
| 506 | // public function getRemoteFromAddressAndId($address, $id) { |
||
| 507 | // foreach ($this->links AS $link) { |
||
| 508 | // if ($link->getAddress() === $address && $link->getUniqueId() === $id) { |
||
| 509 | // return $link; |
||
| 510 | // } |
||
| 511 | // } |
||
| 512 | // |
||
| 513 | // return null; |
||
| 514 | // } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param integer|string $type |
||
| 518 | * |
||
| 519 | * @return integer |
||
| 520 | */ |
||
| 521 | public static function typeInt($type) { |
||
| 540 | } |
||
| 541 |
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.