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 |
||
70 | class Config implements ConfigInterface, GeneratorInterface |
||
71 | { |
||
72 | 3 | /** |
|
73 | * Array with all certificates |
||
74 | 3 | * |
|
75 | * @var array |
||
76 | */ |
||
77 | 3 | private $certs = []; |
|
78 | |||
79 | /** |
||
80 | * List of all routes available on server |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | private $routes = []; |
||
85 | |||
86 | /** |
||
87 | * List of lines which must be pushed to clients |
||
88 | 3 | * |
|
89 | * @var array |
||
90 | 3 | */ |
|
91 | 3 | private $pushes = []; |
|
92 | 3 | ||
93 | 1 | /** |
|
94 | * All parameters added via addParam method |
||
95 | 3 | * |
|
96 | * @var array |
||
97 | 3 | */ |
|
98 | private $parameters = []; |
||
99 | |||
100 | /** |
||
101 | * Config constructor. |
||
102 | * |
||
103 | * @param array $parameters List of default parameters |
||
104 | */ |
||
105 | public function __construct(array $parameters = []) |
||
109 | 2 | ||
110 | 2 | /** |
|
111 | 2 | * Alias for client line of config |
|
112 | 2 | * |
|
113 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
114 | */ |
||
115 | public function client(): ConfigInterface |
||
119 | |||
120 | /** |
||
121 | * Import content of all listed certificates |
||
122 | * |
||
123 | * @return void |
||
124 | */ |
||
125 | public function loadCertificates(): void |
||
131 | |||
132 | /** |
||
133 | * Alias to setCert |
||
134 | 3 | * |
|
135 | * @deprecated TODO: Delete in future releases |
||
136 | 3 | */ |
|
137 | public function addCert(string $type, string $pathOrContent, bool $isContent = false): ConfigInterface |
||
141 | |||
142 | /** |
||
143 | * Add new cert into the configuration |
||
144 | * |
||
145 | * @param string $type Type of certificate [ca, cert, key, dh, tls-auth] |
||
146 | * @param string $path Absolute or relative path to certificate or content of this file |
||
147 | * @param bool|null $isContent If true, then script will try to load file from dist by $path |
||
148 | * |
||
149 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
150 | * @throws \RuntimeException |
||
151 | */ |
||
152 | public function setCert(string $type, string $path, bool $isContent = null): ConfigInterface |
||
163 | |||
164 | /** |
||
165 | * Return information about specified certificate |
||
166 | * |
||
167 | * @param string $type |
||
168 | * |
||
169 | * @return array |
||
170 | * @throws \RuntimeException |
||
171 | */ |
||
172 | public function getCert(string $type): array |
||
178 | |||
179 | /** |
||
180 | * Alias to setPush |
||
181 | * |
||
182 | * @deprecated TODO: Delete in future releases |
||
183 | */ |
||
184 | public function addPush(string $line): ConfigInterface |
||
188 | |||
189 | /** |
||
190 | * Append new push into the array |
||
191 | * |
||
192 | * @param string $line String with line which must be pushed |
||
193 | * |
||
194 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
195 | */ |
||
196 | public function setPush(string $line): ConfigInterface |
||
201 | |||
202 | /** |
||
203 | * Remove route line from push array |
||
204 | * |
||
205 | * @param string $line String with line which must be pushed |
||
206 | * |
||
207 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
208 | */ |
||
209 | public function unsetPush(string $line): ConfigInterface |
||
214 | |||
215 | /** |
||
216 | * Append new route into the array |
||
217 | * |
||
218 | * @param string $line String with route |
||
219 | * |
||
220 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
221 | */ |
||
222 | public function setRoute(string $line): ConfigInterface |
||
227 | |||
228 | /** |
||
229 | * Remove route line from routes array |
||
230 | * |
||
231 | * @param string $line String with route |
||
232 | * |
||
233 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
234 | */ |
||
235 | public function unsetRoute(string $line): ConfigInterface |
||
240 | |||
241 | /** |
||
242 | * Alias to set |
||
243 | * |
||
244 | * @deprecated TODO: Delete in future releases |
||
245 | */ |
||
246 | public function add(string $name, $value = null): ConfigInterface |
||
250 | |||
251 | /** |
||
252 | * Add some new parameter to the list of parameters |
||
253 | * |
||
254 | * @param string $name Name of parameter |
||
255 | * @param string|bool|null $value Value of parameter |
||
256 | * |
||
257 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
258 | * @example $this->add('client')->add('remote', 'vpn.example.com'); |
||
259 | */ |
||
260 | public function set(string $name, $value = null): ConfigInterface |
||
294 | |||
295 | /** |
||
296 | * Get some custom element |
||
297 | * |
||
298 | * @param string|null $name Name of parameter |
||
299 | * |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function get(string $name) |
||
306 | |||
307 | /** |
||
308 | * Generate config by parameters in memory |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public function generate(): string |
||
317 | |||
318 | /** |
||
319 | * @param string $name |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function __isset(string $name): bool |
||
337 | |||
338 | /** |
||
339 | * @param string $name |
||
340 | * @param string|bool|integer|null $value |
||
341 | */ |
||
342 | public function __set(string $name, $value = null): void |
||
347 | |||
348 | /** |
||
349 | * @param string $name |
||
350 | * |
||
351 | * @return string|bool|null |
||
352 | */ |
||
353 | public function __get(string $name) |
||
357 | |||
358 | /** |
||
359 | * Remove some parameter from array by name |
||
360 | * |
||
361 | * @param string $name Name of parameter |
||
362 | * |
||
363 | * @return void |
||
364 | * @throws \RuntimeException |
||
365 | */ |
||
366 | public function __unset(string $name) |
||
392 | |||
393 | /** |
||
394 | * Remove selected certificate from array |
||
395 | * |
||
396 | * @param string $type Type of certificate [ca, cert, key, dh, tls-auth] |
||
397 | * |
||
398 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
399 | * @throws \RuntimeException |
||
400 | */ |
||
401 | public function unsetCert(string $type): ConfigInterface |
||
408 | |||
409 | /** |
||
410 | * Set scope of certs |
||
411 | * |
||
412 | * @param \OpenVPN\Types\Cert[] $certs |
||
413 | * @param bool $loadCertificates |
||
414 | * |
||
415 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
416 | */ |
||
417 | public function setCerts(array $certs, bool $loadCertificates = false): ConfigInterface |
||
431 | |||
432 | /** |
||
433 | * Set scope of unique pushes |
||
434 | * |
||
435 | * @param \OpenVPN\Types\Push[] $pushes |
||
436 | * |
||
437 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
438 | */ |
||
439 | public function setPushes(array $pushes): ConfigInterface |
||
447 | |||
448 | /** |
||
449 | * Set scope of unique routes |
||
450 | * |
||
451 | * @param \OpenVPN\Types\Route[] $routes |
||
452 | * |
||
453 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
454 | */ |
||
455 | public function setRoutes(array $routes): ConfigInterface |
||
463 | |||
464 | /** |
||
465 | * Set scope of unique parameters |
||
466 | * |
||
467 | * @param \OpenVPN\Types\Parameter[] $parameters |
||
468 | * |
||
469 | * @return \OpenVPN\Interfaces\ConfigInterface |
||
470 | */ |
||
471 | public function setParams(array $parameters): ConfigInterface |
||
479 | |||
480 | /** |
||
481 | * Export array of all certificates |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | public function getCerts(): array |
||
489 | |||
490 | /** |
||
491 | * Export array of all pushes |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | public function getPushes(): array |
||
499 | |||
500 | /** |
||
501 | * Export array of all routes |
||
502 | * |
||
503 | * @return array |
||
504 | */ |
||
505 | public function getRoutes(): array |
||
509 | |||
510 | /** |
||
511 | * Export array of all parameters |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | public function getParameters(): array |
||
519 | } |
||
520 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.