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 |
||
12 | class Command |
||
13 | { |
||
14 | protected $pidFile; |
||
15 | |||
16 | protected $options = []; |
||
17 | |||
18 | protected $host = '127.0.0.1'; |
||
19 | |||
20 | protected $port = 8083; |
||
21 | |||
22 | protected $bootstrap = 'bootstrap/app.php'; |
||
23 | |||
24 | protected $serverOptions = []; |
||
25 | |||
26 | public function __construct() |
||
30 | |||
31 | public static function main($argv) |
||
37 | |||
38 | public function run($argv) |
||
53 | |||
54 | /** |
||
55 | * @param array $argv |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | public function handleAction($argv) |
||
73 | |||
74 | public function handleArguments() |
||
135 | |||
136 | /** |
||
137 | * Show usage. |
||
138 | */ |
||
139 | public function usage() |
||
172 | |||
173 | public function printVersionString() |
||
177 | |||
178 | /** |
||
179 | * Stop the server. |
||
180 | * |
||
181 | * @throws \Exception |
||
182 | * |
||
183 | * @return void |
||
184 | */ |
||
185 | public function stop() |
||
199 | |||
200 | /** |
||
201 | * Reload the server. |
||
202 | * |
||
203 | * @throws \Exception |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | public function reload() |
||
211 | |||
212 | /** |
||
213 | * Restart the server. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | public function restart() |
||
235 | |||
236 | /** |
||
237 | * Get process identifier of this server. |
||
238 | * |
||
239 | * @throws \Exception |
||
240 | * |
||
241 | * @return string|false |
||
242 | */ |
||
243 | protected function getPid() |
||
263 | |||
264 | /** |
||
265 | * Set the error handling for the application. |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | protected function registerErrorHandling() |
||
287 | |||
288 | /** |
||
289 | * Handle an uncaught exception instance. |
||
290 | * |
||
291 | * @param \Exception $e |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | protected function handleUncaughtException($e) |
||
303 | |||
304 | /** |
||
305 | * Handle the application shutdown routine. |
||
306 | * |
||
307 | * @return void |
||
308 | */ |
||
309 | protected function handleShutdown() |
||
321 | |||
322 | /** |
||
323 | * Determine if the error type is fatal. |
||
324 | * |
||
325 | * @param int $type |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | protected function isFatalError($type) |
||
339 | } |
||
340 |
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: