Complex classes like GSEvent 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 GSEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 47 | class GSEvent implements JsonSerializable { |
||
| 48 | |||
| 49 | |||
| 50 | const SEVERITY_LOW = 1; |
||
| 51 | const SEVERITY_HIGH = 3; |
||
| 52 | |||
| 53 | const GLOBAL_SYNC = 'GlobalScale\GlobalSync'; |
||
| 54 | const CIRCLE_STATUS = 'GlobalScale\CircleStatus'; |
||
| 55 | |||
| 56 | const CIRCLE_CREATE = 'GlobalScale\CircleCreate'; |
||
| 57 | const CIRCLE_UPDATE = 'GlobalScale\CircleUpdate'; |
||
| 58 | const CIRCLE_DESTROY = 'GlobalScale\CircleDestroy'; |
||
| 59 | const MEMBER_ADD = 'GlobalScale\MemberAdd'; |
||
| 60 | const MEMBER_JOIN = 'GlobalScale\MemberJoin'; |
||
| 61 | const MEMBER_LEAVE = 'GlobalScale\MemberLeave'; |
||
| 62 | const MEMBER_LEVEL = 'GlobalScale\MemberLevel'; |
||
| 63 | const MEMBER_UPDATE = 'GlobalScale\MemberUpdate'; |
||
| 64 | const MEMBER_REMOVE = 'GlobalScale\MemberRemove'; |
||
| 65 | const USER_DELETED = 'GlobalScale\UserDeleted'; |
||
| 66 | |||
| 67 | const FILE_SHARE = 'GlobalScale\FileShare'; |
||
| 68 | const FILE_UNSHARE = 'GlobalScale\FileUnshare'; |
||
| 69 | |||
| 70 | |||
| 71 | use TArrayTools; |
||
| 72 | |||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | private $type = ''; |
||
| 76 | |||
| 77 | /** @var string */ |
||
| 78 | private $source = ''; |
||
| 79 | |||
| 80 | /** @var Circle */ |
||
| 81 | private $circle; |
||
| 82 | |||
| 83 | /** @var Member */ |
||
| 84 | private $member; |
||
| 85 | |||
| 86 | /** @var SimpleDataStore */ |
||
| 87 | private $data; |
||
| 88 | |||
| 89 | /** @var int */ |
||
| 90 | private $severity = self::SEVERITY_LOW; |
||
| 91 | |||
| 92 | /** @var SimpleDataStore */ |
||
| 93 | private $result; |
||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $key = ''; |
||
| 97 | |||
| 98 | /** @var bool */ |
||
| 99 | private $local = false; |
||
| 100 | |||
| 101 | /** @var bool */ |
||
| 102 | private $force = false; |
||
| 103 | |||
| 104 | /** @var bool */ |
||
| 105 | private $async = false; |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * GSEvent constructor. |
||
| 110 | * |
||
| 111 | * @param string $type |
||
| 112 | * @param bool $local |
||
| 113 | * @param bool $force |
||
| 114 | */ |
||
| 115 | function __construct(string $type = '', bool $local = false, bool $force = false) { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getType(): string { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param mixed $type |
||
| 133 | * |
||
| 134 | * @return GSEvent |
||
| 135 | */ |
||
| 136 | public function setType($type): self { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getSource(): string { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $source |
||
| 152 | * |
||
| 153 | * @return GSEvent |
||
| 154 | */ |
||
| 155 | public function setSource(string $source): self { |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function isLocal(): bool { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param bool $local |
||
| 186 | * |
||
| 187 | * @return GSEvent |
||
| 188 | */ |
||
| 189 | public function setLocal(bool $local): self { |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isForced(): bool { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param bool $force |
||
| 205 | * |
||
| 206 | * @return GSEvent |
||
| 207 | */ |
||
| 208 | public function setForced(bool $force): self { |
||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function isAsync(): bool { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param bool $async |
||
| 225 | * |
||
| 226 | * @return GSEvent |
||
| 227 | */ |
||
| 228 | public function setAsync(bool $async): self { |
||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * @return Circle |
||
| 238 | */ |
||
| 239 | public function getCircle(): Circle { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param Circle $circle |
||
| 245 | * |
||
| 246 | * @return GSEvent |
||
| 247 | */ |
||
| 248 | public function setCircle(Circle $circle): self { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function hasCircle(): bool { |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * @return Member |
||
| 264 | */ |
||
| 265 | public function getMember(): Member { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param Member $member |
||
| 271 | * |
||
| 272 | * @return GSEvent |
||
| 273 | */ |
||
| 274 | public function setMember(Member $member): self { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function hasMember(): bool { |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * @param SimpleDataStore $data |
||
| 290 | * |
||
| 291 | * @return GSEvent |
||
| 292 | */ |
||
| 293 | public function setData(SimpleDataStore $data): self { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return SimpleDataStore |
||
| 301 | */ |
||
| 302 | public function getData(): SimpleDataStore { |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * @return int |
||
| 309 | */ |
||
| 310 | public function getSeverity(): int { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param int $severity |
||
| 316 | * |
||
| 317 | * @return GSEvent |
||
| 318 | */ |
||
| 319 | public function setSeverity(int $severity): self { |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * @return SimpleDataStore |
||
| 328 | */ |
||
| 329 | public function getResult(): SimpleDataStore { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param SimpleDataStore $result |
||
| 335 | * |
||
| 336 | * @return GSEvent |
||
| 337 | */ |
||
| 338 | public function setResult(SimpleDataStore $result): self { |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getKey(): string { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $key |
||
| 354 | * |
||
| 355 | * @return GSEvent |
||
| 356 | */ |
||
| 357 | public function setKey(string $key): self { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function isValid(): bool { |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $json |
||
| 378 | * |
||
| 379 | * @return GSEvent |
||
| 380 | * @throws JsonException |
||
| 381 | * @throws ModelException |
||
| 382 | */ |
||
| 383 | public function importFromJson(string $json): self { |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * @param array $data |
||
| 395 | * |
||
| 396 | * @return GSEvent |
||
| 397 | * @throws ModelException |
||
| 398 | */ |
||
| 399 | public function import(array $data): self { |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | function jsonSerialize(): array { |
||
| 449 | |||
| 450 | |||
| 451 | } |
||
| 452 | |||
| 453 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.