Complex classes like Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Page extends Item |
||
| 23 | { |
||
| 24 | const SLUGIFY_PATTERN = '/(^\/|[^_a-z0-9\/]|-)+/'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var SplFileInfo |
||
| 28 | */ |
||
| 29 | protected $file; |
||
| 30 | /** |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $virtual; |
||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $type; |
||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $folder; |
||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $slug; |
||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $path; |
||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $section; |
||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $frontmatter; |
||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $body; |
||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $html; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Constructor. |
||
| 69 | * |
||
| 70 | * @param string $id |
||
| 71 | */ |
||
| 72 | public function __construct(string $id) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Create ID from file. |
||
| 143 | * |
||
| 144 | * @param SplFileInfo $file |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function createId(SplFileInfo $file): string |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set file. |
||
| 155 | * |
||
| 156 | * @param SplFileInfo $file |
||
| 157 | * |
||
| 158 | * @return self |
||
| 159 | */ |
||
| 160 | public function setFile(SplFileInfo $file): self |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Parse file content. |
||
| 167 | * |
||
| 168 | * @return self |
||
| 169 | */ |
||
| 170 | public function parse(): self |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get frontmatter. |
||
| 182 | * |
||
| 183 | * @return string|null |
||
| 184 | */ |
||
| 185 | public function getFrontmatter(): ?string |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get body as raw. |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getBody(): ?string |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Turn a path (string) into a slug (URL). |
||
| 202 | * |
||
| 203 | * @param string $path |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public static function slugify(string $path): string |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Is current page is virtual? |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function isVirtual(): bool |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Set page type. |
||
| 226 | * |
||
| 227 | * @param string $type |
||
| 228 | * |
||
| 229 | * @return self |
||
| 230 | */ |
||
| 231 | public function setType(string $type): self |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get page type. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getType(): string |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Set path without slug. |
||
| 250 | * |
||
| 251 | * @param string $folder |
||
| 252 | * |
||
| 253 | * @return self |
||
| 254 | */ |
||
| 255 | public function setFolder(string $folder): self |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get path without slug. |
||
| 264 | * |
||
| 265 | * @return string|null |
||
| 266 | */ |
||
| 267 | public function getFolder(): ?string |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set slug. |
||
| 274 | * |
||
| 275 | * @param string $slug |
||
| 276 | * |
||
| 277 | * @return self |
||
| 278 | */ |
||
| 279 | public function setSlug(string $slug): self |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get slug. |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getSlug(): string |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set path. |
||
| 303 | * |
||
| 304 | * @param string $path |
||
| 305 | * |
||
| 306 | * @return self |
||
| 307 | */ |
||
| 308 | public function setPath(string $path): self |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get path. |
||
| 336 | * |
||
| 337 | * @return string|null |
||
| 338 | */ |
||
| 339 | public function getPath(): ?string |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @see getPath() |
||
| 353 | * |
||
| 354 | * @return string|null |
||
| 355 | */ |
||
| 356 | public function getPathname(): ?string |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set section. |
||
| 363 | * |
||
| 364 | * @param string $section |
||
| 365 | * |
||
| 366 | * @return self |
||
| 367 | */ |
||
| 368 | public function setSection(string $section): self |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get section. |
||
| 377 | * |
||
| 378 | * @return string|null |
||
| 379 | */ |
||
| 380 | public function getSection(): ?string |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set body as HTML. |
||
| 391 | * |
||
| 392 | * @param string $html |
||
| 393 | * |
||
| 394 | * @return self |
||
| 395 | */ |
||
| 396 | public function setBodyHtml(string $html): self |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get body as HTML. |
||
| 405 | * |
||
| 406 | * @return string|null |
||
| 407 | */ |
||
| 408 | public function getBodyHtml(): ?string |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @see getBodyHtml() |
||
| 415 | * |
||
| 416 | * @return string|null |
||
| 417 | */ |
||
| 418 | public function getContent(): ?string |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Return output file. |
||
| 425 | * |
||
| 426 | * Use cases: |
||
| 427 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
| 428 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
| 429 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
| 430 | * - path only (ie: _redirects) |
||
| 431 | * |
||
| 432 | * @param string $format |
||
| 433 | * @param Config $config |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | public function getOutputFile(string $format, Config $config = null): string |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Return URL. |
||
| 475 | * |
||
| 476 | * @param string $format |
||
| 477 | * @param Config $config |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getUrl(string $format = 'html', Config $config = null): string |
||
| 492 | |||
| 493 | /* |
||
| 494 | * Helper to set and get variables. |
||
| 495 | */ |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Set an array as variables. |
||
| 499 | * |
||
| 500 | * @param array $variables |
||
| 501 | * |
||
| 502 | * @throws \Exception |
||
| 503 | * |
||
| 504 | * @return $this |
||
| 505 | */ |
||
| 506 | public function setVariables($variables) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get all variables. |
||
| 520 | * |
||
| 521 | * @return array |
||
| 522 | */ |
||
| 523 | public function getVariables(): array |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Set a variable. |
||
| 530 | * |
||
| 531 | * @param $name |
||
| 532 | * @param $value |
||
| 533 | * |
||
| 534 | * @throws \Exception |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | public function setVariable($name, $value) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Is variable exist? |
||
| 597 | * |
||
| 598 | * @param string $name |
||
| 599 | * |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public function hasVariable(string $name): bool |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Get a variable. |
||
| 609 | * |
||
| 610 | * @param string $name |
||
| 611 | * |
||
| 612 | * @return mixed|null |
||
| 613 | */ |
||
| 614 | public function getVariable(string $name) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Unset a variable. |
||
| 623 | * |
||
| 624 | * @param string $name |
||
| 625 | * |
||
| 626 | * @return $this |
||
| 627 | */ |
||
| 628 | public function unVariable(string $name): self |
||
| 636 | } |
||
| 637 |