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 string |
||
| 33 | */ |
||
| 34 | protected $fileExtension; |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $filePath; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $fileName; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $filePathname; |
||
| 47 | /** |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $virtual = false; |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $type; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $pathname; |
||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $path; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $name; |
||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $section; |
||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $frontmatter; |
||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $body; |
||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $html; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Constructor. |
||
| 86 | * |
||
| 87 | * @param SplFileInfo|null $file |
||
| 88 | */ |
||
| 89 | public function __construct(SplFileInfo $file = null) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Turn a path (string) into a slung (URL). |
||
| 169 | * |
||
| 170 | * @param string $string |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public static function slugify(string $string): string |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Is current page is virtual? |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function isVirtual(): bool |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Set page type. |
||
| 193 | * |
||
| 194 | * @param string $type |
||
| 195 | * |
||
| 196 | * @return self |
||
| 197 | */ |
||
| 198 | public function setType(string $type): self |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get page type. |
||
| 207 | * |
||
| 208 | * @return string|null |
||
| 209 | */ |
||
| 210 | public function getType(): ?string |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Parse file content. |
||
| 217 | * |
||
| 218 | * @return self |
||
| 219 | */ |
||
| 220 | public function parse(): self |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set name. |
||
| 232 | * |
||
| 233 | * @param string $name |
||
| 234 | * |
||
| 235 | * @return self |
||
| 236 | */ |
||
| 237 | public function setName(string $name): self |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get name. |
||
| 246 | * |
||
| 247 | * @return string|null |
||
| 248 | */ |
||
| 249 | public function getName(): ?string |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set path. |
||
| 256 | * |
||
| 257 | * @param $path |
||
| 258 | * |
||
| 259 | * @return self |
||
| 260 | */ |
||
| 261 | public function setPath(string $path): self |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get path. |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getPath(): string |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set path name. |
||
| 280 | * |
||
| 281 | * @param string $pathname |
||
| 282 | * |
||
| 283 | * @return self |
||
| 284 | */ |
||
| 285 | public function setPathname(string $pathname): self |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get path name. |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getPathname(): string |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set section. |
||
| 310 | * |
||
| 311 | * @param string $section |
||
| 312 | * |
||
| 313 | * @return self |
||
| 314 | */ |
||
| 315 | public function setSection(string $section): self |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get section. |
||
| 324 | * |
||
| 325 | * @return string|false |
||
| 326 | */ |
||
| 327 | public function getSection(): ?string |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get frontmatter. |
||
| 338 | * |
||
| 339 | * @return string|null |
||
| 340 | */ |
||
| 341 | public function getFrontmatter(): ?string |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get body as raw. |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function getBody(): ?string |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Set body as HTML. |
||
| 358 | * |
||
| 359 | * @param string $html |
||
| 360 | * |
||
| 361 | * @return self |
||
| 362 | */ |
||
| 363 | public function setBodyHtml(string $html): self |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get body as HTML. |
||
| 372 | * |
||
| 373 | * @return string|null |
||
| 374 | */ |
||
| 375 | public function getBodyHtml(): ?string |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @see getBodyHtml() |
||
| 382 | * |
||
| 383 | * @return string|null |
||
| 384 | */ |
||
| 385 | public function getContent(): ?string |
||
| 389 | |||
| 390 | /* |
||
| 391 | * Helper to set and get variables. |
||
| 392 | */ |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set an array as variables. |
||
| 396 | * |
||
| 397 | * @param array $variables |
||
| 398 | * |
||
| 399 | * @throws \Exception |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | public function setVariables($variables) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get all variables. |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | public function getVariables() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set a variable. |
||
| 427 | * |
||
| 428 | * @param $name |
||
| 429 | * @param $value |
||
| 430 | * |
||
| 431 | * @throws \Exception |
||
| 432 | * |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | public function setVariable($name, $value) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Is variable exist? |
||
| 478 | * |
||
| 479 | * @param $name |
||
| 480 | * |
||
| 481 | * @return bool |
||
| 482 | */ |
||
| 483 | public function hasVariable($name) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get a variable. |
||
| 490 | * |
||
| 491 | * @param string $name |
||
| 492 | * |
||
| 493 | * @return mixed|null |
||
| 494 | */ |
||
| 495 | public function getVariable($name) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Unset a variable. |
||
| 504 | * |
||
| 505 | * @param $name |
||
| 506 | * |
||
| 507 | * @return $this |
||
| 508 | */ |
||
| 509 | public function unVariable($name) |
||
| 517 | } |
||
| 518 |
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..