Complex classes like Config 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 Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Config |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Default configuration options |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $default_config = [ |
||
| 27 | 'java_bin' => 'java', |
||
| 28 | 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
| 29 | 'log_file' => '{base_dir}/var/pjbserver-port{tcp_port}.log', |
||
| 30 | 'pid_file' => '{base_dir}/var/pjbserver-port{tcp_port}.pid', |
||
| 31 | 'classpaths' => [], |
||
| 32 | 'threads' => 50 |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Internal configuration array |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $config; |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Constructor |
||
| 44 | * |
||
| 45 | * <code> |
||
| 46 | * |
||
| 47 | * $params = [ |
||
| 48 | * // Port (required) |
||
| 49 | * 'port' => 8089, |
||
| 50 | * |
||
| 51 | * // Classpath autoloads (optional) |
||
| 52 | * 'classpaths' => [ |
||
| 53 | * '/my/path/to_specific/jar_file.jar', |
||
| 54 | * '/my/path/to_all_jars/*.jar' |
||
| 55 | * ], |
||
| 56 | * |
||
| 57 | * 'threads' => 50, |
||
| 58 | * |
||
| 59 | * // Defaults (optional) |
||
| 60 | * 'java_bin' => 'java', |
||
| 61 | * 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
| 62 | * 'log_file' => '{base_dir}/var/pjbserver-port{tcp_port}.log', |
||
| 63 | * 'pid_file' => '{base_dir}/var/pjbserver-port{tcp_port}.pid' |
||
| 64 | * |
||
| 65 | * ]; |
||
| 66 | * $config = new StandaloneServer\Config($params); |
||
| 67 | * </code> |
||
| 68 | * |
||
| 69 | * @throws Exception\InvalidArgumentException |
||
| 70 | * @param array $config |
||
| 71 | * |
||
| 72 | */ |
||
| 73 | 31 | public function __construct(array $config) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Return port on which standalone server listens |
||
| 91 | * @return int |
||
| 92 | */ |
||
| 93 | 18 | public function getPort() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Return jar file of the server |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 18 | public function getServerJar() |
|
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Return java binary |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 18 | public function getJavaBin() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Return log file |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | 18 | public function getLogFile() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Return an array containing the java classpath(s) for the server |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | 18 | public function getClasspaths() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Return pid file where to store process id |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | 21 | public function getPidFile() |
|
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Return standalone server threads |
||
| 147 | * |
||
| 148 | * @return int|string |
||
| 149 | */ |
||
| 150 | 15 | public function getThreads() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Return standalone configuration |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | 4 | public function getConfig() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Return default configuration options |
||
| 167 | * @param int $port |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 27 | protected function getDefaultConfig($port) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Substitute the placeholder {tcp_port} and {base_dir} |
||
| 177 | * from a config array |
||
| 178 | * |
||
| 179 | * @param array $configArray associative array |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | 27 | protected function substitutePlaceholders(array $configArray, $port) |
|
| 183 | { |
||
| 184 | 27 | $substituted = []; |
|
| 185 | 27 | $base_dir = $this->getBaseDir(); |
|
| 186 | |||
| 187 | 27 | foreach ($configArray as $key => $value) { |
|
| 188 | 27 | $tmp = str_replace('{base_dir}', $base_dir, $value); |
|
| 189 | 27 | $tmp = str_replace('{tcp_port}', $port, $tmp); |
|
| 190 | 27 | $substituted[$key] = $tmp; |
|
| 191 | 27 | } |
|
| 192 | 27 | return $substituted; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Return pjbserver-tools installation base directory |
||
| 197 | * |
||
| 198 | * @throws Exception\RuntimeException |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | 27 | public function getBaseDir() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Check configuration parameters |
||
| 216 | * @throws Exception\InvalidArgumentException |
||
| 217 | * @param array $config |
||
| 218 | */ |
||
| 219 | 27 | protected function checkConfig(array $config) |
|
| 294 | } |
||
| 295 |