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 | // specific value |
||
| 81 | const CFG_CIRCLE = 0; // only for code readability. Circle is locked by default. |
||
| 82 | const CFG_SINGLE = 1; // Circle with only one single member. |
||
| 83 | const CFG_PERSONAL = 2; // Personal circle, only the owner can see it. |
||
| 84 | |||
| 85 | // bitwise |
||
| 86 | const CFG_VISIBLE = 8; // Visible to everyone, if not visible, people have to know its name to be able to find it |
||
| 87 | const CFG_OPEN = 16; // Circle is open, people can join |
||
| 88 | const CFG_INVITE = 32; // Adding a member generate an invitation that needs to be accepted |
||
| 89 | const CFG_REQUEST = 64; // Request to join Circles needs to be confirmed by a moderator |
||
| 90 | const CFG_FRIEND = 128; // Members of the circle can invite their friends |
||
| 91 | const CFG_PROTECTED = 256; // Password protected to join/request |
||
| 92 | const CFG_NO_OWNER = 512; // no owner, only members |
||
| 93 | const CFG_HIDDEN = 1024; // hidden from listing, but available as a share entity |
||
| 94 | const CFG_BACKEND = 2048; // Fully hidden, only backend Circles |
||
| 95 | const CFG_ROOT = 4096; // Circle cannot be inside another Circle |
||
| 96 | const CFG_FEDERATED = 8192; // Federated |
||
| 97 | |||
| 98 | static $DEF = [ |
||
| 99 | 1 => 'S|Single', |
||
| 100 | |||
| 101 | 2 => 'P|Personal', |
||
| 102 | 8 => 'V|Visible', |
||
| 103 | 16 => 'O|Open', |
||
| 104 | 32 => 'I|Invite', |
||
| 105 | 64 => 'JR|Join Request', |
||
| 106 | 128 => 'F|Friends', |
||
| 107 | 256 => 'PP|Password Protected', |
||
| 108 | 512 => 'NO|No Owner', |
||
| 109 | 1024 => 'H|Hidden', |
||
| 110 | 2048 => 'T|Backend', |
||
| 111 | 4096 => 'T|Root', |
||
| 112 | 8192 => 'F|Federated' |
||
| 113 | ]; |
||
| 114 | |||
| 115 | |||
| 116 | /** @var string */ |
||
| 117 | private $id = ''; |
||
| 118 | |||
| 119 | /** @var int */ |
||
| 120 | private $config = 0; |
||
| 121 | |||
| 122 | /** @var int */ |
||
| 123 | private $type = 0; |
||
| 124 | |||
| 125 | /** @var string */ |
||
| 126 | private $name = ''; |
||
| 127 | |||
| 128 | /** @var string */ |
||
| 129 | private $altName = ''; |
||
| 130 | |||
| 131 | /** @var Member */ |
||
| 132 | private $owner; |
||
| 133 | |||
| 134 | /** @var array */ |
||
| 135 | private $members = []; |
||
| 136 | |||
| 137 | /** @var Member */ |
||
| 138 | private $initiator; |
||
| 139 | |||
| 140 | /** @var array */ |
||
| 141 | private $settings = []; |
||
| 142 | |||
| 143 | /** @var string */ |
||
| 144 | private $description = ''; |
||
| 145 | |||
| 146 | /** @var int */ |
||
| 147 | private $contactAddressBook = 0; |
||
| 148 | |||
| 149 | /** @var string */ |
||
| 150 | private $contactGroupName = ''; |
||
| 151 | |||
| 152 | /** @var string */ |
||
| 153 | private $instance = ''; |
||
| 154 | |||
| 155 | // /** @var bool */ |
||
| 156 | // private $hidden = false; |
||
| 157 | |||
| 158 | /** @var int */ |
||
| 159 | private $creation = 0; |
||
| 160 | |||
| 161 | |||
| 162 | /** @var Circle[] */ |
||
| 163 | private $memberOf = null; |
||
| 164 | |||
| 165 | private $completeJson = false; |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Circle constructor. |
||
| 170 | */ |
||
| 171 | public function __construct() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $id |
||
| 176 | * |
||
| 177 | * @return self |
||
| 178 | */ |
||
| 179 | public function setId(string $id): self { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getId(): string { |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @param int $config |
||
| 195 | * |
||
| 196 | * @return self |
||
| 197 | */ |
||
| 198 | public function setConfig(int $config): self { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return int |
||
| 214 | */ |
||
| 215 | public function getConfig(): int { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param int $flag |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function isConfig(int $flag): bool { |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $name |
||
| 231 | * |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | public function setName(string $name): self { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getName(): string { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $altName |
||
| 250 | * |
||
| 251 | * @return self |
||
| 252 | */ |
||
| 253 | public function setAltName(string $altName): self { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getAltName(): string { |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * @param Member $owner |
||
| 269 | * |
||
| 270 | * @return self |
||
| 271 | */ |
||
| 272 | public function setOwner(Member $owner): self { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return Member |
||
| 280 | */ |
||
| 281 | public function getOwner(): Member { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | public function hasOwner(): bool { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $members |
||
| 295 | * |
||
| 296 | * @return self |
||
| 297 | */ |
||
| 298 | public function setMembers(array $members): self { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public function getMembers(): array { |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * @param Member $initiator |
||
| 318 | * |
||
| 319 | * @return Circle |
||
| 320 | */ |
||
| 321 | public function setInitiator(Member $initiator): self { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return Member |
||
| 329 | */ |
||
| 330 | public function getInitiator(): Member { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function hasInitiator(): bool { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $instance |
||
| 343 | * |
||
| 344 | * @return Circle |
||
| 345 | */ |
||
| 346 | public function setInstance(string $instance): self { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return string |
||
| 356 | * @throws OwnerNotFoundException |
||
| 357 | */ |
||
| 358 | public function getInstance(): string { |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * @param array $settings |
||
| 373 | * |
||
| 374 | * @return self |
||
| 375 | */ |
||
| 376 | public function setSettings(array $settings): self { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | public function getSettings(): array { |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $description |
||
| 392 | * |
||
| 393 | * @return self |
||
| 394 | */ |
||
| 395 | public function setDescription(string $description): self { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | public function getDescription(): string { |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * @param int $contactAddressBook |
||
| 411 | * |
||
| 412 | * @return self |
||
| 413 | */ |
||
| 414 | public function setContactAddressBook(int $contactAddressBook): self { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return int |
||
| 422 | */ |
||
| 423 | public function getContactAddressBook(): int { |
||
| 426 | |||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $contactGroupName |
||
| 430 | * |
||
| 431 | * @return self |
||
| 432 | */ |
||
| 433 | public function setContactGroupName(string $contactGroupName): self { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getContactGroupName(): string { |
||
| 445 | |||
| 446 | |||
| 447 | // /** |
||
| 448 | // * @param bool $hidden |
||
| 449 | // * |
||
| 450 | // * @return Circle |
||
| 451 | // */ |
||
| 452 | // public function setHidden(bool $hidden): self { |
||
| 453 | // $this->hidden = $hidden; |
||
| 454 | // |
||
| 455 | // return $this; |
||
| 456 | // } |
||
| 457 | // |
||
| 458 | // /** |
||
| 459 | // * @return bool |
||
| 460 | // */ |
||
| 461 | // public function isHidden(): bool { |
||
| 462 | // return $this->hidden; |
||
| 463 | // } |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * @param array $memberOf |
||
| 468 | * |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function setMemberOf(array $memberOf): self { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return Circle[] |
||
| 479 | */ |
||
| 480 | public function memberOf(): array { |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * @param int $creation |
||
| 491 | * |
||
| 492 | * @return self |
||
| 493 | */ |
||
| 494 | public function setCreation(int $creation): self { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @return int |
||
| 502 | */ |
||
| 503 | public function getCreation(): int { |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * @param array $data |
||
| 510 | * |
||
| 511 | * @return $this |
||
| 512 | * @throws InvalidItemException |
||
| 513 | */ |
||
| 514 | public function import(array $data): IDeserializable { |
||
| 546 | |||
| 547 | |||
| 548 | /** |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | public function jsonSerialize(): array { |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * @param array $data |
||
| 581 | * @param string $prefix |
||
| 582 | * |
||
| 583 | * @return INC21QueryRow |
||
| 584 | * @throws CircleNotFoundException |
||
| 585 | */ |
||
| 586 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
| 609 | |||
| 610 | |||
| 611 | /** |
||
| 612 | * @param Circle $circle |
||
| 613 | * |
||
| 614 | * @return bool |
||
| 615 | * @throws OwnerNotFoundException |
||
| 616 | */ |
||
| 617 | public function compareWith(Circle $circle): bool { |
||
| 638 | |||
| 639 | } |
||
| 640 | |||
| 641 |