Total Complexity | 43 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
25 | class Config |
||
26 | { |
||
27 | public const DEFAULT_INTERFACE = '0.0.0.0'; |
||
28 | public const DEFAULT_PORT = 8086; |
||
29 | public const DEFAULT_PHIREMOCK_PATH = 'vendor/bin/phiremock'; |
||
30 | public const DEFAULT_DELAY = 0; |
||
31 | public const DEFAULT_DEBUG_MODE = false; |
||
32 | public const DEFAULT_EXPECTATIONS_PATH = null; |
||
33 | public const DEFAULT_CERTIFICATE = null; |
||
34 | public const DEFAULT_CERTIFICATE_KEY = null; |
||
35 | public const DEFAULT_CERTIFICATE_PASSPHRASE = null; |
||
36 | public const DEFAULT_SERVER_FACTORY = 'default'; |
||
37 | public const DEFAULT_EXTRA_INSTANCES = []; |
||
38 | public const DEFAULT_SUITES = []; |
||
39 | public const DEFAULT_WAIT_UNTIL_READY = false; |
||
40 | public const DEFAULT_WAIT_UNTIL_READY_TIMEOUT = 30; |
||
41 | |||
42 | public const DEFAULT_CONFIG = [ |
||
43 | 'listen' => self::DEFAULT_INTERFACE . ':' . self::DEFAULT_PORT, |
||
44 | 'debug' => self::DEFAULT_DEBUG_MODE, |
||
45 | 'start_delay' => self::DEFAULT_DELAY, |
||
46 | 'bin_path' => self::DEFAULT_PHIREMOCK_PATH, |
||
47 | 'expectations_path' => self::DEFAULT_EXPECTATIONS_PATH, |
||
48 | 'server_factory' => self::DEFAULT_SERVER_FACTORY, |
||
49 | 'certificate' => self::DEFAULT_CERTIFICATE, |
||
50 | 'certificate_key' => self::DEFAULT_CERTIFICATE_KEY, |
||
51 | 'cert_passphrase' => self::DEFAULT_CERTIFICATE_PASSPHRASE, |
||
52 | 'extra_instances' => self::DEFAULT_EXTRA_INSTANCES, |
||
53 | 'suites' => self::DEFAULT_SUITES, |
||
54 | 'wait_until_ready' => self::DEFAULT_WAIT_UNTIL_READY, |
||
55 | 'wait_until_ready_timeout' => self::DEFAULT_WAIT_UNTIL_READY_TIMEOUT |
||
56 | ]; |
||
57 | |||
58 | /** @var string */ |
||
59 | private $interface; |
||
60 | /** @var int */ |
||
61 | private $port; |
||
62 | /** @var int */ |
||
63 | private $delay; |
||
64 | /** @var bool */ |
||
65 | private $debug; |
||
66 | /** @var Path */ |
||
67 | private $phiremockPath; |
||
68 | /** @var Path */ |
||
69 | private $expectationsPath; |
||
70 | /** @var Path */ |
||
71 | private $logsPath; |
||
72 | /** @var string */ |
||
73 | private $serverFactory; |
||
74 | /** @var Path */ |
||
75 | private $certificate; |
||
76 | /** @var Path */ |
||
77 | private $certificateKey; |
||
78 | /** @var string */ |
||
79 | private $certificatePassphrase; |
||
80 | /** @var Config[] */ |
||
81 | private $extraInstances; |
||
82 | /** @var string[] */ |
||
83 | private $suites; |
||
84 | /** @var callable */ |
||
85 | private $output; |
||
86 | /** @var bool */ |
||
87 | private $waitUntilReady; |
||
88 | /** @var int */ |
||
89 | private $waitUntilReadyTimeout; |
||
90 | |||
91 | /** @throws ConfigurationException */ |
||
92 | public function __construct(array $config, callable $output) |
||
93 | { |
||
94 | $this->output = $output; |
||
95 | $this->initInterfaceAndPort($config); |
||
96 | $this->initExpectationsPath($config); |
||
97 | $this->initServerFactory($config); |
||
98 | $this->initDelay($config); |
||
99 | $this->phiremockPath = new Path($config['bin_path']); |
||
100 | $this->logsPath = new Path($config['logs_path']); |
||
101 | $this->debug = (bool) $config['debug']; |
||
102 | $this->initCertificatePath($config); |
||
103 | $this->initCertificateKeyPath($config); |
||
104 | $this->certificatePassphrase = $config['cert_passphrase']; |
||
105 | $this->initExtraInstances($config); |
||
106 | $this->suites = $config['suites']; |
||
107 | $this->waitUntilReady = (bool) $config['wait_until_ready']; |
||
108 | $this->waitUntilReadyTimeout = (int) $config['wait_until_ready_timeout']; |
||
109 | } |
||
110 | |||
111 | public function getSuites(): array |
||
112 | { |
||
113 | return $this->suites; |
||
114 | } |
||
115 | |||
116 | public function getInterface(): string |
||
117 | { |
||
118 | return $this->interface; |
||
119 | } |
||
120 | |||
121 | public function getPort(): string |
||
122 | { |
||
123 | return $this->port; |
||
124 | } |
||
125 | |||
126 | public function isDebugMode(): bool |
||
127 | { |
||
128 | return $this->debug; |
||
129 | } |
||
130 | |||
131 | public function getStartDelay(): string |
||
132 | { |
||
133 | return $this->delay; |
||
134 | } |
||
135 | |||
136 | public function getPhiremockPath(): string |
||
137 | { |
||
138 | return $this->phiremockPath->absoluteOrRelativeToCodeceptionDir(); |
||
139 | } |
||
140 | |||
141 | public function getExpectationsPath(): ?string |
||
142 | { |
||
143 | return $this->expectationsPath ? $this->expectationsPath->absoluteOrRelativeToCodeceptionDir() : null; |
||
144 | } |
||
145 | |||
146 | public function getCertificatePath(): ?string |
||
147 | { |
||
148 | return $this->certificate ? $this->certificate->absoluteOrRelativeToCodeceptionDir() : null; |
||
149 | } |
||
150 | |||
151 | public function getCertificateKeyPath(): ?string |
||
152 | { |
||
153 | return $this->certificateKey ? $this->certificateKey->absoluteOrRelativeToCodeceptionDir() : null; |
||
154 | } |
||
155 | |||
156 | public function getCertificatePassphrase(): ?string |
||
159 | } |
||
160 | |||
161 | public function getLogsPath(): string |
||
162 | { |
||
163 | return $this->logsPath->absoluteOrRelativeToCodeceptionDir(); |
||
164 | } |
||
165 | |||
166 | public function getServerFactory(): ?string |
||
167 | { |
||
168 | return $this->serverFactory; |
||
169 | } |
||
170 | |||
171 | public function getDelay(): int |
||
174 | } |
||
175 | |||
176 | public function getExtraInstances(): array |
||
177 | { |
||
178 | return $this->extraInstances; |
||
179 | } |
||
180 | |||
181 | public function isSecure(): bool |
||
182 | { |
||
183 | return $this->getCertificatePath() !== null |
||
184 | && $this->getCertificateKeyPath() !== null; |
||
185 | } |
||
186 | |||
187 | public function isWaitUntilReady(): bool |
||
188 | { |
||
189 | return $this->waitUntilReady; |
||
190 | } |
||
191 | |||
192 | public function getWaitUntilReadyTimeout(): int |
||
193 | { |
||
194 | return $this->waitUntilReadyTimeout; |
||
195 | } |
||
196 | |||
197 | /** @throws ConfigurationException */ |
||
198 | public static function getDefaultLogsPath(): string |
||
199 | { |
||
200 | return Configuration::logDir(); |
||
201 | } |
||
202 | |||
203 | private function initInterfaceAndPort(array $config): void |
||
204 | { |
||
205 | if (isset($config['listen'])) { |
||
206 | $parts = explode(':', $config['listen']); |
||
207 | $this->interface = $parts[0]; |
||
208 | $this->port = (int) (isset($parts[1]) ? $parts[1] : self::DEFAULT_PORT); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | private function initExpectationsPath(array $config): void |
||
213 | { |
||
214 | $this->expectationsPath = isset($config['expectations_path']) ? new Path($config['expectations_path']) : null; |
||
215 | } |
||
216 | |||
217 | private function initServerFactory(array $config): void |
||
227 | } |
||
228 | |||
229 | private function initDelay(array $config): void |
||
230 | { |
||
231 | if (isset($config['startDelay'])) { |
||
232 | call_user_func($this->output, 'PHIREMOCK/DEPRECATION: startDelay option is deprecated and will be removed. Please use start_delay'); |
||
233 | $this->delay = (int) $config['startDelay']; |
||
234 | return; |
||
235 | } |
||
236 | |||
237 | if (is_int($config['start_delay']) && $config['start_delay'] >= 0) { |
||
238 | $this->delay = (int) $config['start_delay']; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** @throws ConfigurationException */ |
||
243 | private function initExtraInstances(array $config): void |
||
244 | { |
||
245 | $this->extraInstances = self::DEFAULT_EXTRA_INSTANCES; |
||
246 | if (isset($config['extra_instances']) && is_array($config['extra_instances'])) { |
||
247 | foreach ($config['extra_instances'] as $extraInstance) { |
||
248 | $instanceConfig = $extraInstance + self::DEFAULT_CONFIG + ['logs_path' => Config::getDefaultLogsPath()]; |
||
249 | unset($instanceConfig['extra_instances']); |
||
250 | $this->extraInstances[] = new self($instanceConfig, $this->output); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | private function initCertificateKeyPath($config): void |
||
256 | { |
||
257 | $this->certificateKey = $config['certificate_key'] ? new Path($config['certificate_key']) : null; |
||
258 | } |
||
259 | |||
260 | private function initCertificatePath($config): void |
||
263 | } |
||
264 | } |
||
265 |