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 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 | /** |
||
| 68 | * Constructor. |
||
| 69 | * |
||
| 70 | * @param string $id |
||
| 71 | */ |
||
| 72 | public function __construct(string $id) |
||
| 73 | { |
||
| 74 | parent::__construct($id); |
||
| 75 | $this->setVirtual(true); |
||
| 76 | $this->setType(Type::PAGE); |
||
| 77 | // default variables |
||
| 78 | $this->setVariables([ |
||
| 79 | 'title' => 'Page Title', |
||
| 80 | 'date' => time(), |
||
| 81 | 'updated' => time(), |
||
| 82 | 'weight' => null, |
||
| 83 | 'filepath' => null, |
||
| 84 | 'published' => true, |
||
| 85 | 'content_template' => 'page.content.twig', |
||
| 86 | ]); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Turn a path (string) into a slug (URI). |
||
| 91 | * |
||
| 92 | * @param string $path |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public static function slugify(string $path): string |
||
| 97 | { |
||
| 98 | return Slugify::create([ |
||
| 99 | 'regexp' => self::SLUGIFY_PATTERN, |
||
| 100 | ])->slugify($path); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Create ID from file. |
||
| 105 | * |
||
| 106 | * @param SplFileInfo $file |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public static function createId(SplFileInfo $file): string |
||
| 111 | { |
||
| 112 | $id = self::slugify(str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath()).'/'.Prefix::subPrefix($file->getBasename('.'.$file->getExtension()))); |
||
| 113 | |||
| 114 | return $id; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set file. |
||
| 119 | * |
||
| 120 | * @param SplFileInfo $file |
||
| 121 | * |
||
| 122 | * @return self |
||
| 123 | */ |
||
| 124 | public function setFile(SplFileInfo $file): self |
||
| 125 | { |
||
| 126 | $this->setVirtual(false); |
||
| 127 | $this->file = $file; |
||
| 128 | |||
| 129 | /* |
||
| 130 | * File path components |
||
| 131 | */ |
||
| 132 | $fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); |
||
| 133 | $fileExtension = $this->file->getExtension(); |
||
| 134 | $fileName = $this->file->getBasename('.'.$fileExtension); |
||
| 135 | /* |
||
| 136 | * Set protected variables |
||
| 137 | */ |
||
| 138 | $this->setFolder($fileRelativePath); // ie: "blog" |
||
| 139 | $this->setSlug($fileName); // ie: "post-1" |
||
| 140 | $this->setPath($this->getFolder().'/'.$this->getSlug()); // ie: "blog/post-1" |
||
| 141 | //$this->setSection(explode('/', $this->folder)[0]); // ie: "blog" |
||
| 142 | /* |
||
| 143 | * Set default variables |
||
| 144 | */ |
||
| 145 | $this->setVariables([ |
||
| 146 | 'title' => Prefix::subPrefix($fileName), |
||
| 147 | 'date' => $this->file->getCTime(), |
||
| 148 | 'updated' => $this->file->getMTime(), |
||
| 149 | 'filepath' => $this->file->getRelativePathname(), |
||
| 150 | ]); |
||
| 151 | // special case: file has a prefix |
||
| 152 | if (Prefix::hasPrefix($fileName)) { |
||
| 153 | $prefix = Prefix::getPrefix($fileName); |
||
| 154 | // prefix is a valid date? |
||
| 155 | if (Util::isValidDate($prefix)) { |
||
| 156 | $this->setVariable('date', (string) $prefix); |
||
| 157 | } else { |
||
| 158 | // prefix is an integer, use for sorting |
||
| 159 | $this->setVariable('weight', (int) $prefix); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Parse file content. |
||
| 168 | * |
||
| 169 | * @return self |
||
| 170 | */ |
||
| 171 | public function parse(): self |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get frontmatter. |
||
| 183 | * |
||
| 184 | * @return string|null |
||
| 185 | */ |
||
| 186 | public function getFrontmatter(): ?string |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get body as raw. |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getBody(): ?string |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set virtual status. |
||
| 203 | * |
||
| 204 | * @param bool $virtual |
||
| 205 | * |
||
| 206 | * @return self |
||
| 207 | */ |
||
| 208 | public function setVirtual(bool $virtual): self |
||
| 209 | { |
||
| 210 | $this->virtual = $virtual; |
||
| 211 | |||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Is current page is virtual? |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function isVirtual(): bool |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set page type. |
||
| 227 | * |
||
| 228 | * @param string $type |
||
| 229 | * |
||
| 230 | * @return self |
||
| 231 | */ |
||
| 232 | public function setType(string $type): self |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get page type. |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getType(): string |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set path without slug. |
||
| 251 | * |
||
| 252 | * @param string $folder |
||
| 253 | * |
||
| 254 | * @return self |
||
| 255 | */ |
||
| 256 | public function setFolder(string $folder): self |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get path without slug. |
||
| 265 | * |
||
| 266 | * @return string|null |
||
| 267 | */ |
||
| 268 | public function getFolder(): ?string |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set slug. |
||
| 275 | * |
||
| 276 | * @param string $slug |
||
| 277 | * |
||
| 278 | * @return self |
||
| 279 | */ |
||
| 280 | public function setSlug(string $slug): self |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get slug. |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function getSlug(): string |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Set path. |
||
| 304 | * |
||
| 305 | * @param string $path |
||
| 306 | * |
||
| 307 | * @return self |
||
| 308 | */ |
||
| 309 | public function setPath(string $path): self |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get path. |
||
| 338 | * |
||
| 339 | * @return string|null |
||
| 340 | */ |
||
| 341 | public function getPath(): ?string |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @see getPath() |
||
| 348 | * |
||
| 349 | * @return string|null |
||
| 350 | */ |
||
| 351 | public function getPathname(): ?string |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Set section. |
||
| 358 | * |
||
| 359 | * @param string $section |
||
| 360 | * |
||
| 361 | * @return self |
||
| 362 | */ |
||
| 363 | public function setSection(string $section): self |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get section. |
||
| 372 | * |
||
| 373 | * @return string|null |
||
| 374 | */ |
||
| 375 | public function getSection(): ?string |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Set body as HTML. |
||
| 386 | * |
||
| 387 | * @param string $html |
||
| 388 | * |
||
| 389 | * @return self |
||
| 390 | */ |
||
| 391 | public function setBodyHtml(string $html): self |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get body as HTML. |
||
| 400 | * |
||
| 401 | * @return string|null |
||
| 402 | */ |
||
| 403 | public function getBodyHtml(): ?string |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @see getBodyHtml() |
||
| 410 | * |
||
| 411 | * @return string|null |
||
| 412 | */ |
||
| 413 | public function getContent(): ?string |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Return output file. |
||
| 420 | * |
||
| 421 | * Use cases: |
||
| 422 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
| 423 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
| 424 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
| 425 | * - path only (ie: _redirects) |
||
| 426 | * |
||
| 427 | * @param string $format |
||
| 428 | * @param Config $config |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function getOutputFile(string $format, Config $config = null): string |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Return URL. |
||
| 470 | * |
||
| 471 | * @param string $format |
||
| 472 | * @param Config $config |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function getUrl(string $format = 'html', Config $config = null): string |
||
| 487 | |||
| 488 | /* |
||
| 489 | * Helper to set and get variables. |
||
| 490 | */ |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Set an array as variables. |
||
| 494 | * |
||
| 495 | * @param array $variables |
||
| 496 | * |
||
| 497 | * @throws \Exception |
||
| 498 | * |
||
| 499 | * @return $this |
||
| 500 | */ |
||
| 501 | public function setVariables($variables) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get all variables. |
||
| 515 | * |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | public function getVariables(): array |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set a variable. |
||
| 525 | * |
||
| 526 | * @param $name |
||
| 527 | * @param $value |
||
| 528 | * |
||
| 529 | * @throws \Exception |
||
| 530 | * |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | public function setVariable($name, $value) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Is variable exist? |
||
| 592 | * |
||
| 593 | * @param string $name |
||
| 594 | * |
||
| 595 | * @return bool |
||
| 596 | */ |
||
| 597 | public function hasVariable(string $name): bool |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get a variable. |
||
| 604 | * |
||
| 605 | * @param string $name |
||
| 606 | * |
||
| 607 | * @return mixed|null |
||
| 608 | */ |
||
| 609 | public function getVariable(string $name) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Unset a variable. |
||
| 618 | * |
||
| 619 | * @param string $name |
||
| 620 | * |
||
| 621 | * @return $this |
||
| 622 | */ |
||
| 623 | public function unVariable(string $name): self |
||
| 631 | } |
||
| 632 |
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..