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; |
||
| 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 | * @return Member |
||
| 215 | */ |
||
| 216 | public function getGroupViewer() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param Member $group |
||
| 222 | * |
||
| 223 | * @return BaseCircle |
||
| 224 | */ |
||
| 225 | public function setGroupViewer($group) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return Member |
||
| 233 | */ |
||
| 234 | public function getHigherViewer() { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $description |
||
| 256 | * |
||
| 257 | * @return BaseCircle |
||
| 258 | */ |
||
| 259 | public function setDescription($description) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getDescription() { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @param int $contactAddressBook |
||
| 275 | * |
||
| 276 | * @return BaseCircle |
||
| 277 | */ |
||
| 278 | public function setContactAddressBook(int $contactAddressBook) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return int |
||
| 286 | */ |
||
| 287 | public function getContactAddressBook() { |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $contactGroupName |
||
| 294 | * |
||
| 295 | * @return BaseCircle |
||
| 296 | */ |
||
| 297 | public function setContactGroupName($contactGroupName) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getContactGroupName() { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string|array $settings |
||
| 313 | * |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function setSettings($settings) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param bool $json |
||
| 328 | * |
||
| 329 | * @return array|string |
||
| 330 | */ |
||
| 331 | public function getSettings($json = false) { |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $k |
||
| 355 | * @param mixed $v |
||
| 356 | */ |
||
| 357 | public function setSetting($k, $v) { |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $k |
||
| 376 | * |
||
| 377 | * @return string|null |
||
| 378 | */ |
||
| 379 | public function getSetting($k) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * |
||
| 392 | * @param string $type |
||
| 393 | * |
||
| 394 | * @return \OCA\Circles\Model\BaseCircle |
||
| 395 | */ |
||
| 396 | public function setType($type) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return string |
||
|
|
|||
| 404 | */ |
||
| 405 | public function getType() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param string $creation |
||
| 411 | * |
||
| 412 | * @return \OCA\Circles\Model\BaseCircle |
||
| 413 | */ |
||
| 414 | public function setCreation($creation) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function getCreation() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param array $members |
||
| 429 | * |
||
| 430 | * @return BaseCircle |
||
| 431 | */ |
||
| 432 | public function setMembers($members) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getMembers() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param array $groups |
||
| 447 | * |
||
| 448 | * @return BaseCircle |
||
| 449 | */ |
||
| 450 | public function setGroups($groups) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | public function getGroups() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param array $links |
||
| 465 | * |
||
| 466 | * @return BaseCircle |
||
| 467 | */ |
||
| 468 | public function setLinks($links) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | public function getLinks() { |
||
| 480 | |||
| 481 | |||
| 482 | // public function getRemote() { |
||
| 483 | // return $this->remote; |
||
| 484 | // } |
||
| 485 | // |
||
| 486 | // public function addRemote($link) { |
||
| 487 | // array_push($this->remote, $link); |
||
| 488 | // } |
||
| 489 | // |
||
| 490 | // public function getRemoteFromToken($token) { |
||
| 491 | // foreach ($this->links AS $link) { |
||
| 492 | // if ($link->getToken() === $token) { |
||
| 493 | // return $link; |
||
| 494 | // } |
||
| 495 | // } |
||
| 496 | // |
||
| 497 | // return null; |
||
| 498 | // } |
||
| 499 | // |
||
| 500 | // public function getRemoteFromAddressAndId($address, $id) { |
||
| 501 | // foreach ($this->links AS $link) { |
||
| 502 | // if ($link->getAddress() === $address && $link->getUniqueId() === $id) { |
||
| 503 | // return $link; |
||
| 504 | // } |
||
| 505 | // } |
||
| 506 | // |
||
| 507 | // return null; |
||
| 508 | // } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param integer|string $type |
||
| 512 | * |
||
| 513 | * @return integer |
||
| 514 | */ |
||
| 515 | public static function typeInt($type) { |
||
| 534 | } |
||
| 535 |
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.