Total Complexity | 52 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ConfigurationOption 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 ConfigurationOption, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class ConfigurationOption extends ArrayObject |
||
16 | { |
||
17 | /** |
||
18 | * @var bool |
||
19 | */ |
||
20 | protected $itemDetailedDate = false; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $autoTmpFallback = false; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | * @deprecated Do not use this option anymore |
||
30 | */ |
||
31 | protected $ignoreSymfonyNotice = false; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $defaultTtl = 900; |
||
37 | |||
38 | /** |
||
39 | * @var string|Callable |
||
40 | */ |
||
41 | protected $defaultKeyHashFunction = 'md5'; |
||
42 | |||
43 | /** |
||
44 | * @var string|Callable |
||
45 | */ |
||
46 | protected $defaultFileNameHashFunction = 'md5'; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $defaultChmod = 0777; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $path = ''; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $fallback = ''; |
||
62 | |||
63 | /** |
||
64 | * @var \Phpfastcache\Config\ConfigurationOption |
||
65 | */ |
||
66 | protected $fallbackConfig; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $limitedMemoryByObject = 4096; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $compressData = false; |
||
77 | |||
78 | /** |
||
79 | * @var bool |
||
80 | */ |
||
81 | protected $preventCacheSlams = false; |
||
82 | |||
83 | /** |
||
84 | * @var int |
||
85 | */ |
||
86 | protected $cacheSlamsTimeout = 15; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $cacheFileExtension = 'txt'; |
||
92 | |||
93 | /** |
||
94 | * @param $args |
||
95 | * ArrayObject constructor. |
||
96 | */ |
||
97 | public function __construct(...$args) |
||
98 | { |
||
99 | parent::__construct(...$args); |
||
100 | $array =& $this->getArray(); |
||
101 | |||
102 | /** |
||
103 | * Detect unwanted keys and throw an exception. |
||
104 | * No more kidding now, it's 21th century. |
||
105 | */ |
||
106 | if(array_diff_key($array, get_object_vars($this))){ |
||
107 | throw new PhpfastcacheInvalidConfigurationException(\sprintf( |
||
108 | 'Invalid option(s) for the config %s: %s', |
||
109 | static::class, |
||
110 | implode(', ', array_keys(array_diff_key($array, get_object_vars($this)))) |
||
111 | )); |
||
112 | } |
||
113 | |||
114 | foreach (get_object_vars($this) as $property => $value) { |
||
115 | |||
116 | if(array_key_exists($property, $array)){ |
||
117 | $this->$property = &$array[ $property ]; |
||
118 | }else{ |
||
119 | $array[ $property ] = &$this->$property; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | foreach (get_class_methods($this) as $method) { |
||
124 | if(strpos($method, 'set') === 0){ |
||
125 | $value = null; |
||
|
|||
126 | try{ |
||
127 | /** |
||
128 | * We use property instead of getter |
||
129 | * because of is/get conditions and |
||
130 | * to allow us to retrieve the value |
||
131 | * in catch statement bloc |
||
132 | */ |
||
133 | $value = $this->{lcfirst(substr($method, 3))}; |
||
134 | $this->{$method}($value); |
||
135 | }catch(\TypeError $e){ |
||
136 | $typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value); |
||
137 | $reflectionMethod = new \ReflectionMethod($this, $method); |
||
138 | $parameter = $reflectionMethod->getParameters()[0] ?? null; |
||
139 | $typeHintExpected = ($parameter instanceof \ReflectionParameter ? ($parameter->getType() === 'object' ? $parameter->getClass() : $parameter->getType()) : 'Unknown type'); |
||
140 | |||
141 | throw new PhpfastcacheInvalidConfigurationException(\sprintf( |
||
142 | 'Invalid type hint found for "%s", expected "%s" got "%s"', |
||
143 | lcfirst(substr($method, 3)), |
||
144 | $typeHintExpected, |
||
145 | $typeHintGot |
||
146 | )); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param string $optionName |
||
154 | * @return mixed|null |
||
155 | * @deprecated Use ->getOptionName() instead |
||
156 | */ |
||
157 | public function getOption(string $optionName) |
||
158 | { |
||
159 | \trigger_error(\sprintf('Method "%s" is deprecated, use "getOptionName()" instead', __METHOD__), E_USER_DEPRECATED); |
||
160 | return $this->$optionName ?? null; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $optionName |
||
165 | * @return mixed|null |
||
166 | */ |
||
167 | public function isValidOption(string $optionName) |
||
168 | { |
||
169 | return property_exists($this, $optionName); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function isItemDetailedDate(): bool |
||
176 | { |
||
177 | return $this->itemDetailedDate; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param bool $itemDetailedDate |
||
182 | * @return ConfigurationOption |
||
183 | */ |
||
184 | public function setItemDetailedDate(bool $itemDetailedDate): self |
||
185 | { |
||
186 | $this->itemDetailedDate = $itemDetailedDate; |
||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isAutoTmpFallback(): bool |
||
194 | { |
||
195 | return $this->autoTmpFallback; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param bool $autoTmpFallback |
||
200 | * @return ConfigurationOption |
||
201 | */ |
||
202 | public function setAutoTmpFallback(bool $autoTmpFallback): self |
||
203 | { |
||
204 | $this->autoTmpFallback = $autoTmpFallback; |
||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @return bool |
||
210 | * @deprecated As of V7 |
||
211 | */ |
||
212 | public function isIgnoreSymfonyNotice(): bool |
||
213 | { |
||
214 | return $this->ignoreSymfonyNotice; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param bool $ignoreSymfonyNotice |
||
219 | * @return ConfigurationOption |
||
220 | * @deprecated As of V7 |
||
221 | */ |
||
222 | public function setIgnoreSymfonyNotice(bool $ignoreSymfonyNotice): self |
||
223 | { |
||
224 | if($ignoreSymfonyNotice){ |
||
225 | \trigger_error('Configuration option "ignoreSymfonyNotice" is deprecated as of the V7', E_USER_DEPRECATED); |
||
226 | } |
||
227 | $this->ignoreSymfonyNotice = $ignoreSymfonyNotice; |
||
228 | return $this; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @return int |
||
233 | */ |
||
234 | public function getDefaultTtl(): int |
||
235 | { |
||
236 | return $this->defaultTtl; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param int $defaultTtl |
||
241 | * @return ConfigurationOption |
||
242 | */ |
||
243 | public function setDefaultTtl(int $defaultTtl): self |
||
244 | { |
||
245 | $this->defaultTtl = $defaultTtl; |
||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @return Callable|string |
||
251 | */ |
||
252 | public function getDefaultKeyHashFunction() |
||
253 | { |
||
254 | return $this->defaultKeyHashFunction; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param Callable|string $defaultKeyHashFunction |
||
259 | * @return ConfigurationOption |
||
260 | * @throws PhpfastcacheInvalidConfigurationException |
||
261 | */ |
||
262 | public function setDefaultKeyHashFunction($defaultKeyHashFunction) |
||
263 | { |
||
264 | if (!\function_exists($defaultKeyHashFunction) || !\is_callable($defaultKeyHashFunction)) { |
||
265 | throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string'); |
||
266 | } |
||
267 | $this->defaultKeyHashFunction = $defaultKeyHashFunction; |
||
268 | return $this; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @return Callable|string |
||
273 | */ |
||
274 | public function getDefaultFileNameHashFunction() |
||
275 | { |
||
276 | return $this->defaultFileNameHashFunction; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param Callable|string $defaultKeyHashFunction |
||
281 | * @return ConfigurationOption |
||
282 | * @throws PhpfastcacheInvalidConfigurationException |
||
283 | */ |
||
284 | public function setDefaultFileNameHashFunction($defaultFileNameHashFunction) |
||
285 | { |
||
286 | if (!\function_exists($defaultFileNameHashFunction) || !\is_callable($defaultFileNameHashFunction)) { |
||
287 | throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string'); |
||
288 | } |
||
289 | $this->defaultFileNameHashFunction = $defaultFileNameHashFunction; |
||
290 | return $this; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @return int |
||
295 | */ |
||
296 | public function getDefaultChmod(): int |
||
297 | { |
||
298 | return $this->defaultChmod; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param int $defaultChmod |
||
303 | * @return ConfigurationOption |
||
304 | */ |
||
305 | public function setDefaultChmod(int $defaultChmod): self |
||
306 | { |
||
307 | $this->defaultChmod = $defaultChmod; |
||
308 | return $this; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getPath(): string |
||
315 | { |
||
316 | return $this->path; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param string $path |
||
321 | * @return ConfigurationOption |
||
322 | */ |
||
323 | public function setPath(string $path): self |
||
324 | { |
||
325 | $this->path = $path; |
||
326 | return $this; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @return bool|string |
||
331 | */ |
||
332 | public function getFallback() |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param string $fallback |
||
339 | * @return ConfigurationOption |
||
340 | */ |
||
341 | public function setFallback(string $fallback): self |
||
342 | { |
||
343 | $this->fallback = $fallback; |
||
344 | return $this; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @return \Phpfastcache\Config\ConfigurationOption|null |
||
349 | */ |
||
350 | public function getFallbackConfig() |
||
351 | { |
||
352 | return $this->fallbackConfig; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param \Phpfastcache\Config\ConfigurationOption|null $fallbackConfig |
||
357 | * @return ConfigurationOption |
||
358 | */ |
||
359 | public function setFallbackConfig($fallbackConfig): self |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @return int |
||
374 | */ |
||
375 | public function getLimitedMemoryByObject(): int |
||
376 | { |
||
377 | return $this->limitedMemoryByObject; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @param int $limitedMemoryByObject |
||
382 | * @return ConfigurationOption |
||
383 | */ |
||
384 | public function setLimitedMemoryByObject(int $limitedMemoryByObject): self |
||
385 | { |
||
386 | $this->limitedMemoryByObject = $limitedMemoryByObject; |
||
387 | return $this; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @return bool |
||
392 | */ |
||
393 | public function isCompressData(): bool |
||
394 | { |
||
395 | return $this->compressData; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * @param bool $compressData |
||
400 | * @return ConfigurationOption |
||
401 | */ |
||
402 | public function setCompressData(bool $compressData): self |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function isPreventCacheSlams(): bool |
||
412 | { |
||
413 | return $this->preventCacheSlams; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @param bool $preventCacheSlams |
||
418 | * @return ConfigurationOption |
||
419 | */ |
||
420 | public function setPreventCacheSlams(bool $preventCacheSlams): self |
||
421 | { |
||
422 | $this->preventCacheSlams = $preventCacheSlams; |
||
423 | return $this; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * @return int |
||
428 | */ |
||
429 | public function getCacheSlamsTimeout(): int |
||
430 | { |
||
431 | return $this->cacheSlamsTimeout; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param int $cacheSlamsTimeout |
||
436 | * @return ConfigurationOption |
||
437 | */ |
||
438 | public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * @return string |
||
446 | */ |
||
447 | public function getCacheFileExtension(): string |
||
448 | { |
||
449 | return $this->cacheFileExtension; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @param string $cacheFileExtension |
||
454 | * @return ConfigurationOption |
||
455 | * @throws PhpfastcacheInvalidConfigurationException |
||
456 | */ |
||
457 | public function setCacheFileExtension(string $cacheFileExtension): self |
||
481 | } |
||
482 | } |