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 | public function setMemberId($memberId) { |
||
| 188 | $this->memberId = $memberId; |
||
| 189 | |||
| 190 | return $this; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getMemberId() { |
||
| 194 | return $this->memberId; |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | public function setDisplayName($display) { |
||
| 199 | $this->displayName = $display; |
||
| 200 | |||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getDisplayName() { |
||
| 205 | return $this->displayName; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | public function setLevel($level) { |
||
| 210 | $this->level = (int)$level; |
||
| 211 | |||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | public function getLevel() { |
||
| 216 | return $this->level; |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | public function setNote($note) { |
||
| 221 | $this->note = $note; |
||
| 222 | |||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | |||
| 226 | public function getNote() { |
||
| 227 | return $this->note; |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | public function setContactId($contactId) { |
||
| 232 | $this->contactId = $contactId; |
||
| 233 | |||
| 234 | return $this; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function getContactId() { |
||
| 238 | return $this->contactId; |
||
| 239 | } |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * @param array $contactMeta |
||
| 244 | * |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | public function setContactMeta(array $contactMeta): self { |
||
| 248 | $this->contactMeta = $contactMeta; |
||
| 249 | |||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function getContactMeta(): array { |
||
| 257 | return $this->contactMeta; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $k |
||
| 262 | * @param string $v |
||
| 263 | * |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function addContactMeta(string $k, string $v): self { |
||
| 267 | $this->contactMeta[$k] = $v; |
||
| 268 | |||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $k |
||
| 274 | * @param string $v |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function addContactMetaArray(string $k, string $v): self { |
||
| 279 | if (!array_key_exists($k, $this->contactMeta)) { |
||
| 280 | $this->contactMeta[$k] = []; |
||
| 281 | } |
||
| 282 | |||
| 283 | $this->contactMeta[$k][] = $v; |
||
| 284 | |||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $k |
||
| 290 | * @param array $v |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function setContactMetaArray(string $k, array $v): self { |
||
| 295 | $this->contactMeta[$k] = $v; |
||
| 296 | |||
| 297 | return $this; |
||
| 298 | } |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @param $status |
||
| 303 | * |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function setStatus($status) { |
||
| 307 | if (is_null($status)) { |
||
| 308 | $this->status = self::STATUS_NONMEMBER; |
||
| 309 | } else { |
||
| 310 | $this->status = $status; |
||
| 311 | } |
||
| 312 | |||
| 313 | return $this; |
||
| 314 | } |
||
| 315 | |||
| 316 | public function getStatus() { |
||
| 317 | return $this->status; |
||
| 318 | } |
||
| 319 | |||
| 320 | |||
| 321 | public function setJoined($joined) { |
||
| 322 | $this->joined = $joined; |
||
| 323 | |||
| 324 | return $this; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getJoined() { |
||
| 328 | return $this->joined; |
||
| 329 | } |
||
| 330 | |||
| 331 | |||
| 332 | public function getJoinedSince(): int { |
||
| 333 | return $this->joinedSince; |
||
| 334 | } |
||
| 335 | |||
| 336 | public function setJoinedSince(int $since) { |
||
| 337 | $this->joinedSince = $since; |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | public function isLevel($level) { |
||
| 342 | return ($this->getLevel() >= $level); |
||
| 343 | } |
||
| 344 | |||
| 345 | |||
| 346 | public function isAlmostMember() { |
||
| 347 | return ($this->getStatus() === Member::STATUS_INVITED |
||
| 348 | || $this->getStatus() === Member::STATUS_REQUEST); |
||
| 349 | } |
||
| 350 | |||
| 351 | |||
| 352 | protected function setAsAMember($level = 1) { |
||
| 353 | $this->setStatus(Member::STATUS_MEMBER); |
||
| 354 | $this->setLevel($level); |
||
| 355 | } |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * @param $arr |
||
| 360 | * |
||
| 361 | * @return null|Member |
||
| 362 | */ |
||
| 363 | public static function fromArray($arr) { |
||
| 364 | if ($arr === null) { |
||
| 365 | return null; |
||
| 366 | } |
||
| 367 | |||
| 368 | $member = new Member(); |
||
| 369 | $member->setCircleId($arr['circle_id']); |
||
| 370 | $member->setLevel($arr['level']); |
||
| 371 | |||
| 372 | $member->setType(MiscService::get($arr, 'user_type')); |
||
| 373 | $member->setType(MiscService::get($arr, 'type', $member->getType())); |
||
| 374 | |||
| 375 | $member->setUserId($arr['user_id']); |
||
| 376 | $member->setStatus($arr['status']); |
||
| 377 | $member->setNote($arr['note']); |
||
| 378 | $member->setJoined($arr['joined']); |
||
| 379 | |||
| 380 | return $member; |
||
| 381 | } |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $json |
||
| 386 | * |
||
| 387 | * @return Member |
||
|
|
|||
| 388 | */ |
||
| 389 | public static function fromJSON($json) { |
||
| 390 | return self::fromArray(json_decode($json, true)); |
||
| 391 | } |
||
| 392 | |||
| 393 | |||
| 394 | public function jsonSerialize() { |
||
| 395 | return [ |
||
| 396 | 'circle_id' => $this->getCircleId(), |
||
| 397 | 'member_id' => $this->getMemberId(), |
||
| 398 | 'user_id' => $this->getUserId(), |
||
| 399 | 'user_type' => $this->getType(), |
||
| 400 | 'display_name' => $this->getDisplayName(), |
||
| 401 | 'contact_id' => $this->getContactId(), |
||
| 402 | 'level' => $this->getLevel(), |
||
| 403 | 'level_string' => $this->getLevelString(), |
||
| 404 | 'status' => $this->getStatus(), |
||
| 405 | 'note' => $this->getNote(), |
||
| 406 | 'joined' => $this->getJoined() |
||
| 407 | ]; |
||
| 408 | } |
||
| 409 | |||
| 410 | public function getLevelString() { |
||
| 426 | |||
| 427 | |||
| 428 | public function getTypeString() { |
||
| 442 | |||
| 443 | public function getTypeName() { |
||
| 444 | switch ($this->getType()) { |
||
| 445 | case self::TYPE_USER: |
||
| 446 | case self::TYPE_MAIL: |
||
| 447 | case self::TYPE_CONTACT: |
||
| 448 | return 'user'; |
||
| 449 | case self::TYPE_GROUP: |
||
| 450 | return 'user-group'; |
||
| 451 | } |
||
| 452 | |||
| 453 | return 'none'; |
||
| 454 | } |
||
| 455 | } |
||
| 456 |
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.