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 |
||
10 | class StandaloneServer |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var int |
||
15 | */ |
||
16 | protected $port; |
||
17 | |||
18 | /** |
||
19 | * |
||
20 | * @var StandaloneServer\Config |
||
21 | */ |
||
22 | protected $config; |
||
23 | |||
24 | /** |
||
25 | * Tells whether the standalone server is started |
||
26 | * @var boolean |
||
27 | */ |
||
28 | protected $started = false; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * |
||
33 | * @var PortTester |
||
34 | */ |
||
35 | protected $portTester; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * @var |
||
40 | */ |
||
41 | protected $logger; |
||
42 | |||
43 | /** |
||
44 | * Constructor |
||
45 | * |
||
46 | * <code> |
||
47 | * |
||
48 | * $config = array( |
||
49 | * 'port' => 8089, |
||
50 | * |
||
51 | * // optionally |
||
52 | * 'server_jar' => 'path/to/JavaBridge.jar' |
||
53 | * 'java_bin' => 'path/to/java' |
||
54 | * ); |
||
55 | * $server = new StandaloneServer($config); |
||
56 | * |
||
57 | * </code> |
||
58 | * |
||
59 | * @throws Exception\InvalidArgumentException |
||
60 | * @param StandaloneServer\Config $config |
||
61 | * @param LoggerInterface $logger |
||
62 | * |
||
63 | */ |
||
64 | public function __construct(StandaloneServer\Config $config, LoggerInterface $logger=null) |
||
82 | |||
83 | /** |
||
84 | * Start the standalone server |
||
85 | * |
||
86 | * @throws Exception\RuntimeException |
||
87 | * |
||
88 | * @param int $timeout_ms maximum number of milliseconds to wait for server start |
||
89 | * @return void |
||
90 | */ |
||
91 | public function start($timeout_ms = 3000) |
||
160 | |||
161 | |||
162 | |||
163 | /** |
||
164 | * Stop the standalone server |
||
165 | * |
||
166 | * @throws Exception\RuntimeException |
||
167 | * @param boolean $throws_exception wether to throw exception if pid or process cannot be found or killed. |
||
168 | * @return void |
||
169 | */ |
||
170 | public function stop($throws_exception = false) |
||
223 | |||
224 | /** |
||
225 | * Tells whether the standalone server is started |
||
226 | * @return boolean |
||
227 | */ |
||
228 | public function isStarted() |
||
232 | |||
233 | /** |
||
234 | * Return command used to start the standalone server |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getCommand() |
||
272 | |||
273 | |||
274 | /** |
||
275 | * Get standalone server pid number as it was stored during last start |
||
276 | * |
||
277 | * @throws Exception\PidNotFoundException |
||
278 | * @return int |
||
279 | */ |
||
280 | public function getPid() |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Return the content of the output_file |
||
300 | * @throws Exception\RuntimeException |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getOutput() |
||
318 | |||
319 | |||
320 | /** |
||
321 | * Test whether the standalone server process |
||
322 | * is effectively running |
||
323 | * |
||
324 | * @throws Exception\PidNotFoundException |
||
325 | * @param boolean $throwsException if false discard exception if pidfile not exists |
||
326 | * @return boolean |
||
327 | */ |
||
328 | public function isProcessRunning($throwsException = false) |
||
346 | |||
347 | |||
348 | /** |
||
349 | * Restart the standalone server |
||
350 | */ |
||
351 | public function restart() |
||
356 | |||
357 | /** |
||
358 | * Return underlying configuration object |
||
359 | * @return StandaloneServer\Config |
||
360 | */ |
||
361 | public function getConfig() |
||
365 | } |
||
366 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.