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 = [ |
||
| 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 = [ |
||
| 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 | 19 | 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 | 11 | public function start($timeout_ms = 3000) |
|
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Stop the standalone server |
||
| 183 | * |
||
| 184 | * @throws Exception\RuntimeException |
||
| 185 | * @param boolean $throws_exception wether to throw exception if pid or process cannot be found or killed. |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | 12 | public function stop($throws_exception = false) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Tells whether the standalone server is started |
||
| 240 | * @return boolean |
||
| 241 | */ |
||
| 242 | 11 | public function isStarted() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Return command used to start the standalone server |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | 11 | public function getCommand() |
|
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Get runnin standalone server pid number |
||
| 279 | * |
||
| 280 | * @throws Exception\PidNotFoundException |
||
| 281 | * @return int |
||
| 282 | */ |
||
| 283 | 12 | public function getPid() |
|
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Return the content of the output_file |
||
| 301 | * @throws \RuntimeException |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 3 | public function getOutput() |
|
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Test whether the standalone server process |
||
| 319 | * is effectively running |
||
| 320 | * |
||
| 321 | * @throws Exception\PidNotFoundException |
||
| 322 | * @param $throwsException if false discard exception if pidfile not exists |
||
| 323 | * @return boolean |
||
| 324 | */ |
||
| 325 | 11 | public function isProcessRunning($throwsException = false) |
|
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Restart the standalone server |
||
| 345 | */ |
||
| 346 | 2 | public function restart() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Return port on which standalone server listens |
||
| 354 | * @return int |
||
| 355 | */ |
||
| 356 | 12 | public function getServerPort() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Set port on which standalone server listens. |
||
| 363 | * |
||
| 364 | * @param int $port |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | 14 | protected function setServerPort($port) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Return standalone configuration |
||
| 374 | * |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | 7 | public function getConfig() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Return default configuration options |
||
| 384 | * @param int $port |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | 15 | protected function getDefaultConfig($port) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Check configuration parameters |
||
| 401 | * @throws Exception\InvalidArgumentException |
||
| 402 | * @param array $config |
||
| 403 | */ |
||
| 404 | 15 | protected function checkConfigRequiredArgs(array $config) |
|
| 452 | } |
||
| 453 |
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.