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