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 |
||
17 | class StandaloneServer |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $port; |
||
24 | |||
25 | /** |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $config; |
||
30 | |||
31 | /** |
||
32 | * Tells whether the standalone server is started |
||
33 | * @var boolean |
||
34 | */ |
||
35 | protected $started = false; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $required_arguments = array( |
||
41 | 'port' => 'FILTER_VALIDATE_INT', |
||
42 | 'server_jar' => 'existing_file', |
||
43 | 'log_file' => 'file_with_existing_directory', |
||
44 | 'pid_file' => 'file_with_existing_directory', |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * Default configuration options |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $default_config = array( |
||
52 | 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
53 | 'java_bin' => 'java', |
||
54 | 'log_file' => '{base_dir}/resources/pjb621_standalone/logs/pjbserver-port{tcp_port}.log', |
||
55 | 'pid_file' => '{base_dir}/resources/pjb621_standalone/var/run/pjbserver-port{tcp_port}.pid' |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Constructor |
||
60 | * |
||
61 | * <code> |
||
62 | * |
||
63 | * $config = array( |
||
64 | * 'port' => 8089, |
||
65 | * |
||
66 | * // optionally |
||
67 | * 'server_jar' => 'path/to/JavaBridge.jar' |
||
68 | * 'java_bin' => 'path/to/java' |
||
69 | * ); |
||
70 | * $server = new StandaloneServer($config); |
||
71 | * |
||
72 | * </code> |
||
73 | * |
||
74 | * @throws Exception\InvalidArgumentException |
||
75 | * @param array $config |
||
76 | */ |
||
77 | 10 | public function __construct(array $config) |
|
89 | |||
90 | /** |
||
91 | * Start the standalone server |
||
92 | * |
||
93 | * @throws Exception\RuntimeException |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | 3 | public function start() |
|
149 | |||
150 | /** |
||
151 | * Check if TCP port is available for binding |
||
152 | * |
||
153 | * @param int $port |
||
154 | * @param int $timeout |
||
155 | */ |
||
156 | 3 | protected function isPortAvailable($host, $port, $timeout = 1) |
|
167 | |||
168 | /** |
||
169 | * Return command used to start the standalone server |
||
170 | * @return string |
||
171 | */ |
||
172 | 3 | public function getCommand() |
|
190 | |||
191 | /** |
||
192 | * Stop the standalone server |
||
193 | * |
||
194 | * @throws Exception\RuntimeException |
||
195 | * @param boolean $throws_exception wether to throw exception if pid or process cannot be found or killed. |
||
196 | * @return void |
||
197 | */ |
||
198 | 3 | public function stop($throws_exception = false) |
|
235 | |||
236 | /** |
||
237 | * Tells whether the standalone server is started |
||
238 | * @return boolean |
||
239 | */ |
||
240 | 3 | public function isStarted() |
|
244 | |||
245 | /** |
||
246 | * Get runnin standalone server pid number |
||
247 | * |
||
248 | * @throws Exception\RuntimeException |
||
249 | * @return int |
||
250 | */ |
||
251 | 3 | public function getPid() |
|
265 | |||
266 | /** |
||
267 | * Restart the standalone server |
||
268 | */ |
||
269 | public function restart() |
||
274 | |||
275 | /** |
||
276 | * Return port on which standalone server listens |
||
277 | * @return int |
||
278 | */ |
||
279 | 4 | public function getServerPort() |
|
283 | |||
284 | /** |
||
285 | * Set port on which standalone server listens. |
||
286 | * |
||
287 | * @param int $port |
||
288 | * @return void |
||
289 | */ |
||
290 | 5 | protected function setServerPort($port) |
|
294 | |||
295 | /** |
||
296 | * Return standalone configuration |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | 3 | public function getConfig() |
|
304 | |||
305 | /** |
||
306 | * Return default configuration options |
||
307 | * @param int $port |
||
308 | * @return array |
||
309 | */ |
||
310 | 6 | protected function getDefaultConfig($port) |
|
321 | |||
322 | /** |
||
323 | * Check configuration parameters |
||
324 | * @throws Exception\InvalidArgumentException |
||
325 | * @param array $config |
||
326 | */ |
||
327 | 6 | protected function checkConfigRequiredArgs(array $config) |
|
367 | } |
||
368 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.