Complex classes like HostConfig 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 HostConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class HostConfig |
||
6 | { |
||
7 | /** |
||
8 | * @var string[] |
||
9 | */ |
||
10 | protected $binds; |
||
11 | /** |
||
12 | * @var string[] |
||
13 | */ |
||
14 | protected $links; |
||
15 | /** |
||
16 | * @var string[] |
||
17 | */ |
||
18 | protected $lxcConf; |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | protected $memory; |
||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $memorySwap; |
||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $cpuShares; |
||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $cpuPeriod; |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $cpusetCpus; |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $cpusetMems; |
||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $blkioWeight; |
||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $memorySwappiness; |
||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $oomKillDisable; |
||
55 | /** |
||
56 | * @var PortBinding[] |
||
57 | */ |
||
58 | protected $portBindings; |
||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $publishAllPorts; |
||
63 | /** |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected $privileged; |
||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $readonlyRootfs; |
||
71 | /** |
||
72 | * @var string[] |
||
73 | */ |
||
74 | protected $dns; |
||
75 | /** |
||
76 | * @var string[] |
||
77 | */ |
||
78 | protected $dnsSearch; |
||
79 | /** |
||
80 | * @var string[] |
||
81 | */ |
||
82 | protected $extraHosts; |
||
83 | /** |
||
84 | * @var string[] |
||
85 | */ |
||
86 | protected $volumesFrom; |
||
87 | /** |
||
88 | * @var string[] |
||
89 | */ |
||
90 | protected $capAdd; |
||
91 | /** |
||
92 | * @var string[] |
||
93 | */ |
||
94 | protected $capDrop; |
||
95 | /** |
||
96 | * @var RestartPolicy |
||
97 | */ |
||
98 | protected $restartPolicy; |
||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $networkMode; |
||
103 | /** |
||
104 | * @var Device[] |
||
105 | */ |
||
106 | protected $devices; |
||
107 | /** |
||
108 | * @var Ulimit[] |
||
109 | */ |
||
110 | protected $ulimits; |
||
111 | /** |
||
112 | * @var string[] |
||
113 | */ |
||
114 | protected $securityOpt; |
||
115 | /** |
||
116 | * @var LogConfig |
||
117 | */ |
||
118 | protected $logConfig; |
||
119 | /** |
||
120 | * @var string |
||
121 | */ |
||
122 | protected $cgroupParent; |
||
123 | |||
124 | /** |
||
125 | * @return string[] |
||
126 | */ |
||
127 | public function getBinds() |
||
131 | |||
132 | /** |
||
133 | * @param string[] $binds |
||
134 | * |
||
135 | * @return self |
||
136 | */ |
||
137 | public function setBinds(array $binds = null) |
||
143 | |||
144 | /** |
||
145 | * @return string[] |
||
146 | */ |
||
147 | public function getLinks() |
||
151 | |||
152 | /** |
||
153 | * @param string[] $links |
||
154 | * |
||
155 | * @return self |
||
156 | */ |
||
157 | public function setLinks(array $links = null) |
||
163 | |||
164 | /** |
||
165 | * @return string[] |
||
166 | */ |
||
167 | public function getLxcConf() |
||
171 | |||
172 | /** |
||
173 | * @param string[] $lxcConf |
||
174 | * |
||
175 | * @return self |
||
176 | */ |
||
177 | public function setLxcConf(\ArrayObject $lxcConf = null) |
||
183 | |||
184 | /** |
||
185 | * @return int |
||
186 | */ |
||
187 | public function getMemory() |
||
191 | |||
192 | /** |
||
193 | * @param int $memory |
||
194 | * |
||
195 | * @return self |
||
196 | */ |
||
197 | public function setMemory($memory = null) |
||
203 | |||
204 | /** |
||
205 | * @return int |
||
206 | */ |
||
207 | public function getMemorySwap() |
||
211 | |||
212 | /** |
||
213 | * @param int $memorySwap |
||
214 | * |
||
215 | * @return self |
||
216 | */ |
||
217 | public function setMemorySwap($memorySwap = null) |
||
223 | |||
224 | /** |
||
225 | * @return int |
||
226 | */ |
||
227 | public function getCpuShares() |
||
231 | |||
232 | /** |
||
233 | * @param int $cpuShares |
||
234 | * |
||
235 | * @return self |
||
236 | */ |
||
237 | public function setCpuShares($cpuShares = null) |
||
243 | |||
244 | /** |
||
245 | * @return int |
||
246 | */ |
||
247 | public function getCpuPeriod() |
||
251 | |||
252 | /** |
||
253 | * @param int $cpuPeriod |
||
254 | * |
||
255 | * @return self |
||
256 | */ |
||
257 | public function setCpuPeriod($cpuPeriod = null) |
||
263 | |||
264 | /** |
||
265 | * @return string |
||
266 | */ |
||
267 | public function getCpusetCpus() |
||
271 | |||
272 | /** |
||
273 | * @param string $cpusetCpus |
||
274 | * |
||
275 | * @return self |
||
276 | */ |
||
277 | public function setCpusetCpus($cpusetCpus = null) |
||
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getCpusetMems() |
||
291 | |||
292 | /** |
||
293 | * @param string $cpusetMems |
||
294 | * |
||
295 | * @return self |
||
296 | */ |
||
297 | public function setCpusetMems($cpusetMems = null) |
||
303 | |||
304 | /** |
||
305 | * @return int |
||
306 | */ |
||
307 | public function getBlkioWeight() |
||
311 | |||
312 | /** |
||
313 | * @param int $blkioWeight |
||
314 | * |
||
315 | * @return self |
||
316 | */ |
||
317 | public function setBlkioWeight($blkioWeight = null) |
||
323 | |||
324 | /** |
||
325 | * @return int |
||
326 | */ |
||
327 | public function getMemorySwappiness() |
||
331 | |||
332 | /** |
||
333 | * @param int $memorySwappiness |
||
334 | * |
||
335 | * @return self |
||
336 | */ |
||
337 | public function setMemorySwappiness($memorySwappiness = null) |
||
343 | |||
344 | /** |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function getOomKillDisable() |
||
351 | |||
352 | /** |
||
353 | * @param bool $oomKillDisable |
||
354 | * |
||
355 | * @return self |
||
356 | */ |
||
357 | public function setOomKillDisable($oomKillDisable = null) |
||
363 | |||
364 | /** |
||
365 | * @return PortBinding[] |
||
366 | */ |
||
367 | public function getPortBindings() |
||
371 | |||
372 | /** |
||
373 | * @param PortBinding[] $portBindings |
||
374 | * |
||
375 | * @return self |
||
376 | */ |
||
377 | public function setPortBindings(\ArrayObject $portBindings = null) |
||
383 | |||
384 | /** |
||
385 | * @return bool |
||
386 | */ |
||
387 | public function getPublishAllPorts() |
||
391 | |||
392 | /** |
||
393 | * @param bool $publishAllPorts |
||
394 | * |
||
395 | * @return self |
||
396 | */ |
||
397 | public function setPublishAllPorts($publishAllPorts = null) |
||
403 | |||
404 | /** |
||
405 | * @return bool |
||
406 | */ |
||
407 | public function getPrivileged() |
||
411 | |||
412 | /** |
||
413 | * @param bool $privileged |
||
414 | * |
||
415 | * @return self |
||
416 | */ |
||
417 | public function setPrivileged($privileged = null) |
||
423 | |||
424 | /** |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function getReadonlyRootfs() |
||
431 | |||
432 | /** |
||
433 | * @param bool $readonlyRootfs |
||
434 | * |
||
435 | * @return self |
||
436 | */ |
||
437 | public function setReadonlyRootfs($readonlyRootfs = null) |
||
443 | |||
444 | /** |
||
445 | * @return string[] |
||
446 | */ |
||
447 | public function getDns() |
||
451 | |||
452 | /** |
||
453 | * @param string[] $dns |
||
454 | * |
||
455 | * @return self |
||
456 | */ |
||
457 | public function setDns(array $dns = null) |
||
463 | |||
464 | /** |
||
465 | * @return string[] |
||
466 | */ |
||
467 | public function getDnsSearch() |
||
471 | |||
472 | /** |
||
473 | * @param string[] $dnsSearch |
||
474 | * |
||
475 | * @return self |
||
476 | */ |
||
477 | public function setDnsSearch(array $dnsSearch = null) |
||
483 | |||
484 | /** |
||
485 | * @return string[] |
||
486 | */ |
||
487 | public function getExtraHosts() |
||
491 | |||
492 | /** |
||
493 | * @param string[] $extraHosts |
||
494 | * |
||
495 | * @return self |
||
496 | */ |
||
497 | public function setExtraHosts(array $extraHosts = null) |
||
503 | |||
504 | /** |
||
505 | * @return string[] |
||
506 | */ |
||
507 | public function getVolumesFrom() |
||
511 | |||
512 | /** |
||
513 | * @param string[] $volumesFrom |
||
514 | * |
||
515 | * @return self |
||
516 | */ |
||
517 | public function setVolumesFrom(array $volumesFrom = null) |
||
523 | |||
524 | /** |
||
525 | * @return string[] |
||
526 | */ |
||
527 | public function getCapAdd() |
||
531 | |||
532 | /** |
||
533 | * @param string[] $capAdd |
||
534 | * |
||
535 | * @return self |
||
536 | */ |
||
537 | public function setCapAdd(array $capAdd = null) |
||
543 | |||
544 | /** |
||
545 | * @return string[] |
||
546 | */ |
||
547 | public function getCapDrop() |
||
551 | |||
552 | /** |
||
553 | * @param string[] $capDrop |
||
554 | * |
||
555 | * @return self |
||
556 | */ |
||
557 | public function setCapDrop(array $capDrop = null) |
||
563 | |||
564 | /** |
||
565 | * @return RestartPolicy |
||
566 | */ |
||
567 | public function getRestartPolicy() |
||
571 | |||
572 | /** |
||
573 | * @param RestartPolicy $restartPolicy |
||
574 | * |
||
575 | * @return self |
||
576 | */ |
||
577 | public function setRestartPolicy(RestartPolicy $restartPolicy = null) |
||
583 | |||
584 | /** |
||
585 | * @return string |
||
586 | */ |
||
587 | public function getNetworkMode() |
||
591 | |||
592 | /** |
||
593 | * @param string $networkMode |
||
594 | * |
||
595 | * @return self |
||
596 | */ |
||
597 | public function setNetworkMode($networkMode = null) |
||
603 | |||
604 | /** |
||
605 | * @return Device[] |
||
606 | */ |
||
607 | public function getDevices() |
||
611 | |||
612 | /** |
||
613 | * @param Device[] $devices |
||
614 | * |
||
615 | * @return self |
||
616 | */ |
||
617 | public function setDevices(array $devices = null) |
||
623 | |||
624 | /** |
||
625 | * @return Ulimit[] |
||
626 | */ |
||
627 | public function getUlimits() |
||
631 | |||
632 | /** |
||
633 | * @param Ulimit[] $ulimits |
||
634 | * |
||
635 | * @return self |
||
636 | */ |
||
637 | public function setUlimits(array $ulimits = null) |
||
643 | |||
644 | /** |
||
645 | * @return string[] |
||
646 | */ |
||
647 | public function getSecurityOpt() |
||
651 | |||
652 | /** |
||
653 | * @param string[] $securityOpt |
||
654 | * |
||
655 | * @return self |
||
656 | */ |
||
657 | public function setSecurityOpt(array $securityOpt = null) |
||
663 | |||
664 | /** |
||
665 | * @return LogConfig |
||
666 | */ |
||
667 | public function getLogConfig() |
||
671 | |||
672 | /** |
||
673 | * @param LogConfig $logConfig |
||
674 | * |
||
675 | * @return self |
||
676 | */ |
||
677 | public function setLogConfig(LogConfig $logConfig = null) |
||
683 | |||
684 | /** |
||
685 | * @return string |
||
686 | */ |
||
687 | public function getCgroupParent() |
||
691 | |||
692 | /** |
||
693 | * @param string $cgroupParent |
||
694 | * |
||
695 | * @return self |
||
696 | */ |
||
697 | public function setCgroupParent($cgroupParent = null) |
||
703 | } |
||
704 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.