Total Complexity | 40 |
Total Lines | 294 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
21 | class ConfigurationOption implements ConfigurationOptionInterface |
||
22 | { |
||
23 | protected bool $itemDetailedDate = false; |
||
24 | |||
25 | protected bool $autoTmpFallback = false; |
||
26 | |||
27 | protected int $defaultTtl = 900; |
||
28 | |||
29 | /** |
||
30 | * @var string|callable |
||
31 | */ |
||
32 | protected mixed $defaultKeyHashFunction = 'md5'; |
||
33 | |||
34 | /** |
||
35 | * @var string|callable |
||
36 | */ |
||
37 | protected mixed $defaultFileNameHashFunction = 'md5'; |
||
38 | |||
39 | protected string $path = ''; |
||
40 | |||
41 | protected bool $preventCacheSlams = false; |
||
42 | |||
43 | protected int $cacheSlamsTimeout = 15; |
||
44 | |||
45 | protected bool $useStaticItemCaching = true; |
||
46 | |||
47 | protected ?object $superGlobalAccessor = null; |
||
48 | |||
49 | /** |
||
50 | * @throws PhpfastcacheInvalidConfigurationException |
||
51 | */ |
||
52 | public function __construct(array $parameters = []) |
||
53 | { |
||
54 | foreach ($parameters as $configKey => $configVal) { |
||
55 | try { |
||
56 | if (\property_exists($this, $configKey)) { |
||
57 | $this->{'set' . \ucfirst($configKey)}($configVal); |
||
58 | } else { |
||
59 | throw new PhpfastcacheInvalidConfigurationException( |
||
60 | sprintf( |
||
61 | 'Invalid option for the config %s: %s', |
||
62 | $this::class, |
||
63 | $configKey |
||
64 | ) |
||
65 | ); |
||
66 | } |
||
67 | } catch (\TypeError $e) { |
||
68 | throw new PhpfastcacheInvalidConfigurationException( |
||
69 | \sprintf( |
||
70 | 'TypeError exception thrown while trying to set your configuration: %s', |
||
71 | $e->getMessage() |
||
72 | ) |
||
73 | ); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | public function toArray(): array |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param string $optionName |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function isValidOption(string $optionName): bool |
||
88 | { |
||
89 | return property_exists($this, $optionName); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function isItemDetailedDate(): bool |
||
96 | { |
||
97 | return $this->itemDetailedDate; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param bool $itemDetailedDate |
||
102 | * @return ConfigurationOption |
||
103 | */ |
||
104 | public function setItemDetailedDate(bool $itemDetailedDate): static |
||
105 | { |
||
106 | $this->itemDetailedDate = $itemDetailedDate; |
||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function isAutoTmpFallback(): bool |
||
114 | { |
||
115 | return $this->autoTmpFallback; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param bool $autoTmpFallback |
||
120 | * @return ConfigurationOption |
||
121 | */ |
||
122 | public function setAutoTmpFallback(bool $autoTmpFallback): static |
||
123 | { |
||
124 | $this->autoTmpFallback = $autoTmpFallback; |
||
125 | return $this; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return int |
||
130 | */ |
||
131 | public function getDefaultTtl(): int |
||
132 | { |
||
133 | return $this->defaultTtl; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param int $defaultTtl |
||
138 | * @return ConfigurationOption |
||
139 | */ |
||
140 | public function setDefaultTtl(int $defaultTtl): static |
||
141 | { |
||
142 | $this->defaultTtl = $defaultTtl; |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return callable|string |
||
148 | */ |
||
149 | public function getDefaultKeyHashFunction(): callable|string |
||
150 | { |
||
151 | return $this->defaultKeyHashFunction; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param callable|string $defaultKeyHashFunction |
||
156 | * @return ConfigurationOption |
||
157 | * @throws PhpfastcacheInvalidConfigurationException |
||
158 | */ |
||
159 | public function setDefaultKeyHashFunction(callable|string $defaultKeyHashFunction): static |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return callable|string |
||
170 | */ |
||
171 | public function getDefaultFileNameHashFunction(): callable|string |
||
172 | { |
||
173 | return $this->defaultFileNameHashFunction; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param callable|string $defaultFileNameHashFunction |
||
178 | * @return ConfigurationOption |
||
179 | * @throws PhpfastcacheInvalidConfigurationException |
||
180 | */ |
||
181 | public function setDefaultFileNameHashFunction(callable|string $defaultFileNameHashFunction): static |
||
182 | { |
||
183 | if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) { |
||
184 | throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string'); |
||
185 | } |
||
186 | $this->defaultFileNameHashFunction = $defaultFileNameHashFunction; |
||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getPath(): string |
||
194 | { |
||
195 | return $this->path; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param string $path |
||
200 | * @return ConfigurationOption |
||
201 | */ |
||
202 | public function setPath(string $path): static |
||
203 | { |
||
204 | $this->path = $path; |
||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @return bool |
||
210 | */ |
||
211 | public function isPreventCacheSlams(): bool |
||
212 | { |
||
213 | return $this->preventCacheSlams; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param bool $preventCacheSlams |
||
218 | * @return ConfigurationOption |
||
219 | */ |
||
220 | public function setPreventCacheSlams(bool $preventCacheSlams): static |
||
221 | { |
||
222 | $this->preventCacheSlams = $preventCacheSlams; |
||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return int |
||
228 | */ |
||
229 | public function getCacheSlamsTimeout(): int |
||
230 | { |
||
231 | return $this->cacheSlamsTimeout; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param int $cacheSlamsTimeout |
||
236 | * @return ConfigurationOption |
||
237 | */ |
||
238 | public function setCacheSlamsTimeout(int $cacheSlamsTimeout): static |
||
239 | { |
||
240 | $this->cacheSlamsTimeout = $cacheSlamsTimeout; |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function isUseStaticItemCaching(): bool |
||
248 | { |
||
249 | return $this->useStaticItemCaching; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param bool $useStaticItemCaching |
||
254 | * @return ConfigurationOption |
||
255 | */ |
||
256 | public function setUseStaticItemCaching(bool $useStaticItemCaching): static |
||
257 | { |
||
258 | $this->useStaticItemCaching = $useStaticItemCaching; |
||
259 | return $this; |
||
260 | } |
||
261 | |||
262 | |||
263 | /** |
||
264 | * @return object |
||
265 | * @throws PhpfastcacheInvalidArgumentException |
||
266 | */ |
||
267 | public function getSuperGlobalAccessor(): object |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param ?object $superGlobalAccessor |
||
277 | * @return static |
||
278 | * @throws PhpfastcacheInvalidArgumentException |
||
279 | */ |
||
280 | public function setSuperGlobalAccessor(?object $superGlobalAccessor): static |
||
281 | { |
||
282 | /** |
||
283 | * Symfony's implementation for users that want a good control of their code: |
||
284 | * |
||
285 | * $config['superGlobalAccessor'] = \Closure::fromCallable(static function(string $superGlobalName, string $keyName) use ($request) { |
||
286 | * return match ($superGlobalName) { |
||
287 | * 'SERVER' => $request->server->get($keyName), |
||
288 | * 'REQUEST' => $request->request->get($keyName), |
||
289 | * }; |
||
290 | * }); |
||
291 | */ |
||
292 | |||
293 | if ($superGlobalAccessor === null) { |
||
294 | $this->superGlobalAccessor = $this->getDefaultSuperGlobalAccessor(); |
||
295 | } elseif (!\is_callable($superGlobalAccessor)) { |
||
296 | throw new PhpfastcacheInvalidArgumentException('The "superGlobalAccessor" callback must be callable using "__invoke" or \Closure implementation'); |
||
297 | } else { |
||
298 | $this->superGlobalAccessor = $superGlobalAccessor; |
||
299 | } |
||
300 | |||
301 | return $this; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return \Closure |
||
306 | * @SuppressWarnings(PHPMD.Superglobals) |
||
307 | */ |
||
308 | protected function getDefaultSuperGlobalAccessor(): \Closure |
||
315 | }; |
||
316 | }); |
||
317 | } |
||
318 | } |
||
319 |