| Total Complexity | 73 |
| Total Lines | 567 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Extension 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 Extension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Extension extends SlugifyExtension |
||
| 32 | { |
||
| 33 | /** @var Builder */ |
||
| 34 | protected $builder; |
||
| 35 | /** @var Config */ |
||
| 36 | protected $config; |
||
| 37 | /** @var Slugify */ |
||
| 38 | private static $slugifier; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param Builder $builder |
||
| 42 | */ |
||
| 43 | public function __construct(Builder $builder) |
||
| 44 | { |
||
| 45 | if (!self::$slugifier instanceof Slugify) { |
||
| 46 | self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); |
||
| 47 | } |
||
| 48 | |||
| 49 | parent::__construct(self::$slugifier); |
||
| 50 | |||
| 51 | $this->builder = $builder; |
||
| 52 | $this->config = $this->builder->getConfig(); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | public function getName() |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | public function getFilters() |
||
| 67 | { |
||
| 68 | return [ |
||
| 69 | new \Twig\TwigFilter('filter_by', [$this, 'filterBy']), |
||
| 70 | // sort |
||
| 71 | new \Twig\TwigFilter('sort_by_title', [$this, 'sortByTitle']), |
||
| 72 | new \Twig\TwigFilter('sort_by_weight', [$this, 'sortByWeight']), |
||
| 73 | new \Twig\TwigFilter('sort_by_date', [$this, 'sortByDate']), |
||
| 74 | // assets |
||
| 75 | new \Twig\TwigFilter('url', [$this, 'createUrl']), |
||
| 76 | new \Twig\TwigFilter('minify', [$this, 'minify']), |
||
| 77 | new \Twig\TwigFilter('to_css', [$this, 'toCss']), |
||
| 78 | new \Twig\TwigFilter('html', [$this, 'createHtmlElement']), |
||
| 79 | new \Twig\TwigFilter('inline', [$this, 'getContent']), |
||
| 80 | new \Twig\TwigFilter('minify_css', [$this, 'minifyCss']), |
||
| 81 | new \Twig\TwigFilter('minify_js', [$this, 'minifyJs']), |
||
| 82 | new \Twig\TwigFilter('scss_to_css', [$this, 'scssToCss']), |
||
| 83 | new \Twig\TwigFilter('sass_to_css', [$this, 'scssToCss']), |
||
| 84 | new \Twig\TwigFilter('resize', [$this, 'resize']), |
||
| 85 | // content |
||
| 86 | new \Twig\TwigFilter('slugify', [$this, 'slugifyFilter']), |
||
| 87 | new \Twig\TwigFilter('excerpt', [$this, 'excerpt']), |
||
| 88 | new \Twig\TwigFilter('excerpt_html', [$this, 'excerptHtml']), |
||
| 89 | // deprecated |
||
| 90 | new \Twig\TwigFilter('filterBy', [$this, 'filterBy'], ['deprecated' => true, 'alternative' => 'filter_by']), |
||
| 91 | new \Twig\TwigFilter('sortByTitle', [$this, 'sortByTitle'], ['deprecated' => true, 'alternative' => 'sort_by_title']), |
||
| 92 | new \Twig\TwigFilter('sortByWeight', [$this, 'sortByWeight'], ['deprecated' => true, 'alternative' => 'sort_by_weight']), |
||
| 93 | new \Twig\TwigFilter('sortByDate', [$this, 'sortByDate'], ['deprecated' => true, 'alternative' => 'sort_by_date']), |
||
| 94 | new \Twig\TwigFilter('minifyCSS', [$this, 'minifyCss'], ['deprecated' => true, 'alternative' => 'minifyCss']), |
||
| 95 | new \Twig\TwigFilter('minifyJS', [$this, 'minifyJs'], ['deprecated' => true, 'alternative' => 'minifyJs']), |
||
| 96 | new \Twig\TwigFilter('SCSStoCSS', [$this, 'scssToCss'], ['deprecated' => true, 'alternative' => 'scss_to_css']), |
||
| 97 | new \Twig\TwigFilter('excerptHtml', [$this, 'excerptHtml'], ['deprecated' => true, 'alternative' => 'excerpt_html']), |
||
| 98 | new \Twig\TwigFilter('urlize', [$this, 'slugifyFilter'], ['deprecated' => true, 'alternative' => 'slugify']), |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function getFunctions() |
||
| 106 | { |
||
| 107 | return [ |
||
| 108 | // assets |
||
| 109 | new \Twig\TwigFunction('url', [$this, 'createUrl']), |
||
| 110 | new \Twig\TwigFunction('asset', [$this, 'asset']), |
||
| 111 | new \Twig\TwigFunction('hash', [$this, 'hashFile']), |
||
| 112 | // content |
||
| 113 | new \Twig\TwigFunction('readtime', [$this, 'readtime']), |
||
| 114 | // others |
||
| 115 | new \Twig\TwigFunction('getenv', [$this, 'getEnv']), |
||
| 116 | // deprecated |
||
| 117 | new \Twig\TwigFunction('minify', [$this, 'minify'], ['deprecated' => true, 'alternative' => 'minify filter']), |
||
| 118 | new \Twig\TwigFunction('toCSS', [$this, 'toCss'], ['deprecated' => true, 'alternative' => 'to_css filter']), |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Filters by Section. |
||
| 124 | * |
||
| 125 | * Alias of `filterBy('section', $value)`. |
||
| 126 | * |
||
| 127 | * @param PagesCollection $pages |
||
| 128 | * @param string $section |
||
| 129 | * |
||
| 130 | * @return CollectionInterface |
||
| 131 | */ |
||
| 132 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 133 | { |
||
| 134 | return $this->filterBy($pages, 'section', $section); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Filters by variable's name/value. |
||
| 139 | * |
||
| 140 | * @param PagesCollection $pages |
||
| 141 | * @param string $variable |
||
| 142 | * @param string $value |
||
| 143 | * |
||
| 144 | * @return CollectionInterface |
||
| 145 | */ |
||
| 146 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 147 | { |
||
| 148 | $filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { |
||
| 149 | $notVirtual = false; |
||
| 150 | if (!$page->isVirtual()) { |
||
| 151 | $notVirtual = true; |
||
| 152 | } |
||
| 153 | // is a dedicated getter exists? |
||
| 154 | $method = 'get'.ucfirst($variable); |
||
| 155 | if (method_exists($page, $method) && $page->$method() == $value) { |
||
| 156 | return $notVirtual && true; |
||
| 157 | } |
||
| 158 | if ($page->getVariable($variable) == $value) { |
||
| 159 | return $notVirtual && true; |
||
| 160 | } |
||
| 161 | }); |
||
| 162 | |||
| 163 | return $filteredPages; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Sorts by title. |
||
| 168 | * |
||
| 169 | * @param \Traversable $collection |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public function sortByTitle(\Traversable $collection): array |
||
| 174 | { |
||
| 175 | $collection = iterator_to_array($collection); |
||
| 176 | array_multisort(array_keys($collection), SORT_NATURAL | SORT_FLAG_CASE, $collection); |
||
|
1 ignored issue
–
show
|
|||
| 177 | |||
| 178 | return $collection; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sorts by weight. |
||
| 183 | * |
||
| 184 | * @param \Traversable $collection |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public function sortByWeight(\Traversable $collection): array |
||
| 189 | { |
||
| 190 | $callback = function ($a, $b) { |
||
| 191 | if (!isset($a['weight'])) { |
||
| 192 | return 1; |
||
| 193 | } |
||
| 194 | if (!isset($b['weight'])) { |
||
| 195 | return -1; |
||
| 196 | } |
||
| 197 | if ($a['weight'] == $b['weight']) { |
||
| 198 | return 0; |
||
| 199 | } |
||
| 200 | |||
| 201 | return ($a['weight'] < $b['weight']) ? -1 : 1; |
||
| 202 | }; |
||
| 203 | |||
| 204 | $collection = iterator_to_array($collection); |
||
| 205 | usort($collection, $callback); |
||
| 206 | |||
| 207 | return $collection; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Sorts by date. |
||
| 212 | * |
||
| 213 | * @param \Traversable $collection |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | public function sortByDate(\Traversable $collection): array |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Creates an URL. |
||
| 241 | * |
||
| 242 | * $options[ |
||
| 243 | * 'canonical' => true, |
||
| 244 | * 'addhash' => false, |
||
| 245 | * 'format' => 'json', |
||
| 246 | * ]; |
||
| 247 | * |
||
| 248 | * @param Page|Asset|string|null $value |
||
| 249 | * @param array|null $options |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | */ |
||
| 253 | public function createUrl($value = null, $options = null) |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Manages assets (CSS, JS and images). |
||
| 346 | * |
||
| 347 | * @param string $path File path (relative from static/ dir). |
||
| 348 | * @param array|null $options |
||
| 349 | * |
||
| 350 | * @return Asset |
||
| 351 | */ |
||
| 352 | public function asset(string $path, array $options = null): Asset |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Minifying an asset (CSS or JS). |
||
| 359 | * ie: minify('css/style.css'). |
||
| 360 | * |
||
| 361 | * @param string|Asset $asset |
||
| 362 | * |
||
| 363 | * @return Asset |
||
| 364 | */ |
||
| 365 | public function minify($asset): Asset |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Compiles a SCSS asset. |
||
| 376 | * |
||
| 377 | * @param string|Asset $asset |
||
| 378 | * |
||
| 379 | * @return Asset |
||
| 380 | */ |
||
| 381 | public function toCss($asset): Asset |
||
| 382 | { |
||
| 383 | if (!$asset instanceof Asset) { |
||
| 384 | $asset = new Asset($this->builder, $asset); |
||
| 385 | } |
||
| 386 | |||
| 387 | return $asset->compile(); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Resizes an image. |
||
| 392 | * |
||
| 393 | * @param string $path Image path (relative from static/ dir or external). |
||
| 394 | * @param int $size Image new size (width). |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function resize(string $path, int $size): string |
||
| 399 | { |
||
| 400 | return (new Image($this->builder))->resize($path, $size); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Hashing an asset with sha384. |
||
| 405 | * Useful for SRI (Subresource Integrity). |
||
| 406 | * |
||
| 407 | * @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity |
||
| 408 | * |
||
| 409 | * @param string|Asset $path |
||
| 410 | * |
||
| 411 | * @return string|null |
||
| 412 | */ |
||
| 413 | public function hashFile($asset): ?string |
||
| 414 | { |
||
| 415 | if (!$asset instanceof Asset) { |
||
| 416 | $asset = new Asset($this->builder, $asset); |
||
| 417 | } |
||
| 418 | |||
| 419 | return sprintf('sha384-%s', base64_encode(hash('sha384', $asset['content'], true))); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Minifying a CSS string. |
||
| 424 | * |
||
| 425 | * @param string $value |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function minifyCss(string $value): string |
||
| 430 | { |
||
| 431 | $cache = new Cache($this->builder, 'assets'); |
||
| 432 | $cacheKey = $cache->createKeyFromValue($value); |
||
| 433 | if (!$cache->has($cacheKey)) { |
||
| 434 | $minifier = new Minify\CSS($value); |
||
| 435 | $value = $minifier->minify(); |
||
| 436 | $cache->set($cacheKey, $value); |
||
| 437 | } |
||
| 438 | |||
| 439 | return $cache->get($cacheKey, $value); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Minifying a JavaScript string. |
||
| 444 | * |
||
| 445 | * @param string $value |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function minifyJs(string $value): string |
||
| 450 | { |
||
| 451 | $cache = new Cache($this->builder, 'assets'); |
||
| 452 | $cacheKey = $cache->createKeyFromValue($value); |
||
| 453 | if (!$cache->has($cacheKey)) { |
||
| 454 | $minifier = new Minify\JS($value); |
||
| 455 | $value = $minifier->minify(); |
||
| 456 | $cache->set($cacheKey, $value); |
||
| 457 | } |
||
| 458 | |||
| 459 | return $cache->get($cacheKey, $value); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Compiles a SCSS string. |
||
| 464 | * |
||
| 465 | * @param string $value |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function scssToCss(string $value): string |
||
| 470 | { |
||
| 471 | $cache = new Cache($this->builder, 'assets'); |
||
| 472 | $cacheKey = $cache->createKeyFromValue($value); |
||
| 473 | if (!$cache->has($cacheKey)) { |
||
| 474 | $scss = new Compiler(); |
||
| 475 | $value = $scss->compile($value); |
||
| 476 | $cache->set($cacheKey, $value); |
||
| 477 | } |
||
| 478 | |||
| 479 | return $cache->get($cacheKey, $value); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Creates an HTML element from an asset. |
||
| 484 | * |
||
| 485 | * @param Asset $asset |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function createHtmlElement(Asset $asset): string |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns the content of an Asset. |
||
| 516 | * |
||
| 517 | * @param Asset $asset |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | public function getContent(Asset $asset): string |
||
| 522 | { |
||
| 523 | if (is_null($asset['content'])) { |
||
| 524 | throw new Exception(\sprintf('%s is available with CSS et JS files only.', '"inline" filter')); |
||
| 525 | } |
||
| 526 | |||
| 527 | return $asset['content']; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Reads $length first characters of a string and adds a suffix. |
||
| 532 | * |
||
| 533 | * @param string|null $string |
||
| 534 | * @param int $length |
||
| 535 | * @param string $suffix |
||
| 536 | * |
||
| 537 | * @return string|null |
||
| 538 | */ |
||
| 539 | public function excerpt(string $string = null, int $length = 450, string $suffix = ' …'): ?string |
||
| 540 | { |
||
| 541 | $string = str_replace('</p>', '<br /><br />', $string); |
||
| 542 | $string = trim(strip_tags($string, '<br>'), '<br />'); |
||
| 543 | if (mb_strlen($string) > $length) { |
||
| 544 | $string = mb_substr($string, 0, $length); |
||
| 545 | $string .= $suffix; |
||
| 546 | } |
||
| 547 | |||
| 548 | return $string; |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Reads characters before '<!-- excerpt|break -->'. |
||
| 553 | * |
||
| 554 | * @param string|null $string |
||
| 555 | * |
||
| 556 | * @return string|null |
||
| 557 | */ |
||
| 558 | public function excerptHtml(string $string = null): ?string |
||
| 559 | { |
||
| 560 | // https://regex101.com/r/Xl7d5I/3 |
||
| 561 | $pattern = '(.*)(<!--[[:blank:]]?(excerpt|break)[[:blank:]]?-->)(.*)'; |
||
| 562 | preg_match('/'.$pattern.'/is', $string, $matches); |
||
| 563 | if (empty($matches)) { |
||
| 564 | return $string; |
||
| 565 | } |
||
| 566 | |||
| 567 | return trim($matches[1]); |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Calculates estimated time to read a text. |
||
| 572 | * |
||
| 573 | * @param string|null $text |
||
| 574 | * |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | public function readtime(string $text = null): string |
||
| 578 | { |
||
| 579 | $words = str_word_count(strip_tags($text)); |
||
| 580 | $min = floor($words / 200); |
||
| 581 | if ($min === 0) { |
||
| 582 | return '1'; |
||
| 583 | } |
||
| 584 | |||
| 585 | return (string) $min; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Gets the value of an environment variable. |
||
| 590 | * |
||
| 591 | * @param string $var |
||
| 592 | * |
||
| 593 | * @return string|null |
||
| 594 | */ |
||
| 595 | public function getEnv(string $var): ?string |
||
| 598 | } |
||
| 599 | } |
||
| 600 |