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 |
||
| 23 | class Page extends Item |
||
| 24 | { |
||
| 25 | const SLUGIFY_PATTERN = '/(^\/|[^_a-z0-9\/]|-)+/'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var SplFileInfo |
||
| 29 | */ |
||
| 30 | protected $file; |
||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $virtual = false; |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $type; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $rootpath; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $path; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $slug; |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $section; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $frontmatter; |
||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $body; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $html; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Constructor. |
||
| 70 | * |
||
| 71 | * @param SplFileInfo|null $file |
||
| 72 | */ |
||
| 73 | public function __construct(SplFileInfo $file = null) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Parse file content. |
||
| 154 | * |
||
| 155 | * @return self |
||
| 156 | */ |
||
| 157 | public function parse(): self |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get frontmatter. |
||
| 169 | * |
||
| 170 | * @return string|null |
||
| 171 | */ |
||
| 172 | public function getFrontmatter(): ?string |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get body as raw. |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getBody(): ?string |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Turn a path (string) into a slug (URL). |
||
| 189 | * |
||
| 190 | * @param string $path |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public static function slugify(string $path): string |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Is current page is virtual? |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function isVirtual(): bool |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set page type. |
||
| 213 | * |
||
| 214 | * @param string $type |
||
| 215 | * |
||
| 216 | * @return self |
||
| 217 | */ |
||
| 218 | public function setType(string $type): self |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get page type. |
||
| 227 | * |
||
| 228 | * @return string|null |
||
| 229 | */ |
||
| 230 | public function getType(): ?string |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set slug. |
||
| 237 | * |
||
| 238 | * @param string $slug |
||
| 239 | * |
||
| 240 | * @return self |
||
| 241 | */ |
||
| 242 | public function setSlug(string $slug): self |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get slug. |
||
| 251 | * |
||
| 252 | * @return string|null |
||
| 253 | */ |
||
| 254 | public function getSlug(): ?string |
||
| 255 | { |
||
| 256 | return $this->slug; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Set relative path. |
||
| 261 | * |
||
| 262 | * @param $rootpath |
||
| 263 | * |
||
| 264 | * @return self |
||
| 265 | */ |
||
| 266 | public function setRootPath(string $rootpath): self |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get relative path. |
||
| 275 | * |
||
| 276 | * @return string|null |
||
| 277 | */ |
||
| 278 | public function getRootPath(): ?string |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set path. |
||
| 285 | * |
||
| 286 | * @param string $path |
||
| 287 | * |
||
| 288 | * @return self |
||
| 289 | */ |
||
| 290 | public function setPath(string $path): self |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get path. |
||
| 299 | * |
||
| 300 | * @return string|null |
||
| 301 | */ |
||
| 302 | public function getPath(): ?string |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Backward compatibility. |
||
| 316 | */ |
||
| 317 | public function getPathname() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set section. |
||
| 324 | * |
||
| 325 | * @param string $section |
||
| 326 | * |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function setSection(string $section): self |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get section. |
||
| 338 | * |
||
| 339 | * @return string|null |
||
| 340 | */ |
||
| 341 | public function getSection(): ?string |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Set body as HTML. |
||
| 352 | * |
||
| 353 | * @param string $html |
||
| 354 | * |
||
| 355 | * @return self |
||
| 356 | */ |
||
| 357 | public function setBodyHtml(string $html): self |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get body as HTML. |
||
| 366 | * |
||
| 367 | * @return string|null |
||
| 368 | */ |
||
| 369 | public function getBodyHtml(): ?string |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @see getBodyHtml() |
||
| 376 | * |
||
| 377 | * @return string|null |
||
| 378 | */ |
||
| 379 | public function getContent(): ?string |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Return output file. |
||
| 386 | * |
||
| 387 | * Use cases: |
||
| 388 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
| 389 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
| 390 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
| 391 | * - path only (ie: _redirects) |
||
| 392 | * |
||
| 393 | * @param string $format |
||
| 394 | * @param Config $config |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function getOutputFile(string $format, Config $config = null): string |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Return URL. |
||
| 436 | * |
||
| 437 | * @param string $format |
||
| 438 | * @param Config $config |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getUrl(string $format = 'html', Config $config = null): string |
||
| 453 | |||
| 454 | /* |
||
| 455 | * Helper to set and get variables. |
||
| 456 | */ |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Set an array as variables. |
||
| 460 | * |
||
| 461 | * @param array $variables |
||
| 462 | * |
||
| 463 | * @throws \Exception |
||
| 464 | * |
||
| 465 | * @return $this |
||
| 466 | */ |
||
| 467 | public function setVariables($variables) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get all variables. |
||
| 481 | * |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | public function getVariables(): array |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Set a variable. |
||
| 491 | * |
||
| 492 | * @param $name |
||
| 493 | * @param $value |
||
| 494 | * |
||
| 495 | * @throws \Exception |
||
| 496 | * |
||
| 497 | * @return $this |
||
| 498 | */ |
||
| 499 | public function setVariable($name, $value) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Is variable exist? |
||
| 550 | * |
||
| 551 | * @param $name |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | public function hasVariable($name) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Get a variable. |
||
| 562 | * |
||
| 563 | * @param string $name |
||
| 564 | * |
||
| 565 | * @return mixed|null |
||
| 566 | */ |
||
| 567 | public function getVariable($name) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Unset a variable. |
||
| 576 | * |
||
| 577 | * @param $name |
||
| 578 | * |
||
| 579 | * @return $this |
||
| 580 | */ |
||
| 581 | public function unVariable($name) |
||
| 589 | } |
||
| 590 |
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..