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) |
||
168 | |||
169 | /** |
||
170 | * Parse file content. |
||
171 | * |
||
172 | * @return self |
||
173 | */ |
||
174 | public function parse(): self |
||
183 | |||
184 | /** |
||
185 | * Get frontmatter. |
||
186 | * |
||
187 | * @return string|null |
||
188 | */ |
||
189 | public function getFrontmatter(): ?string |
||
193 | |||
194 | /** |
||
195 | * Get body as raw. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getBody(): ?string |
||
203 | |||
204 | /** |
||
205 | * Turn a path (string) into a slug (URL). |
||
206 | * |
||
207 | * @param string $path |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public static function slugify(string $path): string |
||
217 | |||
218 | /** |
||
219 | * Is current page is virtual? |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function isVirtual(): bool |
||
227 | |||
228 | /** |
||
229 | * Set page type. |
||
230 | * |
||
231 | * @param string $type |
||
232 | * |
||
233 | * @return self |
||
234 | */ |
||
235 | public function setType(string $type): self |
||
241 | |||
242 | /** |
||
243 | * Get page type. |
||
244 | * |
||
245 | * @return string|null |
||
246 | */ |
||
247 | public function getType(): ?string |
||
251 | |||
252 | /** |
||
253 | * Set name. |
||
254 | * |
||
255 | * @param string $name |
||
256 | * |
||
257 | * @return self |
||
258 | */ |
||
259 | public function setName(string $name): self |
||
265 | |||
266 | /** |
||
267 | * Get name. |
||
268 | * |
||
269 | * @return string|null |
||
270 | */ |
||
271 | public function getName(): ?string |
||
275 | |||
276 | /** |
||
277 | * Set path. |
||
278 | * |
||
279 | * @param $path |
||
280 | * |
||
281 | * @return self |
||
282 | */ |
||
283 | public function setPath(string $path): self |
||
289 | |||
290 | /** |
||
291 | * Get path. |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public function getPath(): string |
||
299 | |||
300 | /** |
||
301 | * Set path name. |
||
302 | * |
||
303 | * @param string $pathname |
||
304 | * |
||
305 | * @return self |
||
306 | */ |
||
307 | public function setPathname(string $pathname): self |
||
313 | |||
314 | /** |
||
315 | * Get path name. |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | public function getPathname(): string |
||
329 | |||
330 | /** |
||
331 | * Set section. |
||
332 | * |
||
333 | * @param string $section |
||
334 | * |
||
335 | * @return self |
||
336 | */ |
||
337 | public function setSection(string $section): self |
||
343 | |||
344 | /** |
||
345 | * Get section. |
||
346 | * |
||
347 | * @return string|null |
||
348 | */ |
||
349 | public function getSection(): ?string |
||
357 | |||
358 | /** |
||
359 | * Set body as HTML. |
||
360 | * |
||
361 | * @param string $html |
||
362 | * |
||
363 | * @return self |
||
364 | */ |
||
365 | public function setBodyHtml(string $html): self |
||
371 | |||
372 | /** |
||
373 | * Get body as HTML. |
||
374 | * |
||
375 | * @return string|null |
||
376 | */ |
||
377 | public function getBodyHtml(): ?string |
||
381 | |||
382 | /** |
||
383 | * @see getBodyHtml() |
||
384 | * |
||
385 | * @return string|null |
||
386 | */ |
||
387 | public function getContent(): ?string |
||
391 | |||
392 | /** |
||
393 | * Return output file. |
||
394 | * |
||
395 | * Use cases: |
||
396 | * - default: pathname + suffix + extension (ie: blog/post-1/index.html) |
||
397 | * - subpath: pathname + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
398 | * - ugly: pathname + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
399 | * - pathname only (ie: _redirects) |
||
400 | * |
||
401 | * @param string $format |
||
402 | * @param Config $config |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | public function getOutputFile(string $format, Config $config = null): string |
||
440 | |||
441 | /** |
||
442 | * Return URL. |
||
443 | * |
||
444 | * @param string $format |
||
445 | * @param Config $config |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getUrl(string $format = 'html', Config $config = null): string |
||
459 | |||
460 | /* |
||
461 | * Helper to set and get variables. |
||
462 | */ |
||
463 | |||
464 | /** |
||
465 | * Set an array as variables. |
||
466 | * |
||
467 | * @param array $variables |
||
468 | * |
||
469 | * @throws \Exception |
||
470 | * |
||
471 | * @return $this |
||
472 | */ |
||
473 | public function setVariables($variables) |
||
484 | |||
485 | /** |
||
486 | * Get all variables. |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | public function getVariables(): array |
||
494 | |||
495 | /** |
||
496 | * Set a variable. |
||
497 | * |
||
498 | * @param $name |
||
499 | * @param $value |
||
500 | * |
||
501 | * @throws \Exception |
||
502 | * |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function setVariable($name, $value) |
||
548 | |||
549 | /** |
||
550 | * Is variable exist? |
||
551 | * |
||
552 | * @param $name |
||
553 | * |
||
554 | * @return bool |
||
555 | */ |
||
556 | public function hasVariable($name) |
||
560 | |||
561 | /** |
||
562 | * Get a variable. |
||
563 | * |
||
564 | * @param string $name |
||
565 | * |
||
566 | * @return mixed|null |
||
567 | */ |
||
568 | public function getVariable($name) |
||
574 | |||
575 | /** |
||
576 | * Unset a variable. |
||
577 | * |
||
578 | * @param $name |
||
579 | * |
||
580 | * @return $this |
||
581 | */ |
||
582 | public function unVariable($name) |
||
590 | } |
||
591 |
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..