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) |
||
74 | { |
||
75 | $this->file = $file; |
||
76 | |||
77 | // physical page |
||
78 | if ($this->file instanceof SplFileInfo) { |
||
79 | /* |
||
80 | * File path components |
||
81 | */ |
||
82 | // ie: content/Blog/Post 1.md |
||
83 | // | | └─ fileExtension |
||
84 | // | └─ fileName |
||
85 | // └─ filePath |
||
86 | $fileExtension = pathinfo($this->file, PATHINFO_EXTENSION); |
||
87 | $filePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); |
||
88 | $fileName = $this->file->getBasename('.'.$fileExtension); |
||
89 | // filePathname = filePath + '/' + fileName |
||
90 | // ie: content/Blog/Post 1.md -> "Blog/Post 1" |
||
91 | // ie: content/index.md -> "index" |
||
92 | // ie: content/Blog/index.md -> "Blog/" |
||
93 | $filePathname = ($filePath ? $filePath.'/' : '') |
||
94 | .($filePath && $fileName == 'index' ? '' : $fileName); |
||
95 | /* |
||
96 | * Set properties |
||
97 | * |
||
98 | * id = path = rootpath / slug |
||
99 | */ |
||
100 | $this->rootpath = $this->slugify($filePath); // ie: "blog" |
||
101 | $this->slug = $this->slugify(Prefix::subPrefix($fileName)); // ie: "post-1" |
||
102 | $this->path = $this->slugify(Prefix::subPrefix($filePathname)); // ie: "blog/post-1" |
||
103 | $this->id = $this->path; |
||
104 | /* |
||
105 | * Set protected variables |
||
106 | */ |
||
107 | $this->setSection(explode('/', $this->rootpath)[0]); // ie: "blog" |
||
108 | /* |
||
109 | * Set overridable variables |
||
110 | */ |
||
111 | $this->setVariable('title', Prefix::subPrefix($fileName)); // ie: "Post 1" |
||
112 | $this->setVariable('date', $this->file->getCTime()); |
||
113 | $this->setVariable('updated', $this->file->getMTime()); |
||
114 | $this->setVariable('weight', null); |
||
115 | // special case: file has a prefix |
||
116 | if (Prefix::hasPrefix($filePathname)) { |
||
117 | // prefix is a valid date? |
||
118 | if (Util::isValidDate(Prefix::getPrefix($filePathname))) { |
||
119 | $this->setVariable('date', (string) Prefix::getPrefix($filePathname)); |
||
120 | } else { |
||
121 | // prefix is an integer, use for sorting |
||
122 | $this->setVariable('weight', (int) Prefix::getPrefix($filePathname)); |
||
123 | } |
||
124 | } |
||
125 | // physical file relative path |
||
126 | $this->setVariable('filepath', $this->file->getRelativePathname()); |
||
127 | |||
128 | parent::__construct($this->id); |
||
129 | } else { |
||
130 | // virtual page |
||
131 | $this->virtual = true; |
||
132 | // default variables |
||
133 | $this->setVariables([ |
||
134 | 'title' => 'Page Title', |
||
135 | 'date' => time(), |
||
136 | 'updated' => time(), |
||
137 | 'weight' => null, |
||
138 | 'filepath' => null, |
||
139 | ]); |
||
140 | |||
141 | parent::__construct(); |
||
142 | } |
||
143 | // required |
||
144 | $this->setType(Type::PAGE); |
||
145 | $this->setVariables([ |
||
146 | 'published' => true, |
||
147 | 'virtual' => $this->virtual, |
||
148 | 'content_template' => 'page.content.twig', |
||
149 | ]); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Parse file content. |
||
154 | * |
||
155 | * @return self |
||
156 | */ |
||
157 | public function parse(): self |
||
158 | { |
||
159 | $parser = new Parser($this->file); |
||
160 | $parsed = $parser->parse(); |
||
161 | $this->frontmatter = $parsed->getFrontmatter(); |
||
162 | $this->body = $parsed->getBody(); |
||
163 | |||
164 | return $this; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get frontmatter. |
||
169 | * |
||
170 | * @return string|null |
||
171 | */ |
||
172 | public function getFrontmatter(): ?string |
||
173 | { |
||
174 | return $this->frontmatter; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get body as raw. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getBody(): ?string |
||
183 | { |
||
184 | return $this->body; |
||
185 | } |
||
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 |
||
195 | { |
||
196 | return Slugify::create([ |
||
197 | 'regexp' => self::SLUGIFY_PATTERN, |
||
198 | ])->slugify($path); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Is current page is virtual? |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function isVirtual(): bool |
||
207 | { |
||
208 | return $this->virtual; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Set page type. |
||
213 | * |
||
214 | * @param string $type |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | public function setType(string $type): self |
||
219 | { |
||
220 | $this->type = new Type($type); |
||
|
|||
221 | |||
222 | return $this; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Get page type. |
||
227 | * |
||
228 | * @return string|null |
||
229 | */ |
||
230 | public function getType(): ?string |
||
231 | { |
||
232 | return $this->type; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Set slug. |
||
237 | * |
||
238 | * @param string $slug |
||
239 | * |
||
240 | * @return self |
||
241 | */ |
||
242 | public function setSlug(string $slug): self |
||
243 | { |
||
244 | $this->slug = $slug; |
||
245 | |||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Get slug. |
||
251 | * |
||
252 | * @return string|null |
||
253 | */ |
||
254 | public function geSlug(): ?string |
||
255 | { |
||
256 | return $this->slug; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Set relative path. |
||
261 | * |
||
262 | * @param $rootpath |
||
263 | * |
||
264 | * @return self |
||
265 | */ |
||
266 | public function setRootPath(string $rootpath): self |
||
267 | { |
||
268 | $this->rootpath = $rootpath; |
||
269 | |||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get relative path. |
||
275 | * |
||
276 | * @return string|null |
||
277 | */ |
||
278 | public function getRootPath(): ?string |
||
279 | { |
||
280 | return $this->rootpath; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Set path. |
||
285 | * |
||
286 | * @param string $path |
||
287 | * |
||
288 | * @return self |
||
289 | */ |
||
290 | public function setPath(string $path): self |
||
291 | { |
||
292 | $this->path = $path; |
||
293 | |||
294 | return $this; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Get path. |
||
299 | * |
||
300 | * @return string|null |
||
301 | */ |
||
302 | public function getPath(): ?string |
||
303 | { |
||
304 | return $this->path; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Set section. |
||
309 | * |
||
310 | * @param string $section |
||
311 | * |
||
312 | * @return self |
||
313 | */ |
||
314 | public function setSection(string $section): self |
||
320 | |||
321 | /** |
||
322 | * Get section. |
||
323 | * |
||
324 | * @return string|null |
||
325 | */ |
||
326 | public function getSection(): ?string |
||
334 | |||
335 | /** |
||
336 | * Set body as HTML. |
||
337 | * |
||
338 | * @param string $html |
||
339 | * |
||
340 | * @return self |
||
341 | */ |
||
342 | public function setBodyHtml(string $html): self |
||
343 | { |
||
344 | $this->html = $html; |
||
345 | |||
346 | return $this; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Get body as HTML. |
||
351 | * |
||
352 | * @return string|null |
||
353 | */ |
||
354 | public function getBodyHtml(): ?string |
||
358 | |||
359 | /** |
||
360 | * @see getBodyHtml() |
||
361 | * |
||
362 | * @return string|null |
||
363 | */ |
||
364 | public function getContent(): ?string |
||
368 | |||
369 | /** |
||
370 | * Return output file. |
||
371 | * |
||
372 | * Use cases: |
||
373 | * - default: path + suffix + extension (ie: blog/post-1/index.html) |
||
374 | * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
375 | * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
376 | * - path only (ie: _redirects) |
||
377 | * |
||
378 | * @param string $format |
||
379 | * @param Config $config |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | public function getOutputFile(string $format, Config $config = null): string |
||
417 | |||
418 | /** |
||
419 | * Return URL. |
||
420 | * |
||
421 | * @param string $format |
||
422 | * @param Config $config |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | public function getUrl(string $format = 'html', Config $config = null): string |
||
437 | |||
438 | /* |
||
439 | * Helper to set and get variables. |
||
440 | */ |
||
441 | |||
442 | /** |
||
443 | * Set an array as variables. |
||
444 | * |
||
445 | * @param array $variables |
||
446 | * |
||
447 | * @throws \Exception |
||
448 | * |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function setVariables($variables) |
||
462 | |||
463 | /** |
||
464 | * Get all variables. |
||
465 | * |
||
466 | * @return array |
||
467 | */ |
||
468 | public function getVariables(): array |
||
472 | |||
473 | /** |
||
474 | * Set a variable. |
||
475 | * |
||
476 | * @param $name |
||
477 | * @param $value |
||
478 | * |
||
479 | * @throws \Exception |
||
480 | * |
||
481 | * @return $this |
||
482 | */ |
||
483 | public function setVariable($name, $value) |
||
526 | |||
527 | /** |
||
528 | * Is variable exist? |
||
529 | * |
||
530 | * @param $name |
||
531 | * |
||
532 | * @return bool |
||
533 | */ |
||
534 | public function hasVariable($name) |
||
538 | |||
539 | /** |
||
540 | * Get a variable. |
||
541 | * |
||
542 | * @param string $name |
||
543 | * |
||
544 | * @return mixed|null |
||
545 | */ |
||
546 | public function getVariable($name) |
||
552 | |||
553 | /** |
||
554 | * Unset a variable. |
||
555 | * |
||
556 | * @param $name |
||
557 | * |
||
558 | * @return $this |
||
559 | */ |
||
560 | public function unVariable($name) |
||
568 | } |
||
569 |
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..