Complex classes like Command 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 Command, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Command |
||
16 | { |
||
17 | /** |
||
18 | * Pid file. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $pidFile; |
||
23 | |||
24 | /** |
||
25 | * Command options. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $options = []; |
||
30 | |||
31 | /** |
||
32 | * Server host. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $host = '127.0.0.1'; |
||
37 | |||
38 | /** |
||
39 | * Server port. |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $port = 8083; |
||
44 | |||
45 | /** |
||
46 | * Application bootstrap file. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $bootstrap = 'bootstrap/app.php'; |
||
51 | |||
52 | /** |
||
53 | * Http server options. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $serverOptions = []; |
||
58 | |||
59 | /** |
||
60 | * Create a new Command instance. |
||
61 | */ |
||
62 | public function __construct() |
||
63 | { |
||
64 | $this->registerErrorHandling(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Main access. |
||
69 | * |
||
70 | * @param $argv |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | public static function main($argv) |
||
80 | |||
81 | /** |
||
82 | * Run up the server. |
||
83 | * |
||
84 | * @param string $argv |
||
85 | * |
||
86 | * @throws \Exception |
||
87 | */ |
||
88 | public function run($argv) |
||
105 | |||
106 | /** |
||
107 | * Handle command action. |
||
108 | * |
||
109 | * @param array $argv |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function handleAction($argv) |
||
127 | |||
128 | /** |
||
129 | * Handle Command arguments. |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function handleArguments() |
||
194 | |||
195 | /** |
||
196 | * Show usage. |
||
197 | */ |
||
198 | public function usage() |
||
231 | |||
232 | /** |
||
233 | * Print version string. |
||
234 | */ |
||
235 | public function printVersionString() |
||
239 | |||
240 | /** |
||
241 | * Stop the server. |
||
242 | * |
||
243 | * @throws \Exception |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | public function stop() |
||
261 | |||
262 | /** |
||
263 | * Reload the server. |
||
264 | * |
||
265 | * @throws \Exception |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function reload() |
||
273 | |||
274 | /** |
||
275 | * Restart the server. |
||
276 | * |
||
277 | * @throws \Exception |
||
278 | * |
||
279 | * @return void |
||
280 | */ |
||
281 | public function restart() |
||
299 | |||
300 | /** |
||
301 | * Get process identifier of this server. |
||
302 | * |
||
303 | * @throws \Exception |
||
304 | * |
||
305 | * @return string|false |
||
306 | */ |
||
307 | protected function getPid() |
||
327 | |||
328 | /** |
||
329 | * Set the error handling for the application. |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | protected function registerErrorHandling() |
||
351 | |||
352 | /** |
||
353 | * Handle an uncaught exception instance. |
||
354 | * |
||
355 | * @param \Exception $e |
||
356 | * |
||
357 | * @return void |
||
358 | */ |
||
359 | protected function handleUncaughtException($e) |
||
367 | |||
368 | /** |
||
369 | * Handle the application shutdown routine. |
||
370 | * |
||
371 | * @return void |
||
372 | */ |
||
373 | protected function handleShutdown() |
||
385 | |||
386 | /** |
||
387 | * Determine if the error type is fatal. |
||
388 | * |
||
389 | * @param int $type |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | protected function isFatalError($type) |
||
403 | } |
||
404 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: