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 |
||
| 21 | class Page extends Item |
||
| 22 | { |
||
| 23 | use VariableTrait; |
||
| 24 | |||
| 25 | const SLUGIFY_PATTERN = '/(^\/|[^a-z0-9\/]|-)+/'; |
||
| 26 | // https://regex101.com/r/tJWUrd/1 |
||
| 27 | const PREFIX_PATTERN = '^(.*?)(([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])|[0-9]+)(-|_|\.)(.*)$'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var SplFileInfo |
||
| 31 | */ |
||
| 32 | protected $file; |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $fileExtension; |
||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $filePath; |
||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $fileName; |
||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $fileId; |
||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $virtual = false; |
||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $type; |
||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $id; |
||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $pathname; |
||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $path; |
||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $name; |
||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $frontmatter; |
||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $body; |
||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $html; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Constructor. |
||
| 88 | * |
||
| 89 | * @param SplFileInfo|null $file |
||
| 90 | */ |
||
| 91 | public function __construct(SplFileInfo $file = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Return matches array if prefix exist or false. |
||
| 157 | * |
||
| 158 | * @param string $string |
||
| 159 | * |
||
| 160 | * @return string[]|false |
||
| 161 | */ |
||
| 162 | public static function hasPrefix(string $string) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Return prefix if prefix or false. |
||
| 173 | * |
||
| 174 | * @param string $string |
||
| 175 | * |
||
| 176 | * @return string[]|false |
||
|
|
|||
| 177 | */ |
||
| 178 | public static function getPrefix(string $string) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return string without prefix (if exist). |
||
| 189 | * |
||
| 190 | * @param string $string |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public static function subPrefix(string $string): string |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Format string into URL. |
||
| 205 | * |
||
| 206 | * @param string $string |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public static function urlize(string $string): string |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Is current page is virtual? |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function isVirtual(): bool |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set page type. |
||
| 229 | * |
||
| 230 | * @param string $type |
||
| 231 | * |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | public function setType(string $type): self |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get page type. |
||
| 243 | * |
||
| 244 | * @return string|null |
||
| 245 | */ |
||
| 246 | public function getType(): ?string |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Parse file content. |
||
| 253 | * |
||
| 254 | * @return self |
||
| 255 | */ |
||
| 256 | public function parse(): self |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set name. |
||
| 268 | * |
||
| 269 | * @param string $name |
||
| 270 | * |
||
| 271 | * @return self |
||
| 272 | */ |
||
| 273 | public function setName(string $name): self |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get name. |
||
| 282 | * |
||
| 283 | * @return string|null |
||
| 284 | */ |
||
| 285 | public function getName(): ?string |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Set path. |
||
| 292 | * |
||
| 293 | * @param $path |
||
| 294 | * |
||
| 295 | * @return self |
||
| 296 | */ |
||
| 297 | public function setPath(string $path): self |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get path. |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getPath(): string |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Set path name. |
||
| 316 | * |
||
| 317 | * @param string $pathname |
||
| 318 | * |
||
| 319 | * @return self |
||
| 320 | */ |
||
| 321 | public function setPathname(string $pathname): self |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get path name. |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function getPathname(): string |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set section. |
||
| 340 | * |
||
| 341 | * @param string $section |
||
| 342 | * |
||
| 343 | * @return self |
||
| 344 | */ |
||
| 345 | public function setSection(string $section): self |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get section. |
||
| 354 | * |
||
| 355 | * @return string|false |
||
| 356 | */ |
||
| 357 | public function getSection(): ?string |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Set title. |
||
| 368 | * |
||
| 369 | * @param string $title |
||
| 370 | * |
||
| 371 | * @return self |
||
| 372 | */ |
||
| 373 | public function setTitle(string $title): self |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get title. |
||
| 382 | * |
||
| 383 | * @return string|false |
||
| 384 | */ |
||
| 385 | public function getTitle(): ?string |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set date. |
||
| 392 | * |
||
| 393 | * @param string|\DateTime $date |
||
| 394 | * |
||
| 395 | * @return self |
||
| 396 | */ |
||
| 397 | public function setDate($date): self |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get Date. |
||
| 406 | * |
||
| 407 | * @return \DateTime|false |
||
| 408 | */ |
||
| 409 | public function getDate(): ?\DateTime |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Set weight. |
||
| 416 | * |
||
| 417 | * @param int $weight |
||
| 418 | * |
||
| 419 | * @return self |
||
| 420 | */ |
||
| 421 | public function setWeight(int $weight): self |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Get weight. |
||
| 430 | * |
||
| 431 | * @return int|null |
||
| 432 | */ |
||
| 433 | public function getWeight(): ?int |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Set permalink. |
||
| 440 | * |
||
| 441 | * @param string $permalink |
||
| 442 | * |
||
| 443 | * @return self |
||
| 444 | */ |
||
| 445 | public function setPermalink(string $permalink): self |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get permalink. |
||
| 454 | * |
||
| 455 | * @return string|false |
||
| 456 | */ |
||
| 457 | public function getPermalink(): ?string |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get frontmatter. |
||
| 468 | * |
||
| 469 | * @return string|null |
||
| 470 | */ |
||
| 471 | public function getFrontmatter(): ?string |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get body as raw. |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getBody(): ?string |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set body as HTML. |
||
| 488 | * |
||
| 489 | * @param string $html |
||
| 490 | * |
||
| 491 | * @return self |
||
| 492 | */ |
||
| 493 | public function setBodyHtml(string $html): self |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Get body as HTML. |
||
| 502 | * |
||
| 503 | * @return string|null |
||
| 504 | */ |
||
| 505 | public function getBodyHtml(): ?string |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @see getBodyHtml() |
||
| 512 | * |
||
| 513 | * @return string|null |
||
| 514 | */ |
||
| 515 | public function getContent(): ?string |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Set layout. |
||
| 522 | * |
||
| 523 | * @param string $layout |
||
| 524 | * |
||
| 525 | * @return self |
||
| 526 | */ |
||
| 527 | public function setLayout(string $layout): self |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get layout. |
||
| 536 | * |
||
| 537 | * @return string|false |
||
| 538 | */ |
||
| 539 | public function getLayout(): ?string |
||
| 543 | } |
||
| 544 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.