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); |
||
48 | class GSEvent implements JsonSerializable { |
||
49 | |||
50 | |||
51 | const SEVERITY_LOW = 1; |
||
52 | const SEVERITY_HIGH = 3; |
||
53 | |||
54 | const TEST = '\OCA\Circles\GlobalScale\Test'; |
||
55 | const GLOBAL_SYNC = '\OCA\Circles\GlobalScale\GlobalSync'; |
||
56 | const CIRCLE_STATUS = '\OCA\Circles\GlobalScale\CircleStatus'; |
||
57 | |||
58 | const CIRCLE_CREATE = '\OCA\Circles\GlobalScale\CircleCreate'; |
||
59 | const CIRCLE_UPDATE = '\OCA\Circles\GlobalScale\CircleUpdate'; |
||
60 | const CIRCLE_DESTROY = '\OCA\Circles\GlobalScale\CircleDestroy'; |
||
61 | const MEMBER_ADD = '\OCA\Circles\GlobalScale\MemberAdd'; |
||
62 | const MEMBER_JOIN = '\OCA\Circles\GlobalScale\MemberJoin'; |
||
63 | const MEMBER_LEAVE = '\OCA\Circles\GlobalScale\MemberLeave'; |
||
64 | const MEMBER_LEVEL = '\OCA\Circles\GlobalScale\MemberLevel'; |
||
65 | const MEMBER_UPDATE = '\OCA\Circles\GlobalScale\MemberUpdate'; |
||
66 | const MEMBER_REMOVE = '\OCA\Circles\GlobalScale\MemberRemove'; |
||
67 | const USER_DELETED = '\OCA\Circles\GlobalScale\UserDeleted'; |
||
68 | |||
69 | const FILE_SHARE = '\OCA\Circles\GlobalScale\FileShare'; |
||
70 | const FILE_UNSHARE = '\OCA\Circles\GlobalScale\FileUnshare'; |
||
71 | |||
72 | |||
73 | use TArrayTools; |
||
74 | |||
75 | |||
76 | /** @var string */ |
||
77 | private $type = ''; |
||
78 | |||
79 | /** @var string */ |
||
80 | private $source = ''; |
||
81 | |||
82 | /** @var DeprecatedCircle */ |
||
83 | private $deprecatedCircle; |
||
84 | |||
85 | /** @var Circle */ |
||
86 | private $circle; |
||
87 | |||
88 | /** @var DeprecatedMember */ |
||
89 | private $member; |
||
90 | |||
91 | /** @var SimpleDataStore */ |
||
92 | private $data; |
||
93 | |||
94 | /** @var int */ |
||
95 | private $severity = self::SEVERITY_LOW; |
||
96 | |||
97 | /** @var SimpleDataStore */ |
||
98 | private $result; |
||
99 | |||
100 | /** @var string */ |
||
101 | private $key = ''; |
||
102 | |||
103 | /** @var bool */ |
||
104 | private $local = false; |
||
105 | |||
106 | /** @var bool */ |
||
107 | private $force = false; |
||
108 | |||
109 | /** @var bool */ |
||
110 | private $async = false; |
||
111 | |||
112 | /** @var bool */ |
||
113 | private $checked = false; |
||
114 | |||
115 | |||
116 | /** |
||
117 | * GSEvent constructor. |
||
118 | * |
||
119 | * @param string $type |
||
120 | * @param bool $local |
||
121 | * @param bool $force |
||
122 | */ |
||
123 | function __construct(string $type = '', bool $local = false, bool $force = false) { |
||
|
|||
124 | $this->type = $type; |
||
125 | $this->local = $local; |
||
126 | $this->force = $force; |
||
127 | $this->data = new SimpleDataStore(); |
||
128 | $this->result = new SimpleDataStore(); |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getType(): string { |
||
138 | |||
139 | /** |
||
140 | * @param mixed $type |
||
141 | * |
||
142 | * @return GSEvent |
||
143 | */ |
||
144 | public function setType($type): self { |
||
149 | |||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getSource(): string { |
||
157 | |||
158 | /** |
||
159 | * @param string $source |
||
160 | * |
||
161 | * @return GSEvent |
||
162 | */ |
||
163 | public function setSource(string $source): self { |
||
183 | |||
184 | |||
185 | /** |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function isLocal(): bool { |
||
191 | |||
192 | /** |
||
193 | * @param bool $local |
||
194 | * |
||
195 | * @return GSEvent |
||
196 | */ |
||
197 | public function setLocal(bool $local): self { |
||
202 | |||
203 | |||
204 | /** |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isForced(): bool { |
||
210 | |||
211 | /** |
||
212 | * @param bool $force |
||
213 | * |
||
214 | * @return GSEvent |
||
215 | */ |
||
216 | public function setForced(bool $force): self { |
||
221 | |||
222 | |||
223 | /** |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function isAsync(): bool { |
||
229 | |||
230 | /** |
||
231 | * @param bool $async |
||
232 | * |
||
233 | * @return GSEvent |
||
234 | */ |
||
235 | public function setAsync(bool $async): self { |
||
240 | |||
241 | |||
242 | /** |
||
243 | * @return DeprecatedCircle |
||
244 | * @deprecated |
||
245 | */ |
||
246 | public function getDeprecatedCircle(): DeprecatedCircle { |
||
249 | |||
250 | /** |
||
251 | * @param DeprecatedCircle $deprecatedCircle |
||
252 | * |
||
253 | * @return GSEvent |
||
254 | * @deprecated |
||
255 | */ |
||
256 | public function setDeprecatedCircle(DeprecatedCircle $deprecatedCircle): self { |
||
261 | |||
262 | /** |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function hasCircle(): bool { |
||
268 | |||
269 | /** |
||
270 | * @param Circle $circle |
||
271 | * |
||
272 | * @return GSEvent |
||
273 | */ |
||
274 | public function setCircle(Circle $circle): self { |
||
279 | |||
280 | /** |
||
281 | * @return Circle |
||
282 | */ |
||
283 | public function getCircle(): Circle { |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @return DeprecatedMember |
||
290 | */ |
||
291 | public function getMember(): DeprecatedMember { |
||
294 | |||
295 | /** |
||
296 | * @param DeprecatedMember $member |
||
297 | * |
||
298 | * @return GSEvent |
||
299 | */ |
||
300 | public function setMember(DeprecatedMember $member): self { |
||
305 | |||
306 | /** |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function hasMember(): bool { |
||
312 | |||
313 | |||
314 | /** |
||
315 | * @param SimpleDataStore $data |
||
316 | * |
||
317 | * @return GSEvent |
||
318 | */ |
||
319 | public function setData(SimpleDataStore $data): self { |
||
324 | |||
325 | /** |
||
326 | * @return SimpleDataStore |
||
327 | */ |
||
328 | public function getData(): SimpleDataStore { |
||
331 | |||
332 | |||
333 | /** |
||
334 | * @return int |
||
335 | */ |
||
336 | public function getSeverity(): int { |
||
339 | |||
340 | /** |
||
341 | * @param int $severity |
||
342 | * |
||
343 | * @return GSEvent |
||
344 | */ |
||
345 | public function setSeverity(int $severity): self { |
||
350 | |||
351 | |||
352 | /** |
||
353 | * @return SimpleDataStore |
||
354 | */ |
||
355 | public function getResult(): SimpleDataStore { |
||
358 | |||
359 | /** |
||
360 | * @param SimpleDataStore $result |
||
361 | * |
||
362 | * @return GSEvent |
||
363 | */ |
||
364 | public function setResult(SimpleDataStore $result): self { |
||
369 | |||
370 | |||
371 | /** |
||
372 | * @return string |
||
373 | */ |
||
374 | public function getKey(): string { |
||
377 | |||
378 | /** |
||
379 | * @param string $key |
||
380 | * |
||
381 | * @return GSEvent |
||
382 | */ |
||
383 | public function setKey(string $key): self { |
||
388 | |||
389 | |||
390 | /** |
||
391 | * @return bool |
||
392 | */ |
||
393 | public function isValid(): bool { |
||
400 | |||
401 | |||
402 | /** |
||
403 | * @param string $json |
||
404 | * |
||
405 | * @return GSEvent |
||
406 | * @throws JsonException |
||
407 | * @throws ModelException |
||
408 | */ |
||
409 | public function importFromJson(string $json): self { |
||
417 | |||
418 | |||
419 | /** |
||
420 | * @param array $data |
||
421 | * |
||
422 | * @return GSEvent |
||
423 | * @throws ModelException |
||
424 | */ |
||
425 | public function import(array $data): self { |
||
449 | |||
450 | |||
451 | /** |
||
452 | * @return array |
||
453 | */ |
||
454 | function jsonSerialize(): array { |
||
477 | |||
478 | |||
479 | } |
||
480 | |||
481 |
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.