Complex classes like StandaloneServer 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 StandaloneServer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class StandaloneServer |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $port; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $config; |
||
32 | |||
33 | /** |
||
34 | * Tells whether the standalone server is started |
||
35 | * @var boolean |
||
36 | */ |
||
37 | protected $started = false; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $required_arguments = array( |
||
43 | 'port' => 'FILTER_VALIDATE_INT', |
||
44 | 'server_jar' => 'existing_file', |
||
45 | 'log_file' => 'file_with_existing_directory', |
||
46 | 'pid_file' => 'file_with_existing_directory', |
||
47 | 'autoload_path' => 'existing_directory' |
||
48 | ); |
||
49 | |||
50 | /** |
||
51 | * Default configuration options |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $default_config = array( |
||
55 | 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
56 | 'java_bin' => 'java', |
||
57 | 'log_file' => '{base_dir}/resources/pjb621_standalone/logs/pjbserver-port{tcp_port}.log', |
||
58 | 'pid_file' => '{base_dir}/resources/pjb621_standalone/var/run/pjbserver-port{tcp_port}.pid', |
||
59 | 'autoload_path' => '{base_dir}/resources/autoload' |
||
60 | ); |
||
61 | |||
62 | /** |
||
63 | * |
||
64 | * @var PortTester |
||
65 | */ |
||
66 | protected $portTester; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Constructor |
||
71 | * |
||
72 | * <code> |
||
73 | * |
||
74 | * $config = array( |
||
75 | * 'port' => 8089, |
||
76 | * |
||
77 | * // optionally |
||
78 | * 'server_jar' => 'path/to/JavaBridge.jar' |
||
79 | * 'java_bin' => 'path/to/java' |
||
80 | * ); |
||
81 | * $server = new StandaloneServer($config); |
||
82 | * |
||
83 | * </code> |
||
84 | * |
||
85 | * @throws Exception\InvalidArgumentException |
||
86 | * @param array $config |
||
87 | */ |
||
88 | 11 | public function __construct(array $config) |
|
110 | |||
111 | /** |
||
112 | * Start the standalone server |
||
113 | * |
||
114 | * @throws Exception\RuntimeException |
||
115 | * @throws Exce |
||
116 | * |
||
117 | * @param int $timeout_ms maximum number of milliseconds to wait for server start |
||
118 | * @return void |
||
119 | */ |
||
120 | 4 | public function start($timeout_ms = 3000) |
|
179 | |||
180 | |||
181 | |||
182 | /** |
||
183 | * Stop the standalone server |
||
184 | * |
||
185 | * @throws Exception\RuntimeException |
||
186 | * @param boolean $throws_exception wether to throw exception if pid or process cannot be found or killed. |
||
187 | * @return void |
||
188 | */ |
||
189 | 4 | public function stop($throws_exception = false) |
|
226 | |||
227 | /** |
||
228 | * Tells whether the standalone server is started |
||
229 | * @return boolean |
||
230 | */ |
||
231 | 4 | public function isStarted() |
|
235 | |||
236 | /** |
||
237 | * Return command used to start the standalone server |
||
238 | * @return string |
||
239 | */ |
||
240 | 4 | public function getCommand() |
|
264 | |||
265 | |||
266 | /** |
||
267 | * Get runnin standalone server pid number |
||
268 | * |
||
269 | * @throws Exception\RuntimeException |
||
270 | * @return int |
||
271 | */ |
||
272 | 4 | public function getPid() |
|
286 | |||
287 | |||
288 | /** |
||
289 | * Return the content of the output_file |
||
290 | * @throws \RuntimeException |
||
291 | * @return string |
||
292 | */ |
||
293 | 1 | public function getOutput() |
|
303 | |||
304 | /** |
||
305 | * Restart the standalone server |
||
306 | */ |
||
307 | public function restart() |
||
312 | |||
313 | /** |
||
314 | * Return port on which standalone server listens |
||
315 | * @return int |
||
316 | */ |
||
317 | 5 | public function getServerPort() |
|
321 | |||
322 | /** |
||
323 | * Set port on which standalone server listens. |
||
324 | * |
||
325 | * @param int $port |
||
326 | * @return void |
||
327 | */ |
||
328 | 6 | protected function setServerPort($port) |
|
332 | |||
333 | /** |
||
334 | * Return standalone configuration |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | 4 | public function getConfig() |
|
342 | |||
343 | /** |
||
344 | * Return default configuration options |
||
345 | * @param int $port |
||
346 | * @return array |
||
347 | */ |
||
348 | 7 | protected function getDefaultConfig($port) |
|
359 | |||
360 | /** |
||
361 | * Check configuration parameters |
||
362 | * @throws Exception\InvalidArgumentException |
||
363 | * @param array $config |
||
364 | */ |
||
365 | 7 | protected function checkConfigRequiredArgs(array $config) |
|
413 | } |
||
414 |