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 | ); |
||
48 | |||
49 | /** |
||
50 | * Default configuration options |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $default_config = array( |
||
54 | 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
55 | 'java_bin' => 'java', |
||
56 | 'log_file' => '{base_dir}/resources/pjb621_standalone/logs/pjbserver-port{tcp_port}.log', |
||
57 | 'pid_file' => '{base_dir}/resources/pjb621_standalone/var/run/pjbserver-port{tcp_port}.pid' |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @var PortTester |
||
63 | */ |
||
64 | protected $portTester; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Constructor |
||
69 | * |
||
70 | * <code> |
||
71 | * |
||
72 | * $config = array( |
||
73 | * 'port' => 8089, |
||
74 | * |
||
75 | * // optionally |
||
76 | * 'server_jar' => 'path/to/JavaBridge.jar' |
||
77 | * 'java_bin' => 'path/to/java' |
||
78 | * ); |
||
79 | * $server = new StandaloneServer($config); |
||
80 | * |
||
81 | * </code> |
||
82 | * |
||
83 | * @throws Exception\InvalidArgumentException |
||
84 | * @param array $config |
||
85 | */ |
||
86 | 10 | public function __construct(array $config) |
|
106 | |||
107 | /** |
||
108 | * Start the standalone server |
||
109 | * |
||
110 | * @throws Exception\RuntimeException |
||
111 | * @throws Exce |
||
112 | * |
||
113 | * @param int $timeout_ms maximum number of milliseconds to wait for server start |
||
114 | * @return void |
||
115 | */ |
||
116 | 3 | public function start($timeout_ms = 3000) |
|
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * Stop the standalone server |
||
181 | * |
||
182 | * @throws Exception\RuntimeException |
||
183 | * @param boolean $throws_exception wether to throw exception if pid or process cannot be found or killed. |
||
184 | * @return void |
||
185 | */ |
||
186 | 3 | public function stop($throws_exception = false) |
|
223 | |||
224 | /** |
||
225 | * Tells whether the standalone server is started |
||
226 | * @return boolean |
||
227 | */ |
||
228 | 3 | public function isStarted() |
|
232 | |||
233 | /** |
||
234 | * Return command used to start the standalone server |
||
235 | * @return string |
||
236 | */ |
||
237 | 3 | public function getCommand() |
|
255 | |||
256 | |||
257 | /** |
||
258 | * Get runnin standalone server pid number |
||
259 | * |
||
260 | * @throws Exception\RuntimeException |
||
261 | * @return int |
||
262 | */ |
||
263 | 3 | public function getPid() |
|
277 | |||
278 | /** |
||
279 | * Restart the standalone server |
||
280 | */ |
||
281 | public function restart() |
||
286 | |||
287 | /** |
||
288 | * Return port on which standalone server listens |
||
289 | * @return int |
||
290 | */ |
||
291 | 4 | public function getServerPort() |
|
295 | |||
296 | /** |
||
297 | * Set port on which standalone server listens. |
||
298 | * |
||
299 | * @param int $port |
||
300 | * @return void |
||
301 | */ |
||
302 | 5 | protected function setServerPort($port) |
|
306 | |||
307 | /** |
||
308 | * Return standalone configuration |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | 3 | public function getConfig() |
|
316 | |||
317 | /** |
||
318 | * Return default configuration options |
||
319 | * @param int $port |
||
320 | * @return array |
||
321 | */ |
||
322 | 6 | protected function getDefaultConfig($port) |
|
333 | |||
334 | /** |
||
335 | * Check configuration parameters |
||
336 | * @throws Exception\InvalidArgumentException |
||
337 | * @param array $config |
||
338 | */ |
||
339 | 6 | protected function checkConfigRequiredArgs(array $config) |
|
379 | } |
||
380 |