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 int */ |
||
| 75 | private $level; |
||
| 76 | |||
| 77 | /** @var string */ |
||
| 78 | private $status; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | private $contactId = ''; |
||
| 82 | |||
| 83 | /** @var array */ |
||
| 84 | private $contactMeta = []; |
||
| 85 | |||
| 86 | /** @var string */ |
||
| 87 | private $note; |
||
| 88 | |||
| 89 | /** @var string */ |
||
| 90 | private $joined; |
||
| 91 | |||
| 92 | /** @var int */ |
||
| 93 | private $joinedSince; |
||
| 94 | |||
| 95 | /** @var bool */ |
||
| 96 | protected $broadcasting = true; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * BaseMember constructor. |
||
| 100 | * |
||
| 101 | * @param string $circleUniqueId |
||
| 102 | * @param string $userId |
||
| 103 | * @param int $type |
||
| 104 | */ |
||
| 105 | public function __construct($userId = '', $type = 0, $circleUniqueId = '') { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $circleUniqueId |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function setCircleId($circleUniqueId) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getCircleId() { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $circleContactGroupName |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function setCircleContactGroupName($circleContactGroupName): self { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function getCircleContactGroupName(): string { |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | public function getType() { |
||
| 160 | |||
| 161 | public function setType($type) { |
||
| 164 | |||
| 165 | |||
| 166 | public function getViewerType() { |
||
| 173 | |||
| 174 | |||
| 175 | public function setUserId($userId) { |
||
| 181 | |||
| 182 | public function getUserId() { |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | public function setMemberId($memberId) { |
||
| 193 | |||
| 194 | public function getMemberId() { |
||
| 197 | |||
| 198 | |||
| 199 | public function setDisplayName($display) { |
||
| 204 | |||
| 205 | public function getDisplayName() { |
||
| 208 | |||
| 209 | |||
| 210 | public function setLevel($level) { |
||
| 215 | |||
| 216 | public function getLevel() { |
||
| 219 | |||
| 220 | |||
| 221 | public function setNote($note) { |
||
| 226 | |||
| 227 | public function getNote() { |
||
| 230 | |||
| 231 | |||
| 232 | public function setContactId($contactId) { |
||
| 237 | |||
| 238 | public function getContactId() { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * @param array $contactMeta |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function setContactMeta(array $contactMeta): self { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function getContactMeta(): array { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $k |
||
| 263 | * @param string $v |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function addContactMeta(string $k, string $v): self { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $k |
||
| 275 | * @param string $v |
||
| 276 | * |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function addContactMetaArray(string $k, string $v): self { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $k |
||
| 291 | * @param array $v |
||
| 292 | * |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | public function setContactMetaArray(string $k, array $v): self { |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * @param $status |
||
| 304 | * |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | public function setStatus($status) { |
||
| 316 | |||
| 317 | public function getStatus() { |
||
| 320 | |||
| 321 | |||
| 322 | public function setJoined($joined) { |
||
| 327 | |||
| 328 | public function getJoined() { |
||
| 331 | |||
| 332 | |||
| 333 | public function getJoinedSince(): int { |
||
| 336 | |||
| 337 | public function setJoinedSince(int $since) { |
||
| 340 | |||
| 341 | |||
| 342 | public function isLevel($level) { |
||
| 345 | |||
| 346 | |||
| 347 | public function isAlmostMember() { |
||
| 351 | |||
| 352 | |||
| 353 | protected function setAsAMember($level = 1) { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * @param $arr |
||
| 361 | * |
||
| 362 | * @return null|Member |
||
| 363 | */ |
||
| 364 | public static function fromArray($arr) { |
||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * @param $json |
||
| 387 | * |
||
| 388 | * @return Member |
||
|
|
|||
| 389 | */ |
||
| 390 | public static function fromJSON($json) { |
||
| 393 | |||
| 394 | |||
| 395 | public function jsonSerialize() { |
||
| 409 | |||
| 410 | public function getLevelString() { |
||
| 426 | |||
| 427 | |||
| 428 | public function getTypeString() { |
||
| 442 | } |
||
| 443 |
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.