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