Total Complexity | 93 |
Total Lines | 679 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
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.
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 |
||
28 | class Page extends Item |
||
29 | { |
||
30 | public const SLUGIFY_PATTERN = '/(^\/|[^._a-z0-9\/]|-)+/'; // should be '/^\/|[^_a-z0-9\/]+/' |
||
31 | |||
32 | /** @var bool True if page is not created from a file. */ |
||
33 | protected $virtual; |
||
34 | |||
35 | /** @var SplFileInfo */ |
||
36 | protected $file; |
||
37 | |||
38 | /** @var Type Type */ |
||
39 | protected $type; |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $folder; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $slug; |
||
46 | |||
47 | /** @var string path = folder + slug. */ |
||
48 | protected $path; |
||
49 | |||
50 | /** @var string */ |
||
51 | protected $section; |
||
52 | |||
53 | /** @var string */ |
||
54 | protected $frontmatter; |
||
55 | |||
56 | /** @var array Front matter before conversion. */ |
||
57 | protected $fmVariables = []; |
||
58 | |||
59 | /** @var string Body before conversion. */ |
||
60 | protected $body; |
||
61 | |||
62 | /** @var string Body after conversion. */ |
||
63 | protected $html; |
||
64 | |||
65 | /** @var array Output, by format */ |
||
66 | protected $rendered = []; |
||
67 | |||
68 | /** @var Collection Subpages of a list page. */ |
||
69 | protected $subPages; |
||
70 | |||
71 | /** @var array */ |
||
72 | protected $paginator = []; |
||
73 | |||
74 | /** @var \Cecil\Collection\Taxonomy\Vocabulary Terms of a vocabulary. */ |
||
75 | protected $terms; |
||
76 | |||
77 | /** @var Slugify */ |
||
78 | private static $slugifier; |
||
79 | |||
80 | public function __construct(string $id) |
||
81 | { |
||
82 | parent::__construct($id); |
||
83 | $this->setVirtual(true); |
||
84 | $this->setType(Type::PAGE->value); |
||
85 | // default variables |
||
86 | $this->setVariables([ |
||
87 | 'title' => 'Page Title', |
||
88 | 'date' => new \DateTime(), |
||
89 | 'updated' => new \DateTime(), |
||
90 | 'weight' => null, |
||
91 | 'filepath' => null, |
||
92 | 'published' => true, |
||
93 | 'content_template' => 'page.content.twig', |
||
94 | ]); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * toString magic method to prevent Twig get_attribute fatal error. |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function __toString() |
||
103 | { |
||
104 | return $this->getId(); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Turns a path (string) into a slug (URI). |
||
109 | */ |
||
110 | public static function slugify(string $path): string |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Creates the ID from the file (path). |
||
121 | */ |
||
122 | public static function createIdFromFile(SplFileInfo $file): string |
||
123 | { |
||
124 | $relativePath = self::slugify(str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath())); |
||
125 | $basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension()))); |
||
126 | // if file is "README.md", ID is "index" |
||
127 | $basename = (string) str_ireplace('readme', 'index', $basename); |
||
128 | // if file is section's index: "section/index.md", ID is "section" |
||
129 | if (!empty($relativePath) && PrefixSuffix::sub($basename) == 'index') { |
||
130 | // case of a localized section's index: "section/index.fr.md", ID is "fr/section" |
||
131 | if (PrefixSuffix::hasSuffix($basename)) { |
||
132 | return PrefixSuffix::getSuffix($basename) . '/' . $relativePath; |
||
133 | } |
||
134 | |||
135 | return $relativePath; |
||
136 | } |
||
137 | // localized page |
||
138 | if (PrefixSuffix::hasSuffix($basename)) { |
||
139 | return trim(Util::joinPath(PrefixSuffix::getSuffix($basename), $relativePath, PrefixSuffix::sub($basename)), '/'); |
||
140 | } |
||
141 | |||
142 | return trim(Util::joinPath($relativePath, $basename), '/'); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns the ID of a page without language. |
||
147 | */ |
||
148 | public function getIdWithoutLang(): string |
||
149 | { |
||
150 | $langPrefix = $this->getVariable('language') . '/'; |
||
151 | if ($this->hasVariable('language') && Util\Str::startsWith($this->getId(), $langPrefix)) { |
||
152 | return substr($this->getId(), \strlen($langPrefix)); |
||
153 | } |
||
154 | |||
155 | return $this->getId(); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Set file. |
||
160 | */ |
||
161 | public function setFile(SplFileInfo $file): self |
||
162 | { |
||
163 | $this->file = $file; |
||
164 | $this->setVirtual(false); |
||
165 | |||
166 | /* |
||
167 | * File path components |
||
168 | */ |
||
169 | $fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); |
||
170 | $fileExtension = $this->file->getExtension(); |
||
171 | $fileName = $this->file->getBasename('.' . $fileExtension); |
||
172 | // renames "README" to "index" |
||
173 | $fileName = (string) str_ireplace('readme', 'index', $fileName); |
||
174 | // case of "index" = home page |
||
175 | if (empty($this->file->getRelativePath()) && PrefixSuffix::sub($fileName) == 'index') { |
||
176 | $this->setType(Type::HOMEPAGE->value); |
||
177 | } |
||
178 | /* |
||
179 | * Set page properties and variables |
||
180 | */ |
||
181 | $this->setFolder($fileRelativePath); |
||
182 | $this->setSlug($fileName); |
||
183 | $this->setPath($this->getFolder() . '/' . $this->getSlug()); |
||
184 | $this->setVariables([ |
||
185 | 'title' => PrefixSuffix::sub($fileName), |
||
186 | 'date' => (new \DateTime())->setTimestamp($this->file->getMTime()), |
||
187 | 'updated' => (new \DateTime())->setTimestamp($this->file->getMTime()), |
||
188 | 'filepath' => $this->file->getRelativePathname(), |
||
189 | ]); |
||
190 | /* |
||
191 | * Set specific variables |
||
192 | */ |
||
193 | // is file has a prefix? |
||
194 | if (PrefixSuffix::hasPrefix($fileName)) { |
||
195 | $prefix = PrefixSuffix::getPrefix($fileName); |
||
196 | if ($prefix !== null) { |
||
197 | // prefix is an integer: used for sorting |
||
198 | if (is_numeric($prefix)) { |
||
199 | $this->setVariable('weight', (int) $prefix); |
||
200 | } |
||
201 | // prefix is a valid date? |
||
202 | if (Util\Date::isValid($prefix)) { |
||
203 | $this->setVariable('date', (string) $prefix); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | // is file has a language suffix? |
||
208 | if (PrefixSuffix::hasSuffix($fileName)) { |
||
209 | $this->setVariable('language', PrefixSuffix::getSuffix($fileName)); |
||
210 | } |
||
211 | // set reference between page's translations, even if it exist in only one language |
||
212 | $this->setVariable('langref', $this->getPath()); |
||
213 | |||
214 | return $this; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Returns file name, with extension. |
||
219 | */ |
||
220 | public function getFileName(): ?string |
||
221 | { |
||
222 | if ($this->file === null) { |
||
223 | return null; |
||
224 | } |
||
225 | |||
226 | return $this->file->getBasename(); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Returns file real path. |
||
231 | */ |
||
232 | public function getFilePath(): ?string |
||
233 | { |
||
234 | if ($this->file === null) { |
||
235 | return null; |
||
236 | } |
||
237 | |||
238 | return $this->file->getRealPath() === false ? null : $this->file->getRealPath(); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Parse file content. |
||
243 | */ |
||
244 | public function parse(): self |
||
245 | { |
||
246 | $parser = new Parser($this->file); |
||
247 | $parsed = $parser->parse(); |
||
248 | $this->frontmatter = $parsed->getFrontmatter(); |
||
249 | $this->body = $parsed->getBody(); |
||
250 | |||
251 | return $this; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Get front matter. |
||
256 | */ |
||
257 | public function getFrontmatter(): ?string |
||
258 | { |
||
259 | return $this->frontmatter; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Get body as raw. |
||
264 | */ |
||
265 | public function getBody(): ?string |
||
266 | { |
||
267 | return $this->body; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Set virtual status. |
||
272 | */ |
||
273 | public function setVirtual(bool $virtual): self |
||
274 | { |
||
275 | $this->virtual = $virtual; |
||
276 | |||
277 | return $this; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Is current page is virtual? |
||
282 | */ |
||
283 | public function isVirtual(): bool |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Set page type. |
||
290 | */ |
||
291 | public function setType(string $type): self |
||
292 | { |
||
293 | $this->type = Type::from($type); |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get page type. |
||
300 | */ |
||
301 | public function getType(): string |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Set path without slug. |
||
308 | */ |
||
309 | public function setFolder(string $folder): self |
||
310 | { |
||
311 | $this->folder = self::slugify($folder); |
||
312 | |||
313 | return $this; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Get path without slug. |
||
318 | */ |
||
319 | public function getFolder(): ?string |
||
320 | { |
||
321 | return $this->folder; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Set slug. |
||
326 | */ |
||
327 | public function setSlug(string $slug): self |
||
328 | { |
||
329 | if (!$this->slug) { |
||
330 | $slug = self::slugify(PrefixSuffix::sub($slug)); |
||
331 | } |
||
332 | // force slug and update path |
||
333 | if ($this->slug && $this->slug != $slug) { |
||
334 | $this->setPath($this->getFolder() . '/' . $slug); |
||
335 | } |
||
336 | $this->slug = $slug; |
||
337 | |||
338 | return $this; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Get slug. |
||
343 | */ |
||
344 | public function getSlug(): string |
||
345 | { |
||
346 | return $this->slug; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Set path. |
||
351 | */ |
||
352 | public function setPath(string $path): self |
||
353 | { |
||
354 | $path = trim($path, '/'); |
||
355 | |||
356 | // case of homepage |
||
357 | if ($path == 'index') { |
||
358 | $this->path = ''; |
||
359 | |||
360 | return $this; |
||
361 | } |
||
362 | |||
363 | // case of custom sections' index (ie: section/index.md -> section) |
||
364 | if (substr($path, -6) == '/index') { |
||
365 | $path = substr($path, 0, \strlen($path) - 6); |
||
366 | } |
||
367 | $this->path = $path; |
||
368 | |||
369 | $lastslash = strrpos($this->path, '/'); |
||
370 | |||
371 | // case of root/top-level pages |
||
372 | if ($lastslash === false) { |
||
373 | $this->slug = $this->path; |
||
374 | |||
375 | return $this; |
||
376 | } |
||
377 | |||
378 | // case of sections' pages: set section |
||
379 | if (!$this->virtual && $this->getSection() === null) { |
||
380 | $this->section = explode('/', $this->path)[0]; |
||
381 | } |
||
382 | // set/update folder and slug |
||
383 | $this->folder = substr($this->path, 0, $lastslash); |
||
384 | $this->slug = substr($this->path, -(\strlen($this->path) - $lastslash - 1)); |
||
385 | |||
386 | return $this; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Get path. |
||
391 | */ |
||
392 | public function getPath(): ?string |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @see getPath() |
||
399 | */ |
||
400 | public function getPathname(): ?string |
||
401 | { |
||
402 | return $this->getPath(); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Set section. |
||
407 | */ |
||
408 | public function setSection(string $section): self |
||
409 | { |
||
410 | $this->section = $section; |
||
411 | |||
412 | return $this; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Get section. |
||
417 | */ |
||
418 | public function getSection(): ?string |
||
419 | { |
||
420 | return !empty($this->section) ? $this->section : null; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Unset section. |
||
425 | */ |
||
426 | public function unSection(): self |
||
427 | { |
||
428 | $this->section = null; |
||
429 | |||
430 | return $this; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Set body as HTML. |
||
435 | */ |
||
436 | public function setBodyHtml(string $html): self |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Get body as HTML. |
||
445 | */ |
||
446 | public function getBodyHtml(): ?string |
||
447 | { |
||
448 | return $this->html; |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * @see getBodyHtml() |
||
453 | */ |
||
454 | public function getContent(): ?string |
||
455 | { |
||
456 | return $this->getBodyHtml(); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Add rendered. |
||
461 | */ |
||
462 | public function addRendered(array $rendered): self |
||
463 | { |
||
464 | $this->rendered += $rendered; |
||
465 | |||
466 | return $this; |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Get rendered. |
||
471 | */ |
||
472 | public function getRendered(): array |
||
473 | { |
||
474 | return $this->rendered; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Set Subpages. |
||
479 | */ |
||
480 | public function setPages(\Cecil\Collection\Page\Collection $subPages): self |
||
481 | { |
||
482 | $this->subPages = $subPages; |
||
483 | |||
484 | return $this; |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Get Subpages. |
||
489 | */ |
||
490 | public function getPages(): ?\Cecil\Collection\Page\Collection |
||
491 | { |
||
492 | return $this->subPages; |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Set paginator. |
||
497 | */ |
||
498 | public function setPaginator(array $paginator): self |
||
499 | { |
||
500 | $this->paginator = $paginator; |
||
501 | |||
502 | return $this; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Get paginator. |
||
507 | */ |
||
508 | public function getPaginator(): array |
||
509 | { |
||
510 | return $this->paginator; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Paginator backward compatibility. |
||
515 | */ |
||
516 | public function getPagination(): array |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Set vocabulary terms. |
||
523 | */ |
||
524 | public function setTerms(\Cecil\Collection\Taxonomy\Vocabulary $terms): self |
||
525 | { |
||
526 | $this->terms = $terms; |
||
527 | |||
528 | return $this; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Get vocabulary terms. |
||
533 | */ |
||
534 | public function getTerms(): \Cecil\Collection\Taxonomy\Vocabulary |
||
535 | { |
||
536 | return $this->terms; |
||
537 | } |
||
538 | |||
539 | /* |
||
540 | * Helpers to set and get variables. |
||
541 | */ |
||
542 | |||
543 | /** |
||
544 | * Set an array as variables. |
||
545 | * |
||
546 | * @throws RuntimeException |
||
547 | */ |
||
548 | public function setVariables(array $variables): self |
||
549 | { |
||
550 | foreach ($variables as $key => $value) { |
||
551 | $this->setVariable($key, $value); |
||
552 | } |
||
553 | |||
554 | return $this; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Get all variables. |
||
559 | */ |
||
560 | public function getVariables(): array |
||
561 | { |
||
562 | return $this->properties; |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Set a variable. |
||
567 | * |
||
568 | * @param string $name Name of the variable |
||
569 | * @param mixed $value Value of the variable |
||
570 | * |
||
571 | * @throws RuntimeException |
||
572 | */ |
||
573 | public function setVariable(string $name, $value): self |
||
574 | { |
||
575 | $this->filterBool($value); |
||
576 | switch ($name) { |
||
577 | case 'date': |
||
578 | case 'updated': |
||
579 | case 'lastmod': |
||
580 | try { |
||
581 | $date = Util\Date::toDatetime($value); |
||
582 | } catch (\Exception) { |
||
583 | throw new \Exception(\sprintf('The value of "%s" is not a valid date: "%s".', $name, var_export($value, true))); |
||
584 | } |
||
585 | $this->offsetSet($name == 'lastmod' ? 'updated' : $name, $date); |
||
586 | break; |
||
587 | |||
588 | case 'schedule': |
||
589 | /* |
||
590 | * publish: 2012-10-08 |
||
591 | * expiry: 2012-10-09 |
||
592 | */ |
||
593 | $this->offsetSet('published', false); |
||
594 | if (\is_array($value)) { |
||
595 | if (\array_key_exists('publish', $value) && Util\Date::toDatetime($value['publish']) <= Util\Date::toDatetime('now')) { |
||
596 | $this->offsetSet('published', true); |
||
597 | } |
||
598 | if (\array_key_exists('expiry', $value) && Util\Date::toDatetime($value['expiry']) >= Util\Date::toDatetime('now')) { |
||
599 | $this->offsetSet('published', true); |
||
600 | } |
||
601 | } |
||
602 | break; |
||
603 | case 'draft': |
||
604 | // draft: true = published: false |
||
605 | if ($value === true) { |
||
606 | $this->offsetSet('published', false); |
||
607 | } |
||
608 | break; |
||
609 | case 'path': |
||
610 | case 'slug': |
||
611 | $slugify = self::slugify((string) $value); |
||
612 | if ($value != $slugify) { |
||
613 | throw new RuntimeException(\sprintf('"%s" variable should be "%s" (not "%s") in "%s".', $name, $slugify, (string) $value, $this->getId())); |
||
614 | } |
||
615 | $method = 'set' . ucfirst($name); |
||
616 | $this->$method($value); |
||
617 | break; |
||
618 | default: |
||
619 | $this->offsetSet($name, $value); |
||
620 | } |
||
621 | |||
622 | return $this; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Is variable exists? |
||
627 | * |
||
628 | * @param string $name Name of the variable |
||
629 | */ |
||
630 | public function hasVariable(string $name): bool |
||
631 | { |
||
632 | return $this->offsetExists($name); |
||
633 | } |
||
634 | |||
635 | /** |
||
636 | * Get a variable. |
||
637 | * |
||
638 | * @param string $name Name of the variable |
||
639 | * @param mixed|null $default Default value |
||
640 | * |
||
641 | * @return mixed|null |
||
642 | */ |
||
643 | public function getVariable(string $name, $default = null) |
||
644 | { |
||
645 | if ($this->offsetExists($name)) { |
||
646 | return $this->offsetGet($name); |
||
647 | } |
||
648 | |||
649 | return $default; |
||
650 | } |
||
651 | |||
652 | /** |
||
653 | * Unset a variable. |
||
654 | * |
||
655 | * @param string $name Name of the variable |
||
656 | */ |
||
657 | public function unVariable(string $name): self |
||
658 | { |
||
659 | if ($this->offsetExists($name)) { |
||
660 | $this->offsetUnset($name); |
||
661 | } |
||
662 | |||
663 | return $this; |
||
664 | } |
||
665 | |||
666 | /** |
||
667 | * Set front matter (only) variables. |
||
668 | */ |
||
669 | public function setFmVariables(array $variables): self |
||
670 | { |
||
671 | $this->fmVariables = $variables; |
||
672 | |||
673 | return $this; |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * Get front matter variables. |
||
678 | */ |
||
679 | public function getFmVariables(): array |
||
680 | { |
||
681 | return $this->fmVariables; |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * Cast "boolean" string (or array of strings) to boolean. |
||
686 | * |
||
687 | * @param mixed $value Value to filter |
||
688 | * |
||
689 | * @return bool|mixed |
||
690 | * |
||
691 | * @see strToBool() |
||
692 | */ |
||
693 | private function filterBool(&$value) |
||
694 | { |
||
695 | \Cecil\Util\Str::strToBool($value); |
||
696 | if (\is_array($value)) { |
||
697 | array_walk_recursive($value, '\Cecil\Util\Str::strToBool'); |
||
698 | } |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * {@inheritdoc} |
||
703 | */ |
||
704 | public function setId(string $id): self |
||
707 | } |
||
708 | } |
||
709 |