Complex classes like BaseMember 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 BaseMember, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class BaseMember implements \JsonSerializable { |
||
| 34 | |||
| 35 | const LEVEL_NONE = 0; |
||
| 36 | const LEVEL_MEMBER = 1; |
||
| 37 | const LEVEL_MODERATOR = 4; |
||
| 38 | const LEVEL_ADMIN = 8; |
||
| 39 | const LEVEL_OWNER = 9; |
||
| 40 | |||
| 41 | const STATUS_NONMEMBER = 'Unknown'; |
||
| 42 | const STATUS_INVITED = 'Invited'; |
||
| 43 | const STATUS_REQUEST = 'Requesting'; |
||
| 44 | const STATUS_MEMBER = 'Member'; |
||
| 45 | const STATUS_BLOCKED = 'Blocked'; |
||
| 46 | const STATUS_KICKED = 'Kicked'; |
||
| 47 | |||
| 48 | const TYPE_USER = 1; |
||
| 49 | const TYPE_GROUP = 2; |
||
| 50 | const TYPE_MAIL = 3; |
||
| 51 | const TYPE_CONTACT = 4; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $circleUniqueId; |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | private $circleContactGroupName; |
||
| 58 | |||
| 59 | /** @var IL10N */ |
||
| 60 | protected $l10n; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | private $userId = ''; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | private $memberId = ''; |
||
| 67 | |||
| 68 | /** @var int */ |
||
| 69 | private $type = self::TYPE_USER; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | private $displayName = ''; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | private $cachedName = ''; |
||
| 76 | |||
| 77 | /** @var int */ |
||
| 78 | private $level; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | private $status; |
||
| 82 | |||
| 83 | /** @var string */ |
||
| 84 | private $contactId = ''; |
||
| 85 | |||
| 86 | /** @var array */ |
||
| 87 | private $contactMeta = []; |
||
| 88 | |||
| 89 | /** @var string */ |
||
| 90 | private $note; |
||
| 91 | |||
| 92 | /** @var string */ |
||
| 93 | private $instance = ''; |
||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $joined = ''; |
||
| 97 | |||
| 98 | /** @var int */ |
||
| 99 | private $joinedSince; |
||
| 100 | |||
| 101 | /** @var bool */ |
||
| 102 | protected $broadcasting = true; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * BaseMember constructor. |
||
| 106 | * |
||
| 107 | * @param string $circleUniqueId |
||
| 108 | * @param string $userId |
||
| 109 | * @param int $type |
||
| 110 | */ |
||
| 111 | public function __construct($userId = '', $type = 0, $circleUniqueId = '') { |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $circleUniqueId |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function setCircleId($circleUniqueId) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getCircleId() { |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string $circleContactGroupName |
||
| 143 | * |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function setCircleContactGroupName($circleContactGroupName): self { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getCircleContactGroupName(): string { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | public function getType() { |
||
| 166 | |||
| 167 | public function setType($type) { |
||
| 170 | |||
| 171 | |||
| 172 | public function getViewerType() { |
||
| 179 | |||
| 180 | |||
| 181 | public function setUserId($userId) { |
||
| 187 | |||
| 188 | public function getUserId() { |
||
| 191 | |||
| 192 | |||
| 193 | public function setMemberId($memberId) { |
||
| 198 | |||
| 199 | public function getMemberId() { |
||
| 202 | |||
| 203 | |||
| 204 | public function setDisplayName($display) { |
||
| 209 | |||
| 210 | public function getDisplayName() { |
||
| 213 | |||
| 214 | public function setCachedName($display) { |
||
| 219 | |||
| 220 | public function getCachedName() { |
||
| 223 | |||
| 224 | public function setLevel($level) { |
||
| 229 | |||
| 230 | public function getLevel() { |
||
| 233 | |||
| 234 | |||
| 235 | public function setNote($note) { |
||
| 240 | |||
| 241 | public function getNote() { |
||
| 244 | |||
| 245 | |||
| 246 | public function setInstance($instance) { |
||
| 251 | |||
| 252 | public function getInstance() { |
||
| 255 | |||
| 256 | |||
| 257 | public function setContactId($contactId) { |
||
| 262 | |||
| 263 | public function getContactId() { |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * @param array $contactMeta |
||
| 270 | * |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | public function setContactMeta(array $contactMeta): self { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function getContactMeta(): array { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $k |
||
| 288 | * @param string $v |
||
| 289 | * |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function addContactMeta(string $k, string $v): self { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $k |
||
| 300 | * @param string $v |
||
| 301 | * |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function addContactMetaArray(string $k, string $v): self { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $k |
||
| 316 | * @param array $v |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function setContactMetaArray(string $k, array $v): self { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $status |
||
| 329 | * |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function setStatus($status) { |
||
| 341 | |||
| 342 | public function getStatus() { |
||
| 345 | |||
| 346 | |||
| 347 | public function setJoined($joined) { |
||
| 352 | |||
| 353 | public function getJoined() { |
||
| 356 | |||
| 357 | |||
| 358 | public function getJoinedSince(): int { |
||
| 361 | |||
| 362 | public function setJoinedSince(int $since) { |
||
| 365 | |||
| 366 | |||
| 367 | public function isLevel($level) { |
||
| 370 | |||
| 371 | |||
| 372 | public function isAlmostMember() { |
||
| 376 | |||
| 377 | |||
| 378 | protected function setAsAMember($level = 1) { |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $arr |
||
| 386 | * |
||
| 387 | * @return null|Member |
||
| 388 | */ |
||
| 389 | public static function fromArray($arr) { |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @param $json |
||
| 416 | * |
||
| 417 | * @return Member |
||
|
|
|||
| 418 | */ |
||
| 419 | public static function fromJSON($json) { |
||
| 422 | |||
| 423 | |||
| 424 | public function jsonSerialize() { |
||
| 441 | |||
| 442 | public function getLevelString() { |
||
| 458 | |||
| 459 | |||
| 460 | public function getTypeString() { |
||
| 474 | |||
| 475 | public function getTypeName() { |
||
| 487 | } |
||
| 488 |
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.