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 bool */ |
||
| 90 | protected $broadcasting = true; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * BaseMember constructor. |
||
| 94 | * |
||
| 95 | * @param string $circleUniqueId |
||
| 96 | * @param string $userId |
||
| 97 | * @param int $type |
||
| 98 | */ |
||
| 99 | public function __construct($userId = '', $type = 0, $circleUniqueId = '') { |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $circleUniqueId |
||
| 112 | * |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | public function setCircleId($circleUniqueId) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getCircleId() { |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $circleContactGroupName |
||
| 131 | * |
||
| 132 | * @return $this |
||
| 133 | */ |
||
| 134 | public function setCircleContactGroupName($circleContactGroupName): self { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getCircleContactGroupName(): string { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function getType() { |
||
| 154 | |||
| 155 | public function setType($type) { |
||
| 158 | |||
| 159 | |||
| 160 | public function getViewerType() { |
||
| 167 | |||
| 168 | |||
| 169 | public function setUserId($userId) { |
||
| 175 | |||
| 176 | public function getUserId() { |
||
| 179 | |||
| 180 | |||
| 181 | public function setDisplayName($display) { |
||
| 186 | |||
| 187 | public function getDisplayName() { |
||
| 190 | |||
| 191 | |||
| 192 | public function setLevel($level) { |
||
| 197 | |||
| 198 | public function getLevel() { |
||
| 201 | |||
| 202 | |||
| 203 | public function setNote($note) { |
||
| 208 | |||
| 209 | public function getNote() { |
||
| 212 | |||
| 213 | |||
| 214 | public function setContactId($contactId) { |
||
| 219 | |||
| 220 | public function getContactId() { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $contactMeta |
||
| 227 | * |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | public function setContactMeta(array $contactMeta): self { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | public function getContactMeta(): array { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $k |
||
| 245 | * @param string $v |
||
| 246 | * |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function addContactMeta(string $k, string $v): self { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $k |
||
| 257 | * @param string $v |
||
| 258 | * |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function addContactMetaArray(string $k, string $v): self { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $k |
||
| 273 | * @param array $v |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function setContactMetaArray(string $k, array $v): self { |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * @param $status |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function setStatus($status) { |
||
| 298 | |||
| 299 | public function getStatus() { |
||
| 302 | |||
| 303 | |||
| 304 | public function setJoined($joined) { |
||
| 309 | |||
| 310 | public function getJoined() { |
||
| 313 | |||
| 314 | |||
| 315 | public function isLevel($level) { |
||
| 318 | |||
| 319 | |||
| 320 | public function isAlmostMember() { |
||
| 324 | |||
| 325 | |||
| 326 | protected function setAsAMember($level = 1) { |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * @param $arr |
||
| 334 | * |
||
| 335 | * @return null|Member |
||
| 336 | */ |
||
| 337 | public static function fromArray($arr) { |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * @param $json |
||
| 360 | * |
||
| 361 | * @return Member |
||
|
|
|||
| 362 | */ |
||
| 363 | public static function fromJSON($json) { |
||
| 366 | |||
| 367 | |||
| 368 | public function jsonSerialize() { |
||
| 382 | |||
| 383 | public function getLevelString() { |
||
| 399 | |||
| 400 | |||
| 401 | public function getTypeString() { |
||
| 415 | } |
||
| 416 |
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.