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