| Total Complexity | 58 | 
| Total Lines | 538 | 
| 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() | ||
| 139 | ), | ||
| 140 | ]; | ||
| 141 | } | ||
| 142 | |||
| 143 | /** | ||
| 144 |      * {@inheritdoc} | ||
| 145 | */ | ||
| 146 | public function getFunctions() | ||
| 147 |     { | ||
| 148 | return [ | ||
| 149 | // assets | ||
| 150 |             new \Twig\TwigFunction('url', [$this, 'url']), | ||
| 151 |             new \Twig\TwigFunction('asset', [$this, 'asset']), | ||
| 152 |             new \Twig\TwigFunction('integrity', [$this, 'integrity']), | ||
| 153 | // content | ||
| 154 |             new \Twig\TwigFunction('readtime', [$this, 'readtime']), | ||
| 155 | // others | ||
| 156 |             new \Twig\TwigFunction('getenv', [$this, 'getEnv']), | ||
| 157 | // deprecated | ||
| 158 | new \Twig\TwigFunction( | ||
| 159 | 'minify', | ||
| 160 | [$this, 'minify'], | ||
| 161 | ['deprecated' => true, 'alternative' => 'minify filter'] | ||
| 162 | ), | ||
| 163 | new \Twig\TwigFunction( | ||
| 164 | 'toCSS', | ||
| 165 | [$this, 'toCss'], | ||
| 166 | ['deprecated' => true, 'alternative' => 'to_css filter'] | ||
| 167 | ), | ||
| 168 | new \Twig\TwigFunction( | ||
| 169 | 'hash', | ||
| 170 | [$this, 'integrity'], | ||
| 171 | ['deprecated' => true, 'alternative' => 'integrity'] | ||
| 172 | ), | ||
| 173 | ]; | ||
| 174 | } | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Filters by Section. | ||
| 178 |      * Alias of `filterBy('section', $value)`. | ||
| 179 | * | ||
| 180 | * @param PagesCollection $pages | ||
| 181 | * @param string $section | ||
| 182 | * | ||
| 183 | * @return CollectionInterface | ||
| 184 | */ | ||
| 185 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface | ||
| 186 |     { | ||
| 187 | return $this->filterBy($pages, 'section', $section); | ||
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Filters by variable's name/value. | ||
| 192 | * | ||
| 193 | * @param PagesCollection $pages | ||
| 194 | * @param string $variable | ||
| 195 | * @param string $value | ||
| 196 | * | ||
| 197 | * @return CollectionInterface | ||
| 198 | */ | ||
| 199 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface | ||
| 200 |     { | ||
| 201 |         $filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { | ||
| 202 | $notVirtual = false; | ||
| 203 |             if (!$page->isVirtual()) { | ||
| 204 | $notVirtual = true; | ||
| 205 | } | ||
| 206 | // is a dedicated getter exists? | ||
| 207 | $method = 'get'.ucfirst($variable); | ||
| 208 |             if (method_exists($page, $method) && $page->$method() == $value) { | ||
| 209 | return $notVirtual && true; | ||
| 210 | } | ||
| 211 |             if ($page->getVariable($variable) == $value) { | ||
| 212 | return $notVirtual && true; | ||
| 213 | } | ||
| 214 | }); | ||
| 215 | |||
| 216 | return $filteredPages; | ||
| 217 | } | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Sorts by title. | ||
| 221 | * | ||
| 222 | * @param \Traversable $collection | ||
| 223 | * | ||
| 224 | * @return array | ||
| 225 | */ | ||
| 226 | public function sortByTitle(\Traversable $collection): array | ||
| 227 |     { | ||
| 228 | $collection = iterator_to_array($collection); | ||
| 229 | array_multisort(array_keys($collection), SORT_NATURAL | SORT_FLAG_CASE, $collection); | ||
| 1 ignored issue–
                            show | |||
| 230 | |||
| 231 | return $collection; | ||
| 232 | } | ||
| 233 | |||
| 234 | /** | ||
| 235 | * Sorts by weight. | ||
| 236 | * | ||
| 237 | * @param \Traversable $collection | ||
| 238 | * | ||
| 239 | * @return array | ||
| 240 | */ | ||
| 241 | public function sortByWeight(\Traversable $collection): array | ||
| 242 |     { | ||
| 243 |         $callback = function ($a, $b) { | ||
| 244 |             if (!isset($a['weight'])) { | ||
| 245 | return 1; | ||
| 246 | } | ||
| 247 |             if (!isset($b['weight'])) { | ||
| 248 | return -1; | ||
| 249 | } | ||
| 250 |             if ($a['weight'] == $b['weight']) { | ||
| 251 | return 0; | ||
| 252 | } | ||
| 253 | |||
| 254 | return ($a['weight'] < $b['weight']) ? -1 : 1; | ||
| 255 | }; | ||
| 256 | |||
| 257 | $collection = iterator_to_array($collection); | ||
| 258 | usort($collection, $callback); | ||
| 259 | |||
| 260 | return $collection; | ||
| 261 | } | ||
| 262 | |||
| 263 | /** | ||
| 264 | * Sorts by date. | ||
| 265 | * | ||
| 266 | * @param \Traversable $collection | ||
| 267 | * | ||
| 268 | * @return array | ||
| 269 | */ | ||
| 270 | public function sortByDate(\Traversable $collection): array | ||
| 271 |     { | ||
| 272 |         $callback = function ($a, $b) { | ||
| 273 |             if (!isset($a['date'])) { | ||
| 274 | return -1; | ||
| 275 | } | ||
| 276 |             if (!isset($b['date'])) { | ||
| 277 | return 1; | ||
| 278 | } | ||
| 279 |             if ($a['date'] == $b['date']) { | ||
| 280 | return 0; | ||
| 281 | } | ||
| 282 | |||
| 283 | return ($a['date'] > $b['date']) ? -1 : 1; | ||
| 284 | }; | ||
| 285 | |||
| 286 | $collection = iterator_to_array($collection); | ||
| 287 | usort($collection, $callback); | ||
| 288 | |||
| 289 | return $collection; | ||
| 290 | } | ||
| 291 | |||
| 292 | /** | ||
| 293 | * Creates an URL. | ||
| 294 | * | ||
| 295 | * $options[ | ||
| 296 | * 'canonical' => true, | ||
| 297 | * 'addhash' => false, | ||
| 298 | * 'format' => 'json', | ||
| 299 | * ]; | ||
| 300 | * | ||
| 301 | * @param Page|Asset|string|null $value | ||
| 302 | * @param array|null $options | ||
| 303 | * | ||
| 304 | * @return mixed | ||
| 305 | */ | ||
| 306 | public function url($value = null, array $options = null) | ||
| 307 |     { | ||
| 308 | return new Url($this->builder, $value, $options); | ||
| 309 | } | ||
| 310 | |||
| 311 | /** | ||
| 312 | * Creates an asset (CSS, JS, images, etc.). | ||
| 313 | * | ||
| 314 | * @param string $path File path (relative from static/ dir). | ||
| 315 | * @param array|null $options | ||
| 316 | * | ||
| 317 | * @return Asset | ||
| 318 | */ | ||
| 319 | public function asset(string $path, array $options = null): Asset | ||
| 320 |     { | ||
| 321 | return new Asset($this->builder, $path, $options); | ||
| 322 | } | ||
| 323 | |||
| 324 | /** | ||
| 325 | * Minifying an asset (CSS or JS). | ||
| 326 |      * ie: minify('css/style.css'). | ||
| 327 | * | ||
| 328 | * @param string|Asset $asset | ||
| 329 | * | ||
| 330 | * @return Asset | ||
| 331 | */ | ||
| 332 | public function minify($asset): Asset | ||
| 339 | } | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Compiles a SCSS asset. | ||
| 343 | * | ||
| 344 | * @param string|Asset $asset | ||
| 345 | * | ||
| 346 | * @return Asset | ||
| 347 | */ | ||
| 348 | public function toCss($asset): Asset | ||
| 355 | } | ||
| 356 | |||
| 357 | /** | ||
| 358 | * Resizes an image. | ||
| 359 | * | ||
| 360 | * @param string $path Image path (relative from static/ dir or external). | ||
| 361 | * @param int $size Image new size (width). | ||
| 362 | * | ||
| 363 | * @return string | ||
| 364 | */ | ||
| 365 | public function resize(string $path, int $size): string | ||
| 368 | } | ||
| 369 | |||
| 370 | /** | ||
| 371 | * Hashing an asset with algo (sha384 by default). | ||
| 372 | * | ||
| 373 | * @param string|Asset $path | ||
| 374 | * @param string $algo | ||
| 375 | * | ||
| 376 | * @return string | ||
| 377 | */ | ||
| 378 | public function integrity($asset, string $algo = 'sha384'): string | ||
| 379 |     { | ||
| 380 |         if (!$asset instanceof Asset) { | ||
| 381 | $asset = new Asset($this->builder, $asset); | ||
| 382 | } | ||
| 383 | |||
| 384 | return $asset->getIntegrity($algo); | ||
| 385 | } | ||
| 386 | |||
| 387 | /** | ||
| 388 | * Minifying a CSS string. | ||
| 389 | * | ||
| 390 | * @param string $value | ||
| 391 | * | ||
| 392 | * @return string | ||
| 393 | */ | ||
| 394 | public function minifyCss(string $value): string | ||
| 395 |     { | ||
| 396 | $cache = new Cache($this->builder, 'assets'); | ||
| 397 | $cacheKey = $cache->createKeyFromValue($value); | ||
| 398 |         if (!$cache->has($cacheKey)) { | ||
| 399 | $minifier = new Minify\CSS($value); | ||
| 400 | $value = $minifier->minify(); | ||
| 401 | $cache->set($cacheKey, $value); | ||
| 402 | } | ||
| 403 | |||
| 404 | return $cache->get($cacheKey, $value); | ||
| 405 | } | ||
| 406 | |||
| 407 | /** | ||
| 408 | * Minifying a JavaScript string. | ||
| 409 | * | ||
| 410 | * @param string $value | ||
| 411 | * | ||
| 412 | * @return string | ||
| 413 | */ | ||
| 414 | public function minifyJs(string $value): string | ||
| 425 | } | ||
| 426 | |||
| 427 | /** | ||
| 428 | * Compiles a SCSS string. | ||
| 429 | * | ||
| 430 | * @param string $value | ||
| 431 | * | ||
| 432 | * @return string | ||
| 433 | */ | ||
| 434 | public function scssToCss(string $value): string | ||
| 435 |     { | ||
| 436 | $cache = new Cache($this->builder, 'assets'); | ||
| 437 | $cacheKey = $cache->createKeyFromValue($value); | ||
| 438 |         if (!$cache->has($cacheKey)) { | ||
| 439 | $scssPhp = new Compiler(); | ||
| 440 |             $formatter = \sprintf('ScssPhp\ScssPhp\Formatter\%s', ucfirst((string) $this->config->get('assets.compile.style'))); | ||
| 441 |             if (!class_exists($formatter)) { | ||
| 442 |                 throw new Exception(\sprintf('Scss formatter "%s" doesn\'t exists.', $formatter)); | ||
| 443 | } | ||
| 444 | $scssPhp->setFormatter($formatter); | ||
| 445 |             $scssPhp->setVariables($this->config->get('assets.compile.variables') ?? []); | ||
| 446 | $value = $scssPhp->compile($value); | ||
| 447 | $cache->set($cacheKey, $value); | ||
| 448 | } | ||
| 449 | |||
| 450 | return $cache->get($cacheKey, $value); | ||
| 451 | } | ||
| 452 | |||
| 453 | /** | ||
| 454 | * Creates an HTML element from an asset. | ||
| 455 | * | ||
| 456 | * @param Asset $asset | ||
| 457 | * | ||
| 458 | * @return string | ||
| 459 | */ | ||
| 460 | public function html(Asset $asset): string | ||
| 461 |     { | ||
| 462 |         if ($asset['type'] == 'image') { | ||
| 463 | $attributes = $asset['attributes'] ?? []; | ||
| 464 |             $title = array_key_exists('title', $attributes) ? $attributes['title'] : null; | ||
| 465 |             $alt = array_key_exists('alt', $attributes) ? $attributes['alt'] : null; | ||
| 466 | |||
| 467 | return \sprintf( | ||
| 468 | '<img src="%s"%s%s>', | ||
| 469 | $asset['path'], | ||
| 470 |                 !is_null($title) ? \sprintf(' title="%s"', $title) : '', | ||
| 471 |                 !is_null($alt) ? \sprintf(' alt="%s"', $alt) : '' | ||
| 472 | ); | ||
| 473 | } | ||
| 474 | |||
| 475 |         switch ($asset['ext']) { | ||
| 476 | case 'css': | ||
| 477 |                 return \sprintf('<link rel="stylesheet" href="%s">', $asset['path']); | ||
| 478 | case 'js': | ||
| 479 |                 return \sprintf('<script src="%s"></script>', $asset['path']); | ||
| 480 | } | ||
| 481 | |||
| 482 |         throw new Exception(\sprintf('%s is available with CSS, JS and images files only.', '"html" filter')); | ||
| 483 | } | ||
| 484 | |||
| 485 | /** | ||
| 486 | * Returns the content of an Asset. | ||
| 487 | * | ||
| 488 | * @param Asset $asset | ||
| 489 | * | ||
| 490 | * @return string | ||
| 491 | */ | ||
| 492 | public function inline(Asset $asset): string | ||
| 499 | } | ||
| 500 | |||
| 501 | /** | ||
| 502 | * Reads $length first characters of a string and adds a suffix. | ||
| 503 | * | ||
| 504 | * @param string|null $string | ||
| 505 | * @param int $length | ||
| 506 | * @param string $suffix | ||
| 507 | * | ||
| 508 | * @return string|null | ||
| 509 | */ | ||
| 510 | public function excerpt(string $string = null, int $length = 450, string $suffix = ' …'): ?string | ||
| 511 |     { | ||
| 512 |         $string = str_replace('</p>', '<br /><br />', $string); | ||
| 513 | $string = trim(strip_tags($string, '<br>'), '<br />'); | ||
| 514 |         if (mb_strlen($string) > $length) { | ||
| 515 | $string = mb_substr($string, 0, $length); | ||
| 516 | $string .= $suffix; | ||
| 517 | } | ||
| 518 | |||
| 519 | return $string; | ||
| 520 | } | ||
| 521 | |||
| 522 | /** | ||
| 523 | * Reads characters before '<!-- excerpt|break -->'. | ||
| 524 | * | ||
| 525 | * @param string|null $string | ||
| 526 | * | ||
| 527 | * @return string|null | ||
| 528 | */ | ||
| 529 | public function excerptHtml(string $string = null): ?string | ||
| 530 |     { | ||
| 531 | // https://regex101.com/r/Xl7d5I/3 | ||
| 532 | $pattern = '(.*)(<!--[[:blank:]]?(excerpt|break)[[:blank:]]?-->)(.*)'; | ||
| 533 |         preg_match('/'.$pattern.'/is', $string, $matches); | ||
| 534 |         if (empty($matches)) { | ||
| 535 | return $string; | ||
| 536 | } | ||
| 537 | |||
| 538 | return trim($matches[1]); | ||
| 539 | } | ||
| 540 | |||
| 541 | /** | ||
| 542 | * Calculates estimated time to read a text. | ||
| 543 | * | ||
| 544 | * @param string|null $text | ||
| 545 | * | ||
| 546 | * @return string | ||
| 547 | */ | ||
| 548 | public function readtime(string $text = null): string | ||
| 549 |     { | ||
| 550 | $words = str_word_count(strip_tags($text)); | ||
| 551 | $min = floor($words / 200); | ||
| 552 |         if ($min === 0) { | ||
| 553 | return '1'; | ||
| 554 | } | ||
| 555 | |||
| 556 | return (string) $min; | ||
| 557 | } | ||
| 558 | |||
| 559 | /** | ||
| 560 | * Gets the value of an environment variable. | ||
| 561 | * | ||
| 562 | * @param string $var | ||
| 563 | * | ||
| 564 | * @return string|null | ||
| 565 | */ | ||
| 566 | public function getEnv(string $var): ?string | ||
| 569 | } | ||
| 570 | } | ||
| 571 |