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 |
||
| 11 | class StandaloneServer |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | protected $port; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * |
||
| 21 | * @var StandaloneServer\Config |
||
| 22 | */ |
||
| 23 | protected $config; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Tells whether the standalone server is started |
||
| 27 | * @var boolean |
||
| 28 | */ |
||
| 29 | protected $started = false; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * |
||
| 34 | * @var PortTester |
||
| 35 | */ |
||
| 36 | protected $portTester; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * @var LoggerInterface |
||
| 41 | */ |
||
| 42 | protected $logger; |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Process |
||
| 47 | */ |
||
| 48 | protected $process; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor |
||
| 52 | * |
||
| 53 | * <code> |
||
| 54 | * |
||
| 55 | * $config = array( |
||
| 56 | * 'port' => 8089, |
||
| 57 | * |
||
| 58 | * // optionally |
||
| 59 | * 'server_jar' => 'path/to/JavaBridge.jar' |
||
| 60 | * 'java_bin' => 'path/to/java' |
||
| 61 | * ); |
||
| 62 | * $server = new StandaloneServer($config); |
||
| 63 | * |
||
| 64 | * </code> |
||
| 65 | * |
||
| 66 | * @throws Exception\InvalidArgumentException |
||
| 67 | * @param StandaloneServer\Config $config |
||
| 68 | * @param LoggerInterface $logger |
||
| 69 | * |
||
| 70 | */ |
||
| 71 | 22 | public function __construct(StandaloneServer\Config $config, LoggerInterface $logger=null) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Start the standalone server |
||
| 94 | * |
||
| 95 | * @throws Exception\RuntimeException |
||
| 96 | * |
||
| 97 | * @param int $timeout_ms maximum number of milliseconds to wait for server start |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | 14 | public function start($timeout_ms = 3000) |
|
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Stop the standalone server |
||
| 172 | * |
||
| 173 | * @throws Exception\StopFailedException |
||
| 174 | * @param boolean $throwException whether to throw exception if pid exists in config but process cannot be found |
||
| 175 | * @param boolean $clearPidFileOnException clear th pid file if the server was not running |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | 17 | public function stop($throwException=false, $clearPidFileOnException=false) |
|
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @throws Exception\FilePermissionException |
||
| 228 | */ |
||
| 229 | protected function clearPidFile() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Tells whether the standalone server is started |
||
| 243 | * |
||
| 244 | * @param boolean $test_is_running |
||
| 245 | * @return boolean |
||
| 246 | */ |
||
| 247 | public function isStarted($test_is_running=true) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Return command used to start the standalone server |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function getCommand() |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * Get standalone server pid number as it was stored during last start |
||
| 300 | * |
||
| 301 | * @throws Exception\PidNotFoundException|ExceptionPidCorruptedException |
||
| 302 | * @return int |
||
| 303 | */ |
||
| 304 | public function getPid() |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Return the content of the output_file |
||
| 324 | * @throws Exception\RuntimeException |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getOutput() |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Test whether the standalone server process |
||
| 346 | * is effectively running |
||
| 347 | * |
||
| 348 | * @throws Exception\PidNotFoundException |
||
| 349 | * @param boolean $throwsException if false discard exception if pidfile not exists |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | public function isProcessRunning($throwsException=false) |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Restart the standalone server |
||
| 375 | */ |
||
| 376 | public function restart() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return underlying configuration object |
||
| 384 | * @return StandaloneServer\Config |
||
| 385 | */ |
||
| 386 | public function getConfig() |
||
| 390 | } |
||
| 391 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.