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 IL10N */ |
||
| 57 | protected $l10n; |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | private $userId = ''; |
||
| 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 $instance = ''; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | private $joined = ''; |
||
| 82 | |||
| 83 | /** @var bool */ |
||
| 84 | protected $broadcasting = true; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * BaseMember constructor. |
||
| 88 | * |
||
| 89 | * @param string $circleUniqueId |
||
| 90 | * @param string $userId |
||
| 91 | * @param int $type |
||
| 92 | */ |
||
| 93 | public function __construct($userId = '', $type = 0, $circleUniqueId = '') { |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $circleUniqueId |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function setCircleId($circleUniqueId) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getCircleId() { |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getType() { |
||
| 129 | |||
| 130 | public function setType($type) { |
||
| 133 | |||
| 134 | |||
| 135 | public function getViewerType() { |
||
| 142 | |||
| 143 | |||
| 144 | public function setUserId($userId) { |
||
| 150 | |||
| 151 | public function getUserId() { |
||
| 154 | |||
| 155 | |||
| 156 | public function setDisplayName($display) { |
||
| 161 | |||
| 162 | public function getDisplayName() { |
||
| 165 | |||
| 166 | |||
| 167 | public function setLevel($level) { |
||
| 172 | |||
| 173 | public function getLevel() { |
||
| 176 | |||
| 177 | |||
| 178 | public function setNote($note) { |
||
| 183 | |||
| 184 | public function getNote() { |
||
| 187 | |||
| 188 | |||
| 189 | public function setInstance($instance) { |
||
| 194 | |||
| 195 | public function getInstance() { |
||
| 198 | |||
| 199 | |||
| 200 | public function setStatus($status) { |
||
| 209 | |||
| 210 | public function getStatus() { |
||
| 213 | |||
| 214 | |||
| 215 | public function setJoined($joined) { |
||
| 220 | |||
| 221 | public function getJoined() { |
||
| 224 | |||
| 225 | |||
| 226 | public function isLevel($level) { |
||
| 229 | |||
| 230 | |||
| 231 | public function isAlmostMember() { |
||
| 235 | |||
| 236 | |||
| 237 | protected function setAsAMember($level = 1) { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * @param $arr |
||
| 245 | * |
||
| 246 | * @return null|Member |
||
| 247 | */ |
||
| 248 | public static function fromArray($arr) { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * @param $json |
||
| 273 | * |
||
| 274 | * @return Member |
||
|
|
|||
| 275 | */ |
||
| 276 | public static function fromJSON($json) { |
||
| 279 | |||
| 280 | |||
| 281 | public function jsonSerialize() { |
||
| 295 | |||
| 296 | public function getLevelString() { |
||
| 312 | |||
| 313 | |||
| 314 | public function getTypeString() { |
||
| 328 | } |
||
| 329 |
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.