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