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 |
||
| 35 | class BaseMember implements JsonSerializable { |
||
| 36 | |||
| 37 | const LEVEL_NONE = 0; |
||
| 38 | const LEVEL_MEMBER = 1; |
||
| 39 | const LEVEL_MODERATOR = 4; |
||
| 40 | const LEVEL_ADMIN = 8; |
||
| 41 | const LEVEL_OWNER = 9; |
||
| 42 | |||
| 43 | const STATUS_NONMEMBER = 'Unknown'; |
||
| 44 | const STATUS_INVITED = 'Invited'; |
||
| 45 | const STATUS_REQUEST = 'Requesting'; |
||
| 46 | const STATUS_MEMBER = 'Member'; |
||
| 47 | const STATUS_BLOCKED = 'Blocked'; |
||
| 48 | const STATUS_KICKED = 'Kicked'; |
||
| 49 | |||
| 50 | const TYPE_USER = 1; |
||
| 51 | const TYPE_GROUP = 2; |
||
| 52 | const TYPE_MAIL = 3; |
||
| 53 | const TYPE_CONTACT = 4; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $circleUniqueId; |
||
| 57 | |||
| 58 | /** @var IL10N */ |
||
| 59 | protected $l10n; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $userId = ''; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $memberId = ''; |
||
| 66 | |||
| 67 | /** @var int */ |
||
| 68 | private $type = self::TYPE_USER; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | private $cachedName = ''; |
||
| 72 | |||
| 73 | /** @var int */ |
||
| 74 | private $cachedUpdate = 0; |
||
| 75 | |||
| 76 | /** @var int */ |
||
| 77 | private $level; |
||
| 78 | |||
| 79 | /** @var string */ |
||
| 80 | private $status; |
||
| 81 | |||
| 82 | /** @var string */ |
||
| 83 | private $contactId = ''; |
||
| 84 | |||
| 85 | /** @var array */ |
||
| 86 | private $contactMeta = []; |
||
| 87 | |||
| 88 | /** @var string */ |
||
| 89 | private $note; |
||
| 90 | |||
| 91 | /** @var string */ |
||
| 92 | private $instance = ''; |
||
| 93 | |||
| 94 | /** @var string */ |
||
| 95 | private $joined = ''; |
||
| 96 | |||
| 97 | /** @var int */ |
||
| 98 | private $joinedSince; |
||
| 99 | |||
| 100 | /** @var bool */ |
||
| 101 | protected $broadcasting = true; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * BaseMember constructor. |
||
| 105 | * |
||
| 106 | * @param string $circleUniqueId |
||
| 107 | * @param string $userId |
||
| 108 | * @param int $type |
||
| 109 | */ |
||
| 110 | public function __construct($userId = '', $type = 0, $circleUniqueId = '') { |
||
| 111 | $this->l10n = OC::$server->getL10N(Application::APP_NAME); |
||
| 112 | |||
| 113 | $this->setType($type); |
||
| 114 | $this->setUserId($userId); |
||
| 115 | $this->setCircleId($circleUniqueId); |
||
| 116 | $this->setLevel(Member::LEVEL_NONE); |
||
| 117 | $this->setStatus(Member::STATUS_NONMEMBER); |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $circleUniqueId |
||
| 123 | * |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function setCircleId($circleUniqueId) { |
||
| 127 | $this->circleUniqueId = $circleUniqueId; |
||
| 128 | |||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getCircleId() { |
||
| 136 | return $this->circleUniqueId; |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function getType() { |
||
| 144 | return $this->type; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function setType($type) { |
||
| 148 | $this->type = (int)$type; |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | public function getViewerType() { |
||
| 153 | if ($this->getType() === 2) { |
||
| 154 | return 'group'; |
||
| 155 | } else { |
||
| 156 | return 'user'; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | public function setUserId($userId) { |
||
| 162 | $this->userId = $userId; |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getUserId() { |
||
| 168 | return $this->userId; |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | public function setMemberId($memberId) { |
||
| 173 | $this->memberId = $memberId; |
||
| 174 | |||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getMemberId() { |
||
| 179 | return $this->memberId; |
||
| 180 | } |
||
| 181 | |||
| 182 | |||
| 183 | public function setCachedName($display) { |
||
| 184 | $this->cachedName = $display; |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function getCachedName() { |
||
| 190 | if ($this->cachedName === '') { |
||
| 191 | return $this->userId; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $this->cachedName; |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | public function setCachedUpdate(int $time) { |
||
| 199 | $this->cachedUpdate = $time; |
||
| 200 | |||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getCachedUpdate(): int { |
||
| 205 | return $this->cachedUpdate; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | public function setLevel($level) { |
||
| 214 | |||
| 215 | public function getLevel() { |
||
| 218 | |||
| 219 | |||
| 220 | public function setNote($note) { |
||
| 225 | |||
| 226 | public function getNote() { |
||
| 229 | |||
| 230 | |||
| 231 | public function setInstance($instance) { |
||
| 232 | $this->instance = $instance; |
||
| 233 | |||
| 234 | return $this; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function getInstance() { |
||
| 238 | return $this->instance; |
||
| 239 | } |
||
| 240 | |||
| 241 | |||
| 242 | public function setContactId($contactId) { |
||
| 243 | $this->contactId = $contactId; |
||
| 244 | |||
| 245 | return $this; |
||
| 247 | |||
| 248 | public function getContactId() { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * @param array $contactMeta |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function setContactMeta(array $contactMeta): self { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function getContactMeta(): array { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $k |
||
| 273 | * @param string $v |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function addContactMeta(string $k, string $v): self { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $k |
||
| 285 | * @param string $v |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function addContactMetaArray(string $k, string $v): self { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $k |
||
| 301 | * @param array $v |
||
| 302 | * |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function setContactMetaArray(string $k, array $v): self { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * @param $status |
||
| 314 | * |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function setStatus($status) { |
||
| 326 | |||
| 327 | public function getStatus() { |
||
| 330 | |||
| 331 | |||
| 332 | public function setJoined($joined) { |
||
| 337 | |||
| 338 | public function getJoined() { |
||
| 341 | |||
| 342 | |||
| 343 | public function getJoinedSince(): int { |
||
| 346 | |||
| 347 | public function setJoinedSince(int $since) { |
||
| 350 | |||
| 351 | |||
| 352 | public function isLevel($level) { |
||
| 355 | |||
| 356 | |||
| 357 | public function isAlmostMember() { |
||
| 361 | |||
| 362 | |||
| 363 | protected function setAsAMember($level = 1) { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param $arr |
||
| 371 | * |
||
| 372 | * @return null|Member |
||
| 373 | */ |
||
| 374 | public static function fromArray($arr) { |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * @param $json |
||
| 404 | * |
||
| 405 | * @return Member |
||
|
|
|||
| 406 | */ |
||
| 407 | public static function fromJSON($json) { |
||
| 410 | |||
| 411 | |||
| 412 | public function jsonSerialize() { |
||
| 428 | |||
| 429 | public function getLevelString() { |
||
| 445 | |||
| 446 | |||
| 447 | public function getTypeString() { |
||
| 461 | |||
| 462 | public function getTypeName() { |
||
| 474 | } |
||
| 475 |
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.