Complex classes like CacheControlHeader 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CacheControlHeader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class CacheControlHeader implements HeaderInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $directives = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor. |
||
| 28 | * |
||
| 29 | * @param array $directives |
||
| 30 | */ |
||
| 31 | public function __construct(array $directives = array()) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Add cache directive, some directive don't have any value. |
||
| 45 | * Example: no-store, must-revalidate, etc. |
||
| 46 | * |
||
| 47 | * @param string $key |
||
| 48 | * @param string|null $value |
||
| 49 | * |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | public function setDirective($key, $value = null) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set cache as public. |
||
| 70 | * |
||
| 71 | * @return $this |
||
| 72 | */ |
||
| 73 | public function setPublic() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Check if cache private. |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public function isPrivate() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set auto privilege. |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function setAutoPrivilege() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Check if cache has privileges directive. |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public function isHasPrivileges() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Check if directive exist. |
||
| 122 | * |
||
| 123 | * @param string $key |
||
| 124 | * |
||
| 125 | * @return bool Return true if directive exist, false otherwise |
||
| 126 | */ |
||
| 127 | public function hasDirective($key) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Remove directive by key. |
||
| 134 | * |
||
| 135 | * @param string $key |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function removeDirective($key) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set directive. |
||
| 148 | * |
||
| 149 | * @param string $key |
||
| 150 | * @param string|null $value |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | * |
||
| 154 | * @throws InvalidHeaderValueException |
||
| 155 | */ |
||
| 156 | private function set($key, $value = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set cache as private. |
||
| 170 | * |
||
| 171 | * @param string|null $value |
||
| 172 | * |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function setPrivate($value = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Check if cache is public. |
||
| 186 | * |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function isPublic() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set cache max-age. |
||
| 196 | * |
||
| 197 | * @param int $deltaSeconds |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setMaxAge($deltaSeconds) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set shared max age. |
||
| 208 | * |
||
| 209 | * @param int $deltaSeconds |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setSharedMaxAge($deltaSeconds) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Factory create CacheControlHeader from string representation. |
||
| 220 | * |
||
| 221 | * @param string $headerLine |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public static function fromString($headerLine) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Add no-cache directive. |
||
| 248 | * |
||
| 249 | * @param string|null $value |
||
| 250 | * |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | public function setNoCache($value = null) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Check if no cached. |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | public function isNoCache() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Add no-store directive. |
||
| 270 | * |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | public function setNoStore() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Check if no stored. |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function isNoStored() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set proxy-revalidate. |
||
| 290 | * |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function setProxyReValidate() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Check is proxy-revalidate. |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function isProxyReValidate() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Cache should re-validate. |
||
| 310 | * |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function setMustReValidate() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Check if cache must re-validate. |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function isMustReValidate() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Set no-transform. |
||
| 330 | * |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function setNoTransform() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Check if cache is no-transform. |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function isNoTransform() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get max-age. |
||
| 350 | * |
||
| 351 | * @return int|null |
||
| 352 | */ |
||
| 353 | public function getMaxAge() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get directive by key. |
||
| 360 | * |
||
| 361 | * @param string $key |
||
| 362 | * |
||
| 363 | * @return string|null |
||
| 364 | */ |
||
| 365 | public function getDirective($key) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get shared max age. |
||
| 372 | * |
||
| 373 | * @return int|null |
||
| 374 | */ |
||
| 375 | public function getSharedMaxAge() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get all directives. |
||
| 382 | * |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | public function allDirectives() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Cast header to string. |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public function __toString() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get header field name. |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function getFieldName() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get header field value. |
||
| 412 | * |
||
| 413 | * @return mixed |
||
| 414 | */ |
||
| 415 | public function getFieldValue() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set delta second to specified key. |
||
| 431 | * |
||
| 432 | * @param string $key |
||
| 433 | * @param int $deltaSeconds |
||
| 434 | * |
||
| 435 | * @return $this |
||
| 436 | */ |
||
| 437 | private function setDeltaSeconds($key, $deltaSeconds) |
||
| 443 | } |
||
| 444 |