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 $relpath; |
||
| 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) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Parse file content. |
||
| 151 | * |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | public function parse(): self |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get frontmatter. |
||
| 166 | * |
||
| 167 | * @return string|null |
||
| 168 | */ |
||
| 169 | public function getFrontmatter(): ?string |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get body as raw. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getBody(): ?string |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Turn a path (string) into a slug (URL). |
||
| 186 | * |
||
| 187 | * @param string $path |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public static function slugify(string $path): string |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Is current page is virtual? |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function isVirtual(): bool |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set page type. |
||
| 210 | * |
||
| 211 | * @param string $type |
||
| 212 | * |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | public function setType(string $type): self |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get page type. |
||
| 224 | * |
||
| 225 | * @return string|null |
||
| 226 | */ |
||
| 227 | public function getType(): ?string |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set slug. |
||
| 234 | * |
||
| 235 | * @param string $slug |
||
| 236 | * |
||
| 237 | * @return self |
||
| 238 | */ |
||
| 239 | public function setSlug(string $slug): self |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get slug. |
||
| 248 | * |
||
| 249 | * @return string|null |
||
| 250 | */ |
||
| 251 | public function geSlug(): ?string |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set relative path. |
||
| 258 | * |
||
| 259 | * @param $relpath |
||
| 260 | * |
||
| 261 | * @return self |
||
| 262 | */ |
||
| 263 | public function setRelPath(string $relpath): self |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get relative path. |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function getRelPath(): string |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Set path. |
||
| 282 | * |
||
| 283 | * @param string $path |
||
| 284 | * |
||
| 285 | * @return self |
||
| 286 | */ |
||
| 287 | public function setPath(string $path): self |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get path. |
||
| 296 | * |
||
| 297 | * @return string|null |
||
| 298 | */ |
||
| 299 | public function getPath(): ?string |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Set section. |
||
| 306 | * |
||
| 307 | * @param string $section |
||
| 308 | * |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | public function setSection(string $section): self |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get section. |
||
| 320 | * |
||
| 321 | * @return string|null |
||
| 322 | */ |
||
| 323 | public function getSection(): ?string |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set body as HTML. |
||
| 334 | * |
||
| 335 | * @param string $html |
||
| 336 | * |
||
| 337 | * @return self |
||
| 338 | */ |
||
| 339 | public function setBodyHtml(string $html): self |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get body as HTML. |
||
| 348 | * |
||
| 349 | * @return string|null |
||
| 350 | */ |
||
| 351 | public function getBodyHtml(): ?string |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @see getBodyHtml() |
||
| 358 | * |
||
| 359 | * @return string|null |
||
| 360 | */ |
||
| 361 | public function getContent(): ?string |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Return output file. |
||
| 368 | * |
||
| 369 | * Use cases: |
||
| 370 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
| 371 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
| 372 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
| 373 | * - path only (ie: _redirects) |
||
| 374 | * |
||
| 375 | * @param string $format |
||
| 376 | * @param Config $config |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | public function getOutputFile(string $format, Config $config = null): string |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Return URL. |
||
| 417 | * |
||
| 418 | * @param string $format |
||
| 419 | * @param Config $config |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function getUrl(string $format = 'html', Config $config = null): string |
||
| 434 | |||
| 435 | /* |
||
| 436 | * Helper to set and get variables. |
||
| 437 | */ |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set an array as variables. |
||
| 441 | * |
||
| 442 | * @param array $variables |
||
| 443 | * |
||
| 444 | * @throws \Exception |
||
| 445 | * |
||
| 446 | * @return $this |
||
| 447 | */ |
||
| 448 | public function setVariables($variables) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get all variables. |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | public function getVariables(): array |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set a variable. |
||
| 472 | * |
||
| 473 | * @param $name |
||
| 474 | * @param $value |
||
| 475 | * |
||
| 476 | * @throws \Exception |
||
| 477 | * |
||
| 478 | * @return $this |
||
| 479 | */ |
||
| 480 | public function setVariable($name, $value) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Is variable exist? |
||
| 526 | * |
||
| 527 | * @param $name |
||
| 528 | * |
||
| 529 | * @return bool |
||
| 530 | */ |
||
| 531 | public function hasVariable($name) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get a variable. |
||
| 538 | * |
||
| 539 | * @param string $name |
||
| 540 | * |
||
| 541 | * @return mixed|null |
||
| 542 | */ |
||
| 543 | public function getVariable($name) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Unset a variable. |
||
| 552 | * |
||
| 553 | * @param $name |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function unVariable($name) |
||
| 565 | } |
||
| 566 |
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..