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 | /** |
||
| 72 | * Constructor. |
||
| 73 | * |
||
| 74 | * @param string $id |
||
| 75 | */ |
||
| 76 | public function __construct(string $id) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Turn a path (string) into a slug (URI). |
||
| 95 | * |
||
| 96 | * @param string $path |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public static function slugify(string $path): string |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Create ID from file. |
||
| 111 | * |
||
| 112 | * @param SplFileInfo $file |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public static function createId(SplFileInfo $file): string |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set file. |
||
| 131 | * |
||
| 132 | * @param SplFileInfo $file |
||
| 133 | * |
||
| 134 | * @return self |
||
| 135 | */ |
||
| 136 | public function setFile(SplFileInfo $file): self |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Parse file content. |
||
| 186 | * |
||
| 187 | * @return self |
||
| 188 | */ |
||
| 189 | public function parse(): self |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get frontmatter. |
||
| 201 | * |
||
| 202 | * @return string|null |
||
| 203 | */ |
||
| 204 | public function getFrontmatter(): ?string |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get body as raw. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getBody(): ?string |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set virtual status. |
||
| 221 | * |
||
| 222 | * @param bool $virtual |
||
| 223 | * |
||
| 224 | * @return self |
||
| 225 | */ |
||
| 226 | public function setVirtual(bool $virtual): self |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Is current page is virtual? |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function isVirtual(): bool |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Set page type. |
||
| 245 | * |
||
| 246 | * @param string $type |
||
| 247 | * |
||
| 248 | * @return self |
||
| 249 | */ |
||
| 250 | public function setType(string $type): self |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get page type. |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getType(): string |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Set path without slug. |
||
| 269 | * |
||
| 270 | * @param string $folder |
||
| 271 | * |
||
| 272 | * @return self |
||
| 273 | */ |
||
| 274 | public function setFolder(string $folder): self |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get path without slug. |
||
| 283 | * |
||
| 284 | * @return string|null |
||
| 285 | */ |
||
| 286 | public function getFolder(): ?string |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Set slug. |
||
| 293 | * |
||
| 294 | * @param string $slug |
||
| 295 | * |
||
| 296 | * @return self |
||
| 297 | */ |
||
| 298 | public function setSlug(string $slug): self |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get slug. |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function getSlug(): string |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set path. |
||
| 324 | * |
||
| 325 | * @param string $path |
||
| 326 | * |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function setPath(string $path): self |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get path. |
||
| 362 | * |
||
| 363 | * @return string|null |
||
| 364 | */ |
||
| 365 | public function getPath(): ?string |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @see getPath() |
||
| 372 | * |
||
| 373 | * @return string|null |
||
| 374 | */ |
||
| 375 | public function getPathname(): ?string |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Set section. |
||
| 382 | * |
||
| 383 | * @param string $section |
||
| 384 | * |
||
| 385 | * @return self |
||
| 386 | */ |
||
| 387 | public function setSection(string $section): self |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get section. |
||
| 396 | * |
||
| 397 | * @return string|null |
||
| 398 | */ |
||
| 399 | public function getSection(): ?string |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set body as HTML. |
||
| 406 | * |
||
| 407 | * @param string $html |
||
| 408 | * |
||
| 409 | * @return self |
||
| 410 | */ |
||
| 411 | public function setBodyHtml(string $html): self |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get body as HTML. |
||
| 420 | * |
||
| 421 | * @return string|null |
||
| 422 | */ |
||
| 423 | public function getBodyHtml(): ?string |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @see getBodyHtml() |
||
| 430 | * |
||
| 431 | * @return string|null |
||
| 432 | */ |
||
| 433 | public function getContent(): ?string |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Return output file. |
||
| 440 | * |
||
| 441 | * Use cases: |
||
| 442 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
| 443 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
| 444 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
| 445 | * - path only (ie: _redirects) |
||
| 446 | * |
||
| 447 | * @param string $format |
||
| 448 | * @param Config $config |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function getOutputFile(string $format, Config $config = null): string |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Return URL. |
||
| 490 | * |
||
| 491 | * @param string $format |
||
| 492 | * @param Config $config |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | public function getUrl(string $format = 'html', Config $config = null): string |
||
| 507 | |||
| 508 | /* |
||
| 509 | * Helper to set and get variables. |
||
| 510 | */ |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Set an array as variables. |
||
| 514 | * |
||
| 515 | * @param array $variables |
||
| 516 | * |
||
| 517 | * @throws \Exception |
||
| 518 | * |
||
| 519 | * @return $this |
||
| 520 | */ |
||
| 521 | public function setVariables($variables) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get all variables. |
||
| 539 | * |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | public function getVariables(): array |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Set a variable. |
||
| 549 | * |
||
| 550 | * @param $name |
||
| 551 | * @param $value |
||
| 552 | * |
||
| 553 | * @throws \Exception |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function setVariable($name, $value) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Is variable exist? |
||
| 606 | * |
||
| 607 | * @param string $name |
||
| 608 | * |
||
| 609 | * @return bool |
||
| 610 | */ |
||
| 611 | public function hasVariable(string $name): bool |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get a variable. |
||
| 618 | * |
||
| 619 | * @param string $name |
||
| 620 | * |
||
| 621 | * @return mixed|null |
||
| 622 | */ |
||
| 623 | public function getVariable(string $name) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Unset a variable. |
||
| 632 | * |
||
| 633 | * @param string $name |
||
| 634 | * |
||
| 635 | * @return $this |
||
| 636 | */ |
||
| 637 | public function unVariable(string $name): self |
||
| 645 | } |
||
| 646 |
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..