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 | * Default configuration options. |
||
23 | * |
||
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 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $config; |
||
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 | * |
||
71 | * @param array $config |
||
72 | */ |
||
73 | 33 | public function __construct(array $config) |
|
88 | |||
89 | /** |
||
90 | * Return port on which standalone server listens. |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | 19 | public function getPort() |
|
95 | { |
||
96 | 19 | return $this->config['port']; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Return jar file of the server. |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 19 | public function getServerJar() |
|
105 | { |
||
106 | 19 | return $this->config['server_jar']; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Return java binary. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 19 | public function getJavaBin() |
|
115 | { |
||
116 | 19 | return $this->config['java_bin']; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Return log file. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 19 | public function getLogFile() |
|
125 | { |
||
126 | 19 | return $this->config['log_file']; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Return an array containing the java classpath(s) for the server. |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 19 | public function getClasspaths() |
|
135 | { |
||
136 | 19 | return $this->config['classpaths']; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Return pid file where to store process id. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 23 | public function getPidFile() |
|
145 | { |
||
146 | 23 | return $this->config['pid_file']; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Return standalone server threads. |
||
151 | * |
||
152 | * @return int|string |
||
153 | */ |
||
154 | 16 | public function getThreads() |
|
155 | { |
||
156 | 16 | return $this->config['threads']; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Return standalone configuration. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | 5 | public function toArray() |
|
165 | { |
||
166 | 5 | return $this->config; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Return standalone configuration. |
||
171 | * |
||
172 | * @deprecated use toArray() instead |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | public function getConfig() |
||
180 | |||
181 | /** |
||
182 | * Return default configuration options. |
||
183 | * |
||
184 | * @param int $port |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | 29 | protected function getDefaultConfig($port) |
|
192 | |||
193 | /** |
||
194 | * Substitute the placeholder {tcp_port} and {base_dir} |
||
195 | * from a config array. |
||
196 | * |
||
197 | * @param array $configArray associative array |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 29 | protected function substitutePlaceholders(array $configArray, $port) |
|
214 | |||
215 | /** |
||
216 | * Return pjbserver-tools installation base directory. |
||
217 | * |
||
218 | * @throws Exception\RuntimeException |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | 29 | public function getBaseDir() |
|
235 | |||
236 | /** |
||
237 | * Check configuration parameters. |
||
238 | * |
||
239 | * @throws Exception\InvalidArgumentException |
||
240 | * |
||
241 | * @param array $config |
||
242 | */ |
||
243 | 29 | protected function checkConfig(array $config) |
|
318 | } |
||
319 |