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 int */ |
||
| 66 | private $type = self::TYPE_USER; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | private $displayName; |
||
| 70 | |||
| 71 | /** @var int */ |
||
| 72 | private $level; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | private $status; |
||
| 76 | |||
| 77 | /** @var string */ |
||
| 78 | private $contactId = ''; |
||
| 79 | |||
| 80 | /** @var array */ |
||
| 81 | private $contactMeta = []; |
||
| 82 | |||
| 83 | /** @var string */ |
||
| 84 | private $note; |
||
| 85 | |||
| 86 | /** @var string */ |
||
| 87 | private $joined; |
||
| 88 | |||
| 89 | /** @var int */ |
||
| 90 | private $joinedSince; |
||
| 91 | |||
| 92 | /** @var bool */ |
||
| 93 | protected $broadcasting = true; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * BaseMember constructor. |
||
| 97 | * |
||
| 98 | * @param string $circleUniqueId |
||
| 99 | * @param string $userId |
||
| 100 | * @param int $type |
||
| 101 | */ |
||
| 102 | public function __construct($userId = '', $type = 0, $circleUniqueId = '') { |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $circleUniqueId |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function setCircleId($circleUniqueId) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getCircleId() { |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $circleContactGroupName |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function setCircleContactGroupName($circleContactGroupName): self { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getCircleContactGroupName(): string { |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @return int |
||
| 153 | */ |
||
| 154 | public function getType() { |
||
| 157 | |||
| 158 | public function setType($type) { |
||
| 161 | |||
| 162 | |||
| 163 | public function getViewerType() { |
||
| 170 | |||
| 171 | |||
| 172 | public function setUserId($userId) { |
||
| 178 | |||
| 179 | public function getUserId() { |
||
| 182 | |||
| 183 | |||
| 184 | public function setDisplayName($display) { |
||
| 189 | |||
| 190 | public function getDisplayName() { |
||
| 193 | |||
| 194 | |||
| 195 | public function setLevel($level) { |
||
| 200 | |||
| 201 | public function getLevel() { |
||
| 204 | |||
| 205 | |||
| 206 | public function setNote($note) { |
||
| 211 | |||
| 212 | public function getNote() { |
||
| 215 | |||
| 216 | |||
| 217 | public function setContactId($contactId) { |
||
| 222 | |||
| 223 | public function getContactId() { |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * @param array $contactMeta |
||
| 230 | * |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function setContactMeta(array $contactMeta): self { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function getContactMeta(): array { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $k |
||
| 248 | * @param string $v |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function addContactMeta(string $k, string $v): self { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $k |
||
| 260 | * @param string $v |
||
| 261 | * |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function addContactMetaArray(string $k, string $v): self { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $k |
||
| 276 | * @param array $v |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function setContactMetaArray(string $k, array $v): self { |
||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * @param $status |
||
| 289 | * |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function setStatus($status) { |
||
| 301 | |||
| 302 | public function getStatus() { |
||
| 305 | |||
| 306 | |||
| 307 | public function setJoined($joined) { |
||
| 312 | |||
| 313 | public function getJoined() { |
||
| 316 | |||
| 317 | |||
| 318 | public function getJoinedSince(): int { |
||
| 321 | |||
| 322 | public function setJoinedSince(int $since) { |
||
| 325 | |||
| 326 | |||
| 327 | public function isLevel($level) { |
||
| 330 | |||
| 331 | |||
| 332 | public function isAlmostMember() { |
||
| 336 | |||
| 337 | |||
| 338 | protected function setAsAMember($level = 1) { |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * @param $arr |
||
| 346 | * |
||
| 347 | * @return null|Member |
||
| 348 | */ |
||
| 349 | public static function fromArray($arr) { |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * @param $json |
||
| 372 | * |
||
| 373 | * @return Member |
||
|
|
|||
| 374 | */ |
||
| 375 | public static function fromJSON($json) { |
||
| 378 | |||
| 379 | |||
| 380 | public function jsonSerialize() { |
||
| 394 | |||
| 395 | public function getLevelString() { |
||
| 411 | |||
| 412 | |||
| 413 | public function getTypeString() { |
||
| 427 | } |
||
| 428 |
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.