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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 24 | class Extension extends SlugifyExtension |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $destPath; |
||
| 30 | /** |
||
| 31 | * @var Filesystem |
||
| 32 | */ |
||
| 33 | protected $fileSystem; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor. |
||
| 37 | * |
||
| 38 | * @param string $destPath |
||
| 39 | */ |
||
| 40 | public function __construct($destPath) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public function getName() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function getFilters() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | public function getFunctions() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Filter by section. |
||
| 95 | * |
||
| 96 | * @param PagesCollection $pages |
||
| 97 | * @param string $section |
||
| 98 | * |
||
| 99 | * @return CollectionInterface |
||
| 100 | */ |
||
| 101 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Filter by variable. |
||
| 108 | * |
||
| 109 | * @param PagesCollection $pages |
||
| 110 | * @param string $variable |
||
| 111 | * @param string $value |
||
| 112 | * |
||
| 113 | * @throws Exception |
||
| 114 | * |
||
| 115 | * @return CollectionInterface |
||
| 116 | */ |
||
| 117 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Sort by title. |
||
| 135 | * |
||
| 136 | * @param CollectionInterface|array $array |
||
| 137 | * |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | public function sortByTitle($array) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Sort by weight. |
||
| 154 | * |
||
| 155 | * @param CollectionInterface|array $array |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function sortByWeight($array) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Sort by date. |
||
| 187 | * |
||
| 188 | * @param CollectionInterface|array $array |
||
| 189 | * |
||
| 190 | * @return mixed |
||
| 191 | */ |
||
| 192 | public function sortByDate($array) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Create an URL. |
||
| 220 | * |
||
| 221 | * $options[ |
||
| 222 | * 'canonical' => null, |
||
| 223 | * 'addhash' => true, |
||
| 224 | * ]; |
||
| 225 | * |
||
| 226 | * @param \Twig_Environment $env |
||
| 227 | * @param string|Page|null $value |
||
| 228 | * @param array|null $options |
||
| 229 | * |
||
| 230 | * @return string|null |
||
| 231 | */ |
||
| 232 | public function createUrl(\Twig_Environment $env, $value = null, $options = null): ?string |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Minify a CSS or a JS file. |
||
| 300 | * |
||
| 301 | * @param string $path |
||
| 302 | * |
||
| 303 | * @throws Exception |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function minify(string $path): string |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Minify CSS. |
||
| 332 | * |
||
| 333 | * @param string $value |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function minifyCss(string $value): string |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Minify JS. |
||
| 346 | * |
||
| 347 | * @param string $value |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function minifyJs(string $value): string |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Compile style file to CSS. |
||
| 360 | * |
||
| 361 | * @param string $path |
||
| 362 | * |
||
| 363 | * @throws Exception |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function toCss(string $path): string |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Compile SCSS string to CSS. |
||
| 398 | * |
||
| 399 | * @param string $value |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function scssToCss(string $value): string |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Read $lenght first characters of a string and add a suffix. |
||
| 412 | * |
||
| 413 | * @param string $string |
||
| 414 | * @param int $length |
||
| 415 | * @param string $suffix |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function excerpt(string $string, int $length = 450, string $suffix = ' …'): string |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Read characters before '<!-- excerpt -->'. |
||
| 433 | * |
||
| 434 | * @param string $string |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function excerptHtml(string $string): string |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Calculate estimated time to read a text. |
||
| 456 | * |
||
| 457 | * @param string $text |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function readtime(string $text): string |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Hash file with sha384. |
||
| 474 | * |
||
| 475 | * @param string $path |
||
| 476 | * |
||
| 477 | * @return string|null |
||
| 478 | */ |
||
| 479 | public function hashFile(string $path): ?string |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Gets the value of an environment variable. |
||
| 488 | * |
||
| 489 | * @param string $var |
||
| 490 | * |
||
| 491 | * @return string|false |
||
| 492 | */ |
||
| 493 | public function getEnv($var): ?string |
||
| 497 | } |
||
| 498 |