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