Complex classes like RemoteInstance 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 RemoteInstance, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class RemoteInstance extends NC22Signatory implements INC22QueryRow, JsonSerializable { |
||
49 | |||
50 | |||
51 | use TArrayTools; |
||
52 | |||
53 | const TYPE_UNKNOWN = 'Unknown'; // not trusted |
||
54 | const TYPE_PASSIVE = 'Passive'; // Minimum information about Federated Circles are broadcasted if a member belongs to the circle. |
||
55 | const TYPE_EXTERNAL = 'External'; // info about Federated Circles and their members are broadcasted if a member belongs to the circle. |
||
56 | const TYPE_TRUSTED = 'Trusted'; // everything about Federated Circles are broadcasted. |
||
57 | const TYPE_GLOBALSCALE = 'GlobalScale'; // every Circle is broadcasted, |
||
58 | |||
59 | public static $LIST_TYPE = [ |
||
60 | self::TYPE_UNKNOWN, |
||
61 | self::TYPE_PASSIVE, |
||
62 | self::TYPE_EXTERNAL, |
||
63 | self::TYPE_TRUSTED, |
||
64 | self::TYPE_GLOBALSCALE |
||
65 | ]; |
||
66 | |||
67 | const TEST = 'test'; |
||
68 | const INCOMING = 'incoming'; |
||
69 | const EVENT = 'event'; |
||
70 | const CIRCLES = 'circles'; |
||
71 | const CIRCLE = 'circle'; |
||
72 | const MEMBERS = 'members'; |
||
73 | const MEMBER = 'member'; |
||
74 | |||
75 | |||
76 | /** @var int */ |
||
77 | private $dbId = 0; |
||
78 | |||
79 | /** @var string */ |
||
80 | private $type = self::TYPE_UNKNOWN; |
||
81 | |||
82 | /** @var int */ |
||
83 | private $interface = 0; |
||
84 | |||
85 | /** @var string */ |
||
86 | private $test = ''; |
||
87 | |||
88 | /** @var array */ |
||
89 | private $aliases = []; |
||
90 | |||
91 | /** @var string */ |
||
92 | private $incoming = ''; |
||
93 | |||
94 | /** @var string */ |
||
95 | private $root = ''; |
||
96 | |||
97 | /** @var string */ |
||
98 | private $event = ''; |
||
99 | |||
100 | /** @var string */ |
||
101 | private $circles = ''; |
||
102 | |||
103 | /** @var string */ |
||
104 | private $circle = ''; |
||
105 | |||
106 | /** @var string */ |
||
107 | private $members = ''; |
||
108 | |||
109 | /** @var string */ |
||
110 | private $member = ''; |
||
111 | |||
112 | /** @var string */ |
||
113 | private $uid = ''; |
||
114 | |||
115 | /** @var string */ |
||
116 | private $authSigned = ''; |
||
117 | |||
118 | /** @var bool */ |
||
119 | private $identityAuthed = false; |
||
120 | |||
121 | |||
122 | /** |
||
123 | * @param int $dbId |
||
124 | * |
||
125 | * @return self |
||
126 | */ |
||
127 | public function setDbId(int $dbId): self { |
||
132 | |||
133 | /** |
||
134 | * @return int |
||
135 | */ |
||
136 | public function getDbId(): int { |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @param string $type |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function setType(string $type): self { |
||
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getType(): string { |
||
158 | |||
159 | |||
160 | /** |
||
161 | * @param int $interface |
||
162 | * |
||
163 | * @return RemoteInstance |
||
164 | */ |
||
165 | public function setInterface(int $interface): self { |
||
170 | |||
171 | /** |
||
172 | * @return int |
||
173 | */ |
||
174 | public function getInterface(): int { |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @param array $aliases |
||
181 | * |
||
182 | * @return RemoteInstance |
||
183 | */ |
||
184 | public function setAliases(array $aliases): self { |
||
189 | |||
190 | /** |
||
191 | * @return array |
||
192 | */ |
||
193 | public function getAliases(): array { |
||
196 | |||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getIncoming(): string { |
||
204 | |||
205 | /** |
||
206 | * @param string $incoming |
||
207 | * |
||
208 | * @return self |
||
209 | */ |
||
210 | public function setIncoming(string $incoming): self { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @return string |
||
219 | */ |
||
220 | public function getRoot(): string { |
||
223 | |||
224 | /** |
||
225 | * @param string $root |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function setRoot(string $root): self { |
||
234 | |||
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getEvent(): string { |
||
242 | |||
243 | /** |
||
244 | * @param string $event |
||
245 | * |
||
246 | * @return self |
||
247 | */ |
||
248 | public function setEvent(string $event): self { |
||
253 | |||
254 | |||
255 | /** |
||
256 | * @param string $test |
||
257 | * |
||
258 | * @return RemoteInstance |
||
259 | */ |
||
260 | public function setTest(string $test): self { |
||
265 | |||
266 | /** |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getTest(): string { |
||
272 | |||
273 | |||
274 | /** |
||
275 | * @return string |
||
276 | */ |
||
277 | public function getCircles(): string { |
||
280 | |||
281 | /** |
||
282 | * @param string $circles |
||
283 | * |
||
284 | * @return self |
||
285 | */ |
||
286 | public function setCircles(string $circles): self { |
||
291 | |||
292 | |||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getCircle(): string { |
||
299 | |||
300 | /** |
||
301 | * @param string $circle |
||
302 | * |
||
303 | * @return self |
||
304 | */ |
||
305 | public function setCircle(string $circle): self { |
||
310 | |||
311 | |||
312 | /** |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getMembers(): string { |
||
318 | |||
319 | /** |
||
320 | * @param string $members |
||
321 | * |
||
322 | * @return self |
||
323 | */ |
||
324 | public function setMembers(string $members): self { |
||
329 | |||
330 | |||
331 | /** |
||
332 | * @return string |
||
333 | */ |
||
334 | public function getMember(): string { |
||
337 | |||
338 | /** |
||
339 | * @param string $member |
||
340 | * |
||
341 | * @return self |
||
342 | */ |
||
343 | public function setMember(string $member): self { |
||
348 | |||
349 | |||
350 | /** |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function setUidFromKey(): self { |
||
358 | |||
359 | /** |
||
360 | * @param string $uid |
||
361 | * |
||
362 | * @return RemoteInstance |
||
363 | */ |
||
364 | public function setUid(string $uid): self { |
||
369 | |||
370 | /** |
||
371 | * @param bool $shorten |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | public function getUid(bool $shorten = false): string { |
||
382 | |||
383 | |||
384 | /** |
||
385 | * @param string $authSigned |
||
386 | * |
||
387 | * @return RemoteInstance |
||
388 | */ |
||
389 | public function setAuthSigned(string $authSigned): self { |
||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getAuthSigned(): string { |
||
401 | |||
402 | |||
403 | /** |
||
404 | * @param bool $identityAuthed |
||
405 | * |
||
406 | * @return RemoteInstance |
||
407 | */ |
||
408 | public function setIdentityAuthed(bool $identityAuthed): self { |
||
413 | |||
414 | /** |
||
415 | * @return bool |
||
416 | */ |
||
417 | public function isIdentityAuthed(): bool { |
||
420 | |||
421 | /** |
||
422 | * @throws RemoteUidException |
||
423 | */ |
||
424 | public function mustBeIdentityAuthed(): void { |
||
429 | |||
430 | |||
431 | /** |
||
432 | * @param array $data |
||
433 | * |
||
434 | * @return NC22Signatory |
||
435 | */ |
||
436 | public function import(array $data): NC22Signatory { |
||
461 | |||
462 | |||
463 | /** |
||
464 | * @return array |
||
465 | */ |
||
466 | public function jsonSerialize(): array { |
||
489 | |||
490 | |||
491 | /** |
||
492 | * @param array $data |
||
493 | * @param string $prefix |
||
494 | * |
||
495 | * @return self |
||
496 | * @throws RemoteNotFoundException |
||
497 | */ |
||
498 | public function importFromDatabase(array $data, string $prefix = ''): INC22QueryRow { |
||
513 | |||
514 | } |
||
515 | |||
516 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.