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) |
||
90 | { |
||
91 | $this->file = $file; |
||
92 | |||
93 | // physical page |
||
94 | if ($this->file instanceof SplFileInfo) { |
||
95 | /* |
||
96 | * File path components |
||
97 | */ |
||
98 | // ie: content/Blog/Post 1.md |
||
99 | // | | └─ fileExtension |
||
100 | // | └─ fileName |
||
101 | // └─ filePath |
||
102 | $this->fileExtension = pathinfo($this->file, PATHINFO_EXTENSION); |
||
103 | $this->filePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); |
||
104 | $this->fileName = $this->file->getBasename('.'.$this->fileExtension); |
||
105 | // filePathname = filePath + '/' + fileName |
||
106 | // ie: content/Blog/Post 1.md -> "Blog/Post 1" |
||
107 | // ie: content/index.md -> "index" |
||
108 | // ie: content/Blog/index.md -> "Blog/" |
||
109 | $this->filePathname = ($this->filePath ? $this->filePath.'/' : '') |
||
110 | .($this->filePath && $this->fileName == 'index' ? '' : $this->fileName); |
||
111 | /* |
||
112 | * Set properties |
||
113 | */ |
||
114 | // ID. ie: "blog/post-1" |
||
115 | $this->id = $this->slugify(Prefix::subPrefix($this->filePathname)); |
||
116 | // Path. ie: "blog" |
||
117 | $this->path = $this->slugify($this->filePath); |
||
118 | // Name. ie: "post-1" |
||
119 | $this->name = $this->slugify(Prefix::subPrefix($this->fileName)); |
||
120 | // Pathname. ie: "blog/post-1" |
||
121 | $this->pathname = $this->slugify(Prefix::subPrefix($this->filePathname)); |
||
122 | /* |
||
123 | * Set variables |
||
124 | */ |
||
125 | // Section. ie: "blog" |
||
126 | $this->setSection(explode('/', $this->path)[0]); |
||
127 | /* |
||
128 | * Set variables overridden by front matter |
||
129 | */ |
||
130 | // title. ie: "Post 1" |
||
131 | $this->setVariable('title', Prefix::subPrefix($this->fileName)); |
||
132 | // date (from file meta) |
||
133 | $this->setVariable('date', filemtime($this->file->getPathname())); |
||
134 | // weight |
||
135 | $this->setVariable('weight', null); |
||
136 | // special case: file has a prefix |
||
137 | if (Prefix::hasPrefix($this->filePathname)) { |
||
138 | // prefix is a valid date? |
||
139 | if (Util::isValidDate(Prefix::getPrefix($this->filePathname))) { |
||
140 | $this->setVariable('date', (string) Prefix::getPrefix($this->filePathname)); |
||
141 | } else { |
||
142 | // prefix is an integer, use for sorting |
||
143 | $this->setVariable('weight', (int) Prefix::getPrefix($this->filePathname)); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | parent::__construct($this->id); |
||
148 | } else { |
||
149 | // virtual page |
||
150 | $this->virtual = true; |
||
151 | // default variables |
||
152 | $this->setVariables([ |
||
153 | 'title' => 'Default Title', |
||
154 | 'date' => time(), |
||
155 | 'weight' => null, |
||
156 | ]); |
||
157 | |||
158 | parent::__construct(); |
||
159 | } |
||
160 | $this->setType(Type::PAGE); |
||
161 | // required variables |
||
162 | $this->setVariable('virtual', $this->virtual); |
||
163 | $this->setVariable('published', true); |
||
164 | $this->setVariable('content_template', 'page.content.twig'); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Parse file content. |
||
169 | * |
||
170 | * @return self |
||
171 | */ |
||
172 | public function parse(): self |
||
173 | { |
||
174 | $parser = new Parser($this->file); |
||
175 | $parsed = $parser->parse(); |
||
176 | $this->frontmatter = $parsed->getFrontmatter(); |
||
177 | $this->body = $parsed->getBody(); |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Get frontmatter. |
||
184 | * |
||
185 | * @return string|null |
||
186 | */ |
||
187 | public function getFrontmatter(): ?string |
||
188 | { |
||
189 | return $this->frontmatter; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get body as raw. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getBody(): ?string |
||
198 | { |
||
199 | return $this->body; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Turn a path (string) into a slug (URL). |
||
204 | * |
||
205 | * @param string $path |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public static function slugify(string $path): string |
||
210 | { |
||
211 | return Slugify::create([ |
||
212 | 'regexp' => self::SLUGIFY_PATTERN, |
||
213 | ])->slugify($path); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Is current page is virtual? |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function isVirtual(): bool |
||
225 | |||
226 | /** |
||
227 | * Set page type. |
||
228 | * |
||
229 | * @param string $type |
||
230 | * |
||
231 | * @return self |
||
232 | */ |
||
233 | public function setType(string $type): self |
||
239 | |||
240 | /** |
||
241 | * Get page type. |
||
242 | * |
||
243 | * @return string|null |
||
244 | */ |
||
245 | public function getType(): ?string |
||
249 | |||
250 | /** |
||
251 | * Set name. |
||
252 | * |
||
253 | * @param string $name |
||
254 | * |
||
255 | * @return self |
||
256 | */ |
||
257 | public function setName(string $name): self |
||
258 | { |
||
259 | $this->name = $name; |
||
260 | |||
261 | return $this; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Get name. |
||
266 | * |
||
267 | * @return string|null |
||
268 | */ |
||
269 | public function getName(): ?string |
||
270 | { |
||
271 | return $this->name; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Set path. |
||
276 | * |
||
277 | * @param $path |
||
278 | * |
||
279 | * @return self |
||
280 | */ |
||
281 | public function setPath(string $path): self |
||
282 | { |
||
283 | $this->path = $path; |
||
284 | |||
285 | return $this; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Get path. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getPath(): string |
||
294 | { |
||
295 | return $this->path; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Set path name. |
||
300 | * |
||
301 | * @param string $pathname |
||
302 | * |
||
303 | * @return self |
||
304 | */ |
||
305 | public function setPathname(string $pathname): self |
||
306 | { |
||
307 | $this->pathname = $pathname; |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Get path name. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getPathname(): string |
||
318 | { |
||
319 | if ($this->hasVariable('url') |
||
320 | && $this->pathname != $this->getVariable('url') |
||
321 | ) { |
||
322 | $this->setPathname($this->getVariable('url')); |
||
323 | } |
||
324 | |||
325 | return $this->pathname; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Set section. |
||
330 | * |
||
331 | * @param string $section |
||
332 | * |
||
333 | * @return self |
||
334 | */ |
||
335 | public function setSection(string $section): self |
||
336 | { |
||
337 | $this->section = $section; |
||
338 | |||
339 | return $this; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Get section. |
||
344 | * |
||
345 | * @return string|false |
||
346 | */ |
||
347 | public function getSection(): ?string |
||
348 | { |
||
349 | if (empty($this->section) && !empty($this->path)) { |
||
350 | $this->setSection(explode('/', $this->path)[0]); |
||
351 | } |
||
352 | |||
353 | return $this->section; |
||
354 | } |
||
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 | * Return output file. |
||
392 | * |
||
393 | * Use cases: |
||
394 | * - default: pathname + suffix + extension (ie: blog/post-1/index.html) |
||
395 | * - subpath: pathname + subpath + suffix + extension (ie: blog/post-1/amp/index.html) |
||
396 | * - ugly: pathname + extension (ie: 404.html, sitemap.xml, robots.txt) |
||
397 | * - pathname only (ie: _redirects) |
||
398 | * |
||
399 | * @param string $format |
||
400 | * @param Config $config |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getOutputFile(string $format, Config $config = null): string |
||
438 | |||
439 | /** |
||
440 | * Return URL. |
||
441 | * |
||
442 | * @param string $format |
||
443 | * @param Config $config |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | public function getUrl(string $format = 'html', Config $config = null): string |
||
457 | |||
458 | /* |
||
459 | * Helper to set and get variables. |
||
460 | */ |
||
461 | |||
462 | /** |
||
463 | * Set an array as variables. |
||
464 | * |
||
465 | * @param array $variables |
||
466 | * |
||
467 | * @throws \Exception |
||
468 | * |
||
469 | * @return $this |
||
470 | */ |
||
471 | public function setVariables($variables) |
||
482 | |||
483 | /** |
||
484 | * Get all variables. |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | public function getVariables(): array |
||
492 | |||
493 | /** |
||
494 | * Set a variable. |
||
495 | * |
||
496 | * @param $name |
||
497 | * @param $value |
||
498 | * |
||
499 | * @throws \Exception |
||
500 | * |
||
501 | * @return $this |
||
502 | */ |
||
503 | public function setVariable($name, $value) |
||
543 | |||
544 | /** |
||
545 | * Is variable exist? |
||
546 | * |
||
547 | * @param $name |
||
548 | * |
||
549 | * @return bool |
||
550 | */ |
||
551 | public function hasVariable($name) |
||
555 | |||
556 | /** |
||
557 | * Get a variable. |
||
558 | * |
||
559 | * @param string $name |
||
560 | * |
||
561 | * @return mixed|null |
||
562 | */ |
||
563 | public function getVariable($name) |
||
569 | |||
570 | /** |
||
571 | * Unset a variable. |
||
572 | * |
||
573 | * @param $name |
||
574 | * |
||
575 | * @return $this |
||
576 | */ |
||
577 | public function unVariable($name) |
||
585 | } |
||
586 |
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..