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 string */ |
||
| 81 | private $note; |
||
| 82 | |||
| 83 | /** @var string */ |
||
| 84 | private $joined; |
||
| 85 | |||
| 86 | /** @var bool */ |
||
| 87 | protected $broadcasting = true; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * BaseMember constructor. |
||
| 91 | * |
||
| 92 | * @param string $circleUniqueId |
||
| 93 | * @param string $userId |
||
| 94 | * @param int $type |
||
| 95 | */ |
||
| 96 | public function __construct($userId = '', $type = 0, $circleUniqueId = '') { |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $circleUniqueId |
||
| 109 | * |
||
| 110 | * @return $this |
||
| 111 | */ |
||
| 112 | public function setCircleId($circleUniqueId) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function getCircleId() { |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $circleContactGroupName |
||
| 128 | * |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setCircleContactGroupName($circleContactGroupName): self { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getCircleContactGroupName(): string { |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | public function getType() { |
||
| 151 | |||
| 152 | public function setType($type) { |
||
| 155 | |||
| 156 | |||
| 157 | public function getViewerType() { |
||
| 164 | |||
| 165 | |||
| 166 | public function setUserId($userId) { |
||
| 172 | |||
| 173 | public function getUserId() { |
||
| 176 | |||
| 177 | |||
| 178 | public function setDisplayName($display) { |
||
| 183 | |||
| 184 | public function getDisplayName() { |
||
| 187 | |||
| 188 | |||
| 189 | public function setLevel($level) { |
||
| 194 | |||
| 195 | public function getLevel() { |
||
| 198 | |||
| 199 | |||
| 200 | public function setNote($note) { |
||
| 205 | |||
| 206 | public function getNote() { |
||
| 209 | |||
| 210 | |||
| 211 | public function setContactId($contactId) { |
||
| 216 | |||
| 217 | public function getContactId() { |
||
| 220 | |||
| 221 | |||
| 222 | public function setStatus($status) { |
||
| 231 | |||
| 232 | public function getStatus() { |
||
| 235 | |||
| 236 | |||
| 237 | public function setJoined($joined) { |
||
| 242 | |||
| 243 | public function getJoined() { |
||
| 246 | |||
| 247 | |||
| 248 | public function isLevel($level) { |
||
| 251 | |||
| 252 | |||
| 253 | public function isAlmostMember() { |
||
| 257 | |||
| 258 | |||
| 259 | protected function setAsAMember($level = 1) { |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * @param $arr |
||
| 267 | * |
||
| 268 | * @return null|Member |
||
| 269 | */ |
||
| 270 | public static function fromArray($arr) { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param $json |
||
| 293 | * |
||
| 294 | * @return Member |
||
|
|
|||
| 295 | */ |
||
| 296 | public static function fromJSON($json) { |
||
| 299 | |||
| 300 | |||
| 301 | public function jsonSerialize() { |
||
| 315 | |||
| 316 | public function getLevelString() { |
||
| 332 | |||
| 333 | |||
| 334 | public function getTypeString() { |
||
| 348 | } |
||
| 349 |
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.