Complex classes like Circle 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 Circle, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 72 | class Circle extends ManagedModel implements INC21Convert, INC21QueryRow, JsonSerializable { |
||
| 73 | |||
| 74 | |||
| 75 | use TArrayTools; |
||
| 76 | use TNC21Convert; |
||
| 77 | |||
| 78 | |||
| 79 | // specific value |
||
| 80 | const CFG_CIRCLE = 0; // only for code readability. Circle is locked by default. |
||
| 81 | const CFG_SINGLE = 1; // Circle with only one single member. |
||
| 82 | const CFG_PERSONAL = 2; // Personal circle, only the owner can see it. |
||
| 83 | |||
| 84 | // bitwise |
||
| 85 | const CFG_VISIBLE = 8; // Visible to everyone, if not visible, people have to know its name to be able to find it |
||
| 86 | const CFG_OPEN = 16; // Circle is open, people can join |
||
| 87 | const CFG_INVITE = 32; // Adding a member generate an invitation that needs to be accepted |
||
| 88 | const CFG_REQUEST = 64; // Request to join Circles needs to be confirmed by a moderator |
||
| 89 | const CFG_FRIEND = 128; // Members of the circle can invite their friends |
||
| 90 | const CFG_PROTECTED = 256; // Password protected to join/request |
||
| 91 | const CFG_NO_OWNER = 512; // no owner, only members |
||
| 92 | const CFG_HIDDEN = 1024; // Fully hidden, only backend Circles |
||
| 93 | const CFG_ROOT = 2048; // Circle cannot be inside another Circle |
||
| 94 | const CFG_FEDERATED = 4096; // Federated |
||
| 95 | |||
| 96 | const ID_LENGTH = 14; |
||
| 97 | |||
| 98 | static $DEF = [ |
||
| 99 | 1 => 'S|Single', |
||
| 100 | 2 => 'P|Personal', |
||
| 101 | 8 => 'V|Visible', |
||
| 102 | 16 => 'O|Open', |
||
| 103 | 32 => 'I|Invite', |
||
| 104 | 64 => 'JR|Join Request', |
||
| 105 | 128 => 'F|Friends', |
||
| 106 | 256 => 'PP|Password Protected', |
||
| 107 | 512 => 'NO|No Owner', |
||
| 108 | 1024 => 'H|Hidden', |
||
| 109 | 2048 => 'T|Root', |
||
| 110 | 4096 => 'F|Federated' |
||
| 111 | ]; |
||
| 112 | |||
| 113 | |||
| 114 | /** @var string */ |
||
| 115 | private $id = ''; |
||
| 116 | |||
| 117 | /** @var int */ |
||
| 118 | private $config = 0; |
||
| 119 | |||
| 120 | /** @var int */ |
||
| 121 | private $type = 0; |
||
| 122 | |||
| 123 | /** @var string */ |
||
| 124 | private $name = ''; |
||
| 125 | |||
| 126 | /** @var string */ |
||
| 127 | private $altName = ''; |
||
| 128 | |||
| 129 | /** @var Member */ |
||
| 130 | private $owner; |
||
| 131 | |||
| 132 | /** @var array */ |
||
| 133 | private $members = []; |
||
| 134 | |||
| 135 | /** @var Member */ |
||
| 136 | private $viewer; |
||
| 137 | |||
| 138 | /** @var array */ |
||
| 139 | private $settings = []; |
||
| 140 | |||
| 141 | /** @var string */ |
||
| 142 | private $description = ''; |
||
| 143 | |||
| 144 | /** @var int */ |
||
| 145 | private $contactAddressBook = 0; |
||
| 146 | |||
| 147 | /** @var string */ |
||
| 148 | private $contactGroupName = ''; |
||
| 149 | |||
| 150 | /** @var string */ |
||
| 151 | private $instance = ''; |
||
| 152 | |||
| 153 | // /** @var bool */ |
||
| 154 | // private $hidden = false; |
||
| 155 | |||
| 156 | /** @var int */ |
||
| 157 | private $creation = 0; |
||
| 158 | |||
| 159 | |||
| 160 | /** @var Circle[] */ |
||
| 161 | private $memberOf = null; |
||
| 162 | |||
| 163 | private $completeJson = false; |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * Circle constructor. |
||
| 168 | */ |
||
| 169 | public function __construct() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $id |
||
| 174 | * |
||
| 175 | * @return self |
||
| 176 | */ |
||
| 177 | public function setId(string $id): self { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getId(): string { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * @param int $config |
||
| 193 | * |
||
| 194 | * @return self |
||
| 195 | */ |
||
| 196 | public function setConfig(int $config): self { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public function getConfig(): int { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param int $flag |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function isConfig(int $flag): bool { |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $name |
||
| 229 | * |
||
| 230 | * @return self |
||
| 231 | */ |
||
| 232 | public function setName(string $name): self { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getName(): string { |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $altName |
||
| 248 | * |
||
| 249 | * @return self |
||
| 250 | */ |
||
| 251 | public function setAltName(string $altName): self { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function getAltName(): string { |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * @param Member $owner |
||
| 267 | * |
||
| 268 | * @return self |
||
| 269 | */ |
||
| 270 | public function setOwner(Member $owner): self { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return Member |
||
| 278 | */ |
||
| 279 | public function getOwner(): Member { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function hasOwner(): bool { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param array $members |
||
| 293 | * |
||
| 294 | * @return self |
||
| 295 | */ |
||
| 296 | public function setMembers(array $members): self { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function getMembers(): array { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @param Member $viewer |
||
| 316 | * |
||
| 317 | * @return Circle |
||
| 318 | */ |
||
| 319 | public function setViewer(Member $viewer): self { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return Member |
||
| 327 | */ |
||
| 328 | public function getViewer(): Member { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public function hasViewer(): bool { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $instance |
||
| 341 | * |
||
| 342 | * @return Circle |
||
| 343 | */ |
||
| 344 | public function setInstance(string $instance): self { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return string |
||
| 354 | * @throws OwnerNotFoundException |
||
| 355 | */ |
||
| 356 | public function getInstance(): string { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param array $settings |
||
| 371 | * |
||
| 372 | * @return self |
||
| 373 | */ |
||
| 374 | public function setSettings(array $settings): self { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function getSettings(): array { |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $description |
||
| 390 | * |
||
| 391 | * @return self |
||
| 392 | */ |
||
| 393 | public function setDescription(string $description): self { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function getDescription(): string { |
||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * @param int $contactAddressBook |
||
| 409 | * |
||
| 410 | * @return self |
||
| 411 | */ |
||
| 412 | public function setContactAddressBook(int $contactAddressBook): self { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return int |
||
| 420 | */ |
||
| 421 | public function getContactAddressBook(): int { |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * @param string $contactGroupName |
||
| 428 | * |
||
| 429 | * @return self |
||
| 430 | */ |
||
| 431 | public function setContactGroupName(string $contactGroupName): self { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getContactGroupName(): string { |
||
| 443 | |||
| 444 | |||
| 445 | // /** |
||
| 446 | // * @param bool $hidden |
||
| 447 | // * |
||
| 448 | // * @return Circle |
||
| 449 | // */ |
||
| 450 | // public function setHidden(bool $hidden): self { |
||
| 451 | // $this->hidden = $hidden; |
||
| 452 | // |
||
| 453 | // return $this; |
||
| 454 | // } |
||
| 455 | // |
||
| 456 | // /** |
||
| 457 | // * @return bool |
||
| 458 | // */ |
||
| 459 | // public function isHidden(): bool { |
||
| 460 | // return $this->hidden; |
||
| 461 | // } |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * @param array $memberOf |
||
| 466 | * |
||
| 467 | * @return $this |
||
| 468 | */ |
||
| 469 | public function setMemberOf(array $memberOf): self { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return Circle[] |
||
| 477 | */ |
||
| 478 | public function memberOf(): array { |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * @param int $creation |
||
| 489 | * |
||
| 490 | * @return self |
||
| 491 | */ |
||
| 492 | public function setCreation(int $creation): self { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @return int |
||
| 500 | */ |
||
| 501 | public function getCreation(): int { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * @param array $data |
||
| 508 | * |
||
| 509 | * @return $this |
||
| 510 | */ |
||
| 511 | public function import(array $data): INC21Convert { |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | public function jsonSerialize(): array { |
||
| 558 | |||
| 559 | |||
| 560 | /** |
||
| 561 | * @param array $data |
||
| 562 | * |
||
| 563 | * @return INC21QueryRow |
||
| 564 | */ |
||
| 565 | public function importFromDatabase(array $data): INC21QueryRow { |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * @param Circle $circle |
||
| 588 | * |
||
| 589 | * @return bool |
||
| 590 | */ |
||
| 591 | public function compareWith(Circle $circle): bool { |
||
| 605 | |||
| 606 | } |
||
| 607 |