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 geSlug(): ?string |
||
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 |
||
306 | |||
307 | /** |
||
308 | * Backward compatibility. |
||
309 | */ |
||
310 | public function getPathname() |
||
314 | |||
315 | /** |
||
316 | * Set section. |
||
317 | * |
||
318 | * @param string $section |
||
319 | * |
||
320 | * @return self |
||
321 | */ |
||
322 | public function setSection(string $section): self |
||
328 | |||
329 | /** |
||
330 | * Get section. |
||
331 | * |
||
332 | * @return string|null |
||
333 | */ |
||
334 | public function getSection(): ?string |
||
342 | |||
343 | /** |
||
344 | * Set body as HTML. |
||
345 | * |
||
346 | * @param string $html |
||
347 | * |
||
348 | * @return self |
||
349 | */ |
||
350 | public function setBodyHtml(string $html): self |
||
356 | |||
357 | /** |
||
358 | * Get body as HTML. |
||
359 | * |
||
360 | * @return string|null |
||
361 | */ |
||
362 | public function getBodyHtml(): ?string |
||
366 | |||
367 | /** |
||
368 | * @see getBodyHtml() |
||
369 | * |
||
370 | * @return string|null |
||
371 | */ |
||
372 | public function getContent(): ?string |
||
376 | |||
377 | /** |
||
378 | * Return output file. |
||
379 | * |
||
380 | * Use cases: |
||
381 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
382 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
383 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
384 | * - path only (ie: _redirects) |
||
385 | * |
||
386 | * @param string $format |
||
387 | * @param Config $config |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | public function getOutputFile(string $format, Config $config = null): string |
||
425 | |||
426 | /** |
||
427 | * Return URL. |
||
428 | * |
||
429 | * @param string $format |
||
430 | * @param Config $config |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | public function getUrl(string $format = 'html', Config $config = null): string |
||
445 | |||
446 | /* |
||
447 | * Helper to set and get variables. |
||
448 | */ |
||
449 | |||
450 | /** |
||
451 | * Set an array as variables. |
||
452 | * |
||
453 | * @param array $variables |
||
454 | * |
||
455 | * @throws \Exception |
||
456 | * |
||
457 | * @return $this |
||
458 | */ |
||
459 | public function setVariables($variables) |
||
470 | |||
471 | /** |
||
472 | * Get all variables. |
||
473 | * |
||
474 | * @return array |
||
475 | */ |
||
476 | public function getVariables(): array |
||
480 | |||
481 | /** |
||
482 | * Set a variable. |
||
483 | * |
||
484 | * @param $name |
||
485 | * @param $value |
||
486 | * |
||
487 | * @throws \Exception |
||
488 | * |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function setVariable($name, $value) |
||
539 | |||
540 | /** |
||
541 | * Is variable exist? |
||
542 | * |
||
543 | * @param $name |
||
544 | * |
||
545 | * @return bool |
||
546 | */ |
||
547 | public function hasVariable($name) |
||
551 | |||
552 | /** |
||
553 | * Get a variable. |
||
554 | * |
||
555 | * @param string $name |
||
556 | * |
||
557 | * @return mixed|null |
||
558 | */ |
||
559 | public function getVariable($name) |
||
565 | |||
566 | /** |
||
567 | * Unset a variable. |
||
568 | * |
||
569 | * @param $name |
||
570 | * |
||
571 | * @return $this |
||
572 | */ |
||
573 | public function unVariable($name) |
||
581 | } |
||
582 |
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..