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\/]|-)+/'; // should be '/^\/|[^_a-z0-9\/]+/' |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $virtual; |
||
| 30 | /** |
||
| 31 | * @var SplFileInfo |
||
| 32 | */ |
||
| 33 | protected $file; |
||
| 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 | * @var Slugify |
||
| 68 | */ |
||
| 69 | private static $slugifier; |
||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $language; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Constructor. |
||
| 77 | * |
||
| 78 | * @param string $id |
||
| 79 | */ |
||
| 80 | public function __construct(string $id) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Turn a path (string) into a slug (URI). |
||
| 99 | * |
||
| 100 | * @param string $path |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public static function slugify(string $path): string |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Create ID from file. |
||
| 115 | * |
||
| 116 | * @param SplFileInfo $file |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public static function createId(SplFileInfo $file): string |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set file. |
||
| 135 | * |
||
| 136 | * @param SplFileInfo $file |
||
| 137 | * |
||
| 138 | * @return self |
||
| 139 | */ |
||
| 140 | public function setFile(SplFileInfo $file): self |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Parse file content. |
||
| 193 | * |
||
| 194 | * @return self |
||
| 195 | */ |
||
| 196 | public function parse(): self |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get frontmatter. |
||
| 208 | * |
||
| 209 | * @return string|null |
||
| 210 | */ |
||
| 211 | public function getFrontmatter(): ?string |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get body as raw. |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getBody(): ?string |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Set virtual status. |
||
| 228 | * |
||
| 229 | * @param bool $virtual |
||
| 230 | * |
||
| 231 | * @return self |
||
| 232 | */ |
||
| 233 | public function setVirtual(bool $virtual): self |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Is current page is virtual? |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function isVirtual(): bool |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set page type. |
||
| 252 | * |
||
| 253 | * @param string $type |
||
| 254 | * |
||
| 255 | * @return self |
||
| 256 | */ |
||
| 257 | public function setType(string $type): self |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get page type. |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getType(): string |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set path without slug. |
||
| 276 | * |
||
| 277 | * @param string $folder |
||
| 278 | * |
||
| 279 | * @return self |
||
| 280 | */ |
||
| 281 | public function setFolder(string $folder): self |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get path without slug. |
||
| 290 | * |
||
| 291 | * @return string|null |
||
| 292 | */ |
||
| 293 | public function getFolder(): ?string |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Set slug. |
||
| 300 | * |
||
| 301 | * @param string $slug |
||
| 302 | * |
||
| 303 | * @return self |
||
| 304 | */ |
||
| 305 | public function setSlug(string $slug): self |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get slug. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getSlug(): string |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set path. |
||
| 331 | * |
||
| 332 | * @param string $path |
||
| 333 | * |
||
| 334 | * @return self |
||
| 335 | */ |
||
| 336 | public function setPath(string $path): self |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get path. |
||
| 369 | * |
||
| 370 | * @return string|null |
||
| 371 | */ |
||
| 372 | public function getPath(): ?string |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @see getPath() |
||
| 379 | * |
||
| 380 | * @return string|null |
||
| 381 | */ |
||
| 382 | public function getPathname(): ?string |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set section. |
||
| 389 | * |
||
| 390 | * @param string $section |
||
| 391 | * |
||
| 392 | * @return self |
||
| 393 | */ |
||
| 394 | public function setSection(string $section): self |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get section. |
||
| 403 | * |
||
| 404 | * @return string|null |
||
| 405 | */ |
||
| 406 | public function getSection(): ?string |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set body as HTML. |
||
| 413 | * |
||
| 414 | * @param string $html |
||
| 415 | * |
||
| 416 | * @return self |
||
| 417 | */ |
||
| 418 | public function setBodyHtml(string $html): self |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get body as HTML. |
||
| 427 | * |
||
| 428 | * @return string|null |
||
| 429 | */ |
||
| 430 | public function getBodyHtml(): ?string |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @see getBodyHtml() |
||
| 437 | * |
||
| 438 | * @return string|null |
||
| 439 | */ |
||
| 440 | public function getContent(): ?string |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Return output file. |
||
| 447 | * |
||
| 448 | * Use cases: |
||
| 449 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
| 450 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
| 451 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
| 452 | * - path only (ie: _redirects) |
||
| 453 | * |
||
| 454 | * @param string $format |
||
| 455 | * @param Config $config |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function getOutputFile(string $format, Config $config = null): string |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Return URL. |
||
| 502 | * |
||
| 503 | * @param string $format |
||
| 504 | * @param Config $config |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function getUrl(string $format = 'html', Config $config = null): string |
||
| 519 | |||
| 520 | /* |
||
| 521 | * Helper to set and get variables. |
||
| 522 | */ |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Set an array as variables. |
||
| 526 | * |
||
| 527 | * @param array $variables |
||
| 528 | * |
||
| 529 | * @throws \Exception |
||
| 530 | * |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | public function setVariables($variables) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get all variables. |
||
| 551 | * |
||
| 552 | * @return array |
||
| 553 | */ |
||
| 554 | public function getVariables(): array |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Set a variable. |
||
| 561 | * |
||
| 562 | * @param $name |
||
| 563 | * @param $value |
||
| 564 | * |
||
| 565 | * @throws \Exception |
||
| 566 | * |
||
| 567 | * @return $this |
||
| 568 | */ |
||
| 569 | public function setVariable($name, $value) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Is variable exist? |
||
| 618 | * |
||
| 619 | * @param string $name |
||
| 620 | * |
||
| 621 | * @return bool |
||
| 622 | */ |
||
| 623 | public function hasVariable(string $name): bool |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Get a variable. |
||
| 630 | * |
||
| 631 | * @param string $name |
||
| 632 | * |
||
| 633 | * @return mixed|null |
||
| 634 | */ |
||
| 635 | public function getVariable(string $name) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Unset a variable. |
||
| 644 | * |
||
| 645 | * @param string $name |
||
| 646 | * |
||
| 647 | * @return $this |
||
| 648 | */ |
||
| 649 | public function unVariable(string $name): self |
||
| 657 | } |
||
| 658 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..