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 | 33 | public function __construct(array $config) |
|
88 | |||
89 | /** |
||
90 | * Return port on which standalone server listens |
||
91 | * @return int |
||
92 | */ |
||
93 | 19 | public function getPort() |
|
97 | |||
98 | /** |
||
99 | * Return jar file of the server |
||
100 | * @return string |
||
101 | */ |
||
102 | 19 | public function getServerJar() |
|
106 | |||
107 | |||
108 | /** |
||
109 | * Return java binary |
||
110 | * @return string |
||
111 | */ |
||
112 | 19 | public function getJavaBin() |
|
116 | |||
117 | /** |
||
118 | * Return log file |
||
119 | * @return string |
||
120 | */ |
||
121 | 19 | public function getLogFile() |
|
125 | |||
126 | /** |
||
127 | * Return an array containing the java classpath(s) for the server |
||
128 | * @return array |
||
129 | */ |
||
130 | 19 | public function getClasspaths() |
|
134 | |||
135 | /** |
||
136 | * Return pid file where to store process id |
||
137 | * @return string |
||
138 | */ |
||
139 | 23 | public function getPidFile() |
|
143 | |||
144 | |||
145 | /** |
||
146 | * Return standalone server threads |
||
147 | * |
||
148 | * @return int|string |
||
149 | */ |
||
150 | 16 | public function getThreads() |
|
154 | |||
155 | /** |
||
156 | * Return standalone configuration |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | 5 | public function toArray() |
|
164 | |||
165 | /** |
||
166 | * Return standalone configuration |
||
167 | * @deprecated use toArray() instead |
||
168 | * @return array |
||
169 | */ |
||
170 | 4 | public function getConfig() |
|
174 | |||
175 | |||
176 | /** |
||
177 | * Return default configuration options |
||
178 | * @param int $port |
||
179 | * @return array |
||
180 | */ |
||
181 | 29 | protected function getDefaultConfig($port) |
|
185 | |||
186 | /** |
||
187 | * Substitute the placeholder {tcp_port} and {base_dir} |
||
188 | * from a config array |
||
189 | * |
||
190 | * @param array $configArray associative array |
||
191 | * @return array |
||
192 | */ |
||
193 | 29 | protected function substitutePlaceholders(array $configArray, $port) |
|
205 | |||
206 | /** |
||
207 | * Return pjbserver-tools installation base directory |
||
208 | * |
||
209 | * @throws Exception\RuntimeException |
||
210 | * @return string |
||
211 | */ |
||
212 | 29 | public function getBaseDir() |
|
224 | |||
225 | /** |
||
226 | * Check configuration parameters |
||
227 | * @throws Exception\InvalidArgumentException |
||
228 | * @param array $config |
||
229 | */ |
||
230 | 29 | protected function checkConfig(array $config) |
|
305 | } |
||
306 |