Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Container |
||
6 | { |
||
7 | /** |
||
8 | * @var string |
||
9 | */ |
||
10 | protected $appArmorProfile; |
||
11 | /** |
||
12 | * @var string[] |
||
13 | */ |
||
14 | protected $args; |
||
15 | /** |
||
16 | * @var ContainerConfig |
||
17 | */ |
||
18 | protected $config; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $created; |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $driver; |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $execDriver; |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $execIDs; |
||
35 | /** |
||
36 | * @var HostConfig |
||
37 | */ |
||
38 | protected $hostConfig; |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $hostnamePath; |
||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $hostsPath; |
||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $logPath; |
||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $id; |
||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $image; |
||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $mountLabel; |
||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $name; |
||
67 | /** |
||
68 | * @var NetworkConfig |
||
69 | */ |
||
70 | protected $networkSettings; |
||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $path; |
||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $processLabel; |
||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $resolvConfPath; |
||
83 | /** |
||
84 | * @var int |
||
85 | */ |
||
86 | protected $restartCount; |
||
87 | /** |
||
88 | * @var ContainerState |
||
89 | */ |
||
90 | protected $state; |
||
91 | /** |
||
92 | * @var Mount[] |
||
93 | */ |
||
94 | protected $mounts; |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getAppArmorProfile() |
||
103 | |||
104 | /** |
||
105 | * @param string $appArmorProfile |
||
106 | * |
||
107 | * @return self |
||
108 | */ |
||
109 | public function setAppArmorProfile($appArmorProfile = null) |
||
115 | |||
116 | /** |
||
117 | * @return string[] |
||
118 | */ |
||
119 | public function getArgs() |
||
123 | |||
124 | /** |
||
125 | * @param string[] $args |
||
126 | * |
||
127 | * @return self |
||
128 | */ |
||
129 | public function setArgs(array $args = null) |
||
135 | |||
136 | /** |
||
137 | * @return ContainerConfig |
||
138 | */ |
||
139 | public function getConfig() |
||
143 | |||
144 | /** |
||
145 | * @param ContainerConfig $config |
||
146 | * |
||
147 | * @return self |
||
148 | */ |
||
149 | public function setConfig(ContainerConfig $config = null) |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | public function getCreated() |
||
163 | |||
164 | /** |
||
165 | * @param string $created |
||
166 | * |
||
167 | * @return self |
||
168 | */ |
||
169 | public function setCreated($created = null) |
||
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getDriver() |
||
183 | |||
184 | /** |
||
185 | * @param string $driver |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | public function setDriver($driver = null) |
||
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getExecDriver() |
||
203 | |||
204 | /** |
||
205 | * @param string $execDriver |
||
206 | * |
||
207 | * @return self |
||
208 | */ |
||
209 | public function setExecDriver($execDriver = null) |
||
215 | |||
216 | /** |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getExecIDs() |
||
223 | |||
224 | /** |
||
225 | * @param string $execIDs |
||
226 | * |
||
227 | * @return self |
||
228 | */ |
||
229 | public function setExecIDs($execIDs = null) |
||
235 | |||
236 | /** |
||
237 | * @return HostConfig |
||
238 | */ |
||
239 | public function getHostConfig() |
||
243 | |||
244 | /** |
||
245 | * @param HostConfig $hostConfig |
||
246 | * |
||
247 | * @return self |
||
248 | */ |
||
249 | public function setHostConfig(HostConfig $hostConfig = null) |
||
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | public function getHostnamePath() |
||
263 | |||
264 | /** |
||
265 | * @param string $hostnamePath |
||
266 | * |
||
267 | * @return self |
||
268 | */ |
||
269 | public function setHostnamePath($hostnamePath = null) |
||
275 | |||
276 | /** |
||
277 | * @return string |
||
278 | */ |
||
279 | public function getHostsPath() |
||
283 | |||
284 | /** |
||
285 | * @param string $hostsPath |
||
286 | * |
||
287 | * @return self |
||
288 | */ |
||
289 | public function setHostsPath($hostsPath = null) |
||
295 | |||
296 | /** |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getLogPath() |
||
303 | |||
304 | /** |
||
305 | * @param string $logPath |
||
306 | * |
||
307 | * @return self |
||
308 | */ |
||
309 | public function setLogPath($logPath = null) |
||
315 | |||
316 | /** |
||
317 | * @return string |
||
318 | */ |
||
319 | public function getId() |
||
323 | |||
324 | /** |
||
325 | * @param string $id |
||
326 | * |
||
327 | * @return self |
||
328 | */ |
||
329 | public function setId($id = null) |
||
335 | |||
336 | /** |
||
337 | * @return string |
||
338 | */ |
||
339 | public function getImage() |
||
343 | |||
344 | /** |
||
345 | * @param string $image |
||
346 | * |
||
347 | * @return self |
||
348 | */ |
||
349 | public function setImage($image = null) |
||
355 | |||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getMountLabel() |
||
363 | |||
364 | /** |
||
365 | * @param string $mountLabel |
||
366 | * |
||
367 | * @return self |
||
368 | */ |
||
369 | public function setMountLabel($mountLabel = null) |
||
375 | |||
376 | /** |
||
377 | * @return string |
||
378 | */ |
||
379 | public function getName() |
||
383 | |||
384 | /** |
||
385 | * @param string $name |
||
386 | * |
||
387 | * @return self |
||
388 | */ |
||
389 | public function setName($name = null) |
||
395 | |||
396 | /** |
||
397 | * @return NetworkConfig |
||
398 | */ |
||
399 | public function getNetworkSettings() |
||
403 | |||
404 | /** |
||
405 | * @param NetworkConfig $networkSettings |
||
406 | * |
||
407 | * @return self |
||
408 | */ |
||
409 | public function setNetworkSettings(NetworkConfig $networkSettings = null) |
||
415 | |||
416 | /** |
||
417 | * @return string |
||
418 | */ |
||
419 | public function getPath() |
||
423 | |||
424 | /** |
||
425 | * @param string $path |
||
426 | * |
||
427 | * @return self |
||
428 | */ |
||
429 | public function setPath($path = null) |
||
435 | |||
436 | /** |
||
437 | * @return string |
||
438 | */ |
||
439 | public function getProcessLabel() |
||
443 | |||
444 | /** |
||
445 | * @param string $processLabel |
||
446 | * |
||
447 | * @return self |
||
448 | */ |
||
449 | public function setProcessLabel($processLabel = null) |
||
455 | |||
456 | /** |
||
457 | * @return string |
||
458 | */ |
||
459 | public function getResolvConfPath() |
||
463 | |||
464 | /** |
||
465 | * @param string $resolvConfPath |
||
466 | * |
||
467 | * @return self |
||
468 | */ |
||
469 | public function setResolvConfPath($resolvConfPath = null) |
||
475 | |||
476 | /** |
||
477 | * @return int |
||
478 | */ |
||
479 | public function getRestartCount() |
||
483 | |||
484 | /** |
||
485 | * @param int $restartCount |
||
486 | * |
||
487 | * @return self |
||
488 | */ |
||
489 | public function setRestartCount($restartCount = null) |
||
495 | |||
496 | /** |
||
497 | * @return ContainerState |
||
498 | */ |
||
499 | public function getState() |
||
503 | |||
504 | /** |
||
505 | * @param ContainerState $state |
||
506 | * |
||
507 | * @return self |
||
508 | */ |
||
509 | public function setState(ContainerState $state = null) |
||
515 | |||
516 | /** |
||
517 | * @return Mount[] |
||
518 | */ |
||
519 | public function getMounts() |
||
523 | |||
524 | /** |
||
525 | * @param Mount[] $mounts |
||
526 | * |
||
527 | * @return self |
||
528 | */ |
||
529 | public function setMounts(array $mounts = null) |
||
535 | } |
||
536 |
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
.