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 |
||
| 73 | class Circle extends ManagedModel implements IDeserializable, INC21QueryRow, JsonSerializable { |
||
| 74 | |||
| 75 | |||
| 76 | use TArrayTools; |
||
| 77 | use TNC21Deserialize; |
||
| 78 | |||
| 79 | |||
| 80 | const TYPES_SHORT = 1; |
||
| 81 | const TYPES_LONG = 2; |
||
| 82 | |||
| 83 | |||
| 84 | // specific value |
||
| 85 | const CFG_CIRCLE = 0; // only for code readability. Circle is locked by default. |
||
| 86 | const CFG_SINGLE = 1; // Circle with only one single member. |
||
| 87 | const CFG_PERSONAL = 2; // Personal circle, only the owner can see it. |
||
| 88 | |||
| 89 | // bitwise |
||
| 90 | const CFG_SYSTEM = 4; // System Circl (not managed by the official front-end). Meaning some config are limited |
||
| 91 | const CFG_VISIBLE = 8; // Visible to everyone, if not visible, people have to know its name to be able to find it |
||
| 92 | const CFG_OPEN = 16; // Circle is open, people can join |
||
| 93 | const CFG_INVITE = 32; // Adding a member generate an invitation that needs to be accepted |
||
| 94 | const CFG_REQUEST = 64; // Request to join Circles needs to be confirmed by a moderator |
||
| 95 | const CFG_FRIEND = 128; // Members of the circle can invite their friends |
||
| 96 | const CFG_PROTECTED = 256; // Password protected to join/request |
||
| 97 | const CFG_NO_OWNER = 512; // no owner, only members |
||
| 98 | const CFG_HIDDEN = 1024; // hidden from listing, but available as a share entity |
||
| 99 | const CFG_BACKEND = 2048; // Fully hidden, only backend Circles |
||
| 100 | const CFG_ROOT = 4096; // Circle cannot be inside another Circle |
||
| 101 | const CFG_FEDERATED = 8192; // Federated |
||
| 102 | |||
| 103 | |||
| 104 | public static $DEF_CFG_MAX = 16383; |
||
| 105 | |||
| 106 | public static $DEF_CFG = [ |
||
| 107 | 1 => 'S|Single', |
||
| 108 | 2 => 'P|Personal', |
||
| 109 | 4 => 'Y|System', |
||
| 110 | 8 => 'V|Visible', |
||
| 111 | 16 => 'O|Open', |
||
| 112 | 32 => 'I|Invite', |
||
| 113 | 64 => 'JR|Join Request', |
||
| 114 | 128 => 'F|Friends', |
||
| 115 | 256 => 'PP|Password Protected', |
||
| 116 | 512 => 'NO|No Owner', |
||
| 117 | 1024 => 'H|Hidden', |
||
| 118 | 2048 => 'T|Backend', |
||
| 119 | 4096 => 'T|Root', |
||
| 120 | 8192 => 'F|Federated' |
||
| 121 | ]; |
||
| 122 | |||
| 123 | public static $DEF_CFG_CORE_FILTER = [ |
||
| 124 | 1, |
||
| 125 | 2, |
||
| 126 | 4 |
||
| 127 | ]; |
||
| 128 | |||
| 129 | public static $DEF_CFG_SYSTEM_FILTER = [ |
||
| 130 | 512, |
||
| 131 | 1024, |
||
| 132 | 2048, |
||
| 133 | 4096 |
||
| 134 | ]; |
||
| 135 | |||
| 136 | |||
| 137 | /** @var string */ |
||
| 138 | private $id = ''; |
||
| 139 | |||
| 140 | /** @var int */ |
||
| 141 | private $config = 0; |
||
| 142 | |||
| 143 | /** @var int */ |
||
| 144 | private $type = 0; |
||
| 145 | |||
| 146 | /** @var string */ |
||
| 147 | private $name = ''; |
||
| 148 | |||
| 149 | /** @var string */ |
||
| 150 | private $altName = ''; |
||
| 151 | |||
| 152 | /** @var Member */ |
||
| 153 | private $owner; |
||
| 154 | |||
| 155 | /** @var array */ |
||
| 156 | private $members = []; |
||
| 157 | |||
| 158 | /** @var Member */ |
||
| 159 | private $initiator; |
||
| 160 | |||
| 161 | /** @var array */ |
||
| 162 | private $settings = []; |
||
| 163 | |||
| 164 | /** @var string */ |
||
| 165 | private $description = ''; |
||
| 166 | |||
| 167 | /** @var int */ |
||
| 168 | private $contactAddressBook = 0; |
||
| 169 | |||
| 170 | /** @var string */ |
||
| 171 | private $contactGroupName = ''; |
||
| 172 | |||
| 173 | /** @var string */ |
||
| 174 | private $instance = ''; |
||
| 175 | |||
| 176 | // /** @var bool */ |
||
| 177 | // private $hidden = false; |
||
| 178 | |||
| 179 | /** @var int */ |
||
| 180 | private $creation = 0; |
||
| 181 | |||
| 182 | |||
| 183 | /** @var Circle[] */ |
||
| 184 | private $memberOf = null; |
||
| 185 | |||
| 186 | private $completeJson = false; |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Circle constructor. |
||
| 191 | */ |
||
| 192 | public function __construct() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $id |
||
| 197 | * |
||
| 198 | * @return self |
||
| 199 | */ |
||
| 200 | public function setId(string $id): self { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getId(): string { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @param int $config |
||
| 216 | * |
||
| 217 | * @return self |
||
| 218 | */ |
||
| 219 | public function setConfig(int $config): self { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return int |
||
| 235 | */ |
||
| 236 | public function getConfig(): int { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param int $flag |
||
| 242 | * @param int $test |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function isConfig(int $flag, int $test = 0): bool { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param int $flag |
||
| 256 | */ |
||
| 257 | public function addConfig(int $flag): void { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param int $flag |
||
| 265 | */ |
||
| 266 | public function remConfig(int $flag): void { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $name |
||
| 275 | * |
||
| 276 | * @return self |
||
| 277 | */ |
||
| 278 | public function setName(string $name): self { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getName(): string { |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $altName |
||
| 294 | * |
||
| 295 | * @return self |
||
| 296 | */ |
||
| 297 | public function setAltName(string $altName): self { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getAltName(): string { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @param Member $owner |
||
| 313 | * |
||
| 314 | * @return self |
||
| 315 | */ |
||
| 316 | public function setOwner(Member $owner): self { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return Member |
||
| 324 | */ |
||
| 325 | public function getOwner(): Member { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function hasOwner(): bool { |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * @param array $members |
||
| 339 | * |
||
| 340 | * @return self |
||
| 341 | */ |
||
| 342 | public function setMembers(array $members): self { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | public function getMembers(): array { |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * @param Member $initiator |
||
| 362 | * |
||
| 363 | * @return Circle |
||
| 364 | */ |
||
| 365 | public function setInitiator(Member $initiator): self { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return Member |
||
| 373 | */ |
||
| 374 | public function getInitiator(): Member { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | public function hasInitiator(): bool { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $instance |
||
| 387 | * |
||
| 388 | * @return Circle |
||
| 389 | */ |
||
| 390 | public function setInstance(string $instance): self { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return string |
||
| 400 | * @throws OwnerNotFoundException |
||
| 401 | */ |
||
| 402 | public function getInstance(): string { |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * @param array $settings |
||
| 417 | * |
||
| 418 | * @return self |
||
| 419 | */ |
||
| 420 | public function setSettings(array $settings): self { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function getSettings(): array { |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $description |
||
| 436 | * |
||
| 437 | * @return self |
||
| 438 | */ |
||
| 439 | public function setDescription(string $description): self { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getDescription(): string { |
||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * @param int $contactAddressBook |
||
| 455 | * |
||
| 456 | * @return self |
||
| 457 | */ |
||
| 458 | public function setContactAddressBook(int $contactAddressBook): self { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return int |
||
| 466 | */ |
||
| 467 | public function getContactAddressBook(): int { |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $contactGroupName |
||
| 474 | * |
||
| 475 | * @return self |
||
| 476 | */ |
||
| 477 | public function setContactGroupName(string $contactGroupName): self { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public function getContactGroupName(): string { |
||
| 489 | |||
| 490 | |||
| 491 | // /** |
||
| 492 | // * @param bool $hidden |
||
| 493 | // * |
||
| 494 | // * @return Circle |
||
| 495 | // */ |
||
| 496 | // public function setHidden(bool $hidden): self { |
||
| 497 | // $this->hidden = $hidden; |
||
| 498 | // |
||
| 499 | // return $this; |
||
| 500 | // } |
||
| 501 | // |
||
| 502 | // /** |
||
| 503 | // * @return bool |
||
| 504 | // */ |
||
| 505 | // public function isHidden(): bool { |
||
| 506 | // return $this->hidden; |
||
| 507 | // } |
||
| 508 | |||
| 509 | |||
| 510 | /** |
||
| 511 | * @param array $memberOf |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function setMemberOf(array $memberOf): self { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @return Circle[] |
||
| 523 | */ |
||
| 524 | public function memberOf(): array { |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * @param int $creation |
||
| 535 | * |
||
| 536 | * @return self |
||
| 537 | */ |
||
| 538 | public function setCreation(int $creation): self { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return int |
||
| 546 | */ |
||
| 547 | public function getCreation(): int { |
||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * @param array $data |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | * @throws InvalidItemException |
||
| 557 | */ |
||
| 558 | public function import(array $data): IDeserializable { |
||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | public function jsonSerialize(): array { |
||
| 621 | |||
| 622 | |||
| 623 | /** |
||
| 624 | * @param array $data |
||
| 625 | * @param string $prefix |
||
| 626 | * |
||
| 627 | * @return INC21QueryRow |
||
| 628 | * @throws CircleNotFoundException |
||
| 629 | */ |
||
| 630 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
| 653 | |||
| 654 | |||
| 655 | /** |
||
| 656 | * @param Circle $circle |
||
| 657 | * |
||
| 658 | * @return bool |
||
| 659 | * @throws OwnerNotFoundException |
||
| 660 | */ |
||
| 661 | public function compareWith(Circle $circle): bool { |
||
| 682 | |||
| 683 | |||
| 684 | /** |
||
| 685 | * @param Circle $circle |
||
| 686 | * @param int $display |
||
| 687 | * |
||
| 688 | * @return array |
||
| 689 | */ |
||
| 690 | public static function getCircleTypes(Circle $circle, int $display = self::TYPES_LONG): array { |
||
| 710 | |||
| 711 | } |
||
| 712 | |||
| 713 |