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 |
||
| 32 | class BaseMember implements \JsonSerializable { |
||
| 33 | |||
| 34 | const LEVEL_NONE = 0; |
||
| 35 | const LEVEL_MEMBER = 1; |
||
| 36 | const LEVEL_MODERATOR = 4; |
||
| 37 | const LEVEL_ADMIN = 8; |
||
| 38 | const LEVEL_OWNER = 9; |
||
| 39 | |||
| 40 | const STATUS_NONMEMBER = 'Unknown'; |
||
| 41 | const STATUS_INVITED = 'Invited'; |
||
| 42 | const STATUS_REQUEST = 'Requesting'; |
||
| 43 | const STATUS_MEMBER = 'Member'; |
||
| 44 | const STATUS_BLOCKED = 'Blocked'; |
||
| 45 | const STATUS_KICKED = 'Kicked'; |
||
| 46 | |||
| 47 | const TYPE_USER = 1; |
||
| 48 | const TYPE_MAIL = 2; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $circleUniqueId; |
||
| 52 | |||
| 53 | /** @var L10N */ |
||
| 54 | protected $l10n; |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | private $userId = ''; |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | private $groupId = ''; |
||
| 61 | |||
| 62 | /** @var int */ |
||
| 63 | private $type = self::TYPE_USER; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | private $displayName; |
||
| 67 | |||
| 68 | /** @var int */ |
||
| 69 | private $level; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | private $status; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | private $note; |
||
| 76 | |||
| 77 | /** @var string */ |
||
| 78 | private $joined; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * BaseMember constructor. |
||
| 82 | * |
||
| 83 | * @param $l10n |
||
| 84 | * @param string $userId |
||
| 85 | * @param string $circleUniqueId |
||
| 86 | */ |
||
| 87 | public function __construct($l10n, $userId = '', $circleUniqueId = '') { |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $circleUniqueId |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function setCircleId($circleUniqueId) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getCircleId() { |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $groupId |
||
| 122 | */ |
||
| 123 | public function setGroupId($groupId) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getGroupId() { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @return int |
||
| 137 | */ |
||
| 138 | public function getType() { |
||
| 141 | |||
| 142 | public function setType($type) { |
||
| 145 | |||
| 146 | |||
| 147 | public function getViewerType() { |
||
| 154 | |||
| 155 | public function getViewerId() { |
||
| 162 | |||
| 163 | public function setUserId($userId) { |
||
| 174 | |||
| 175 | public function getUserId() { |
||
| 178 | |||
| 179 | |||
| 180 | public function setDisplayName($display) { |
||
| 185 | |||
| 186 | public function getDisplayNAme() { |
||
| 189 | |||
| 190 | |||
| 191 | public function setLevel($level) { |
||
| 196 | |||
| 197 | public function getLevel() { |
||
| 200 | |||
| 201 | |||
| 202 | public function setNote($note) { |
||
| 207 | |||
| 208 | public function getNote() { |
||
| 211 | |||
| 212 | |||
| 213 | public function setStatus($status) { |
||
| 222 | |||
| 223 | public function getStatus() { |
||
| 226 | |||
| 227 | |||
| 228 | public function setJoined($joined) { |
||
| 233 | |||
| 234 | public function getJoined() { |
||
| 237 | |||
| 238 | |||
| 239 | public function isLevel($level) { |
||
| 242 | |||
| 243 | |||
| 244 | public function isAlmostMember() { |
||
| 248 | |||
| 249 | |||
| 250 | protected function setAsAMember($level = 1) { |
||
| 254 | |||
| 255 | |||
| 256 | public static function fromArray($l10n, $arr) { |
||
| 288 | |||
| 289 | |||
| 290 | public static function fromJSON($l10n, $json) { |
||
| 293 | |||
| 294 | public function jsonSerialize() { |
||
| 308 | |||
| 309 | public function getLevelString() { |
||
| 325 | } |
||
| 326 |