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