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 |
||
| 25 | class Extension extends SlugifyExtension |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var Config |
||
| 29 | */ |
||
| 30 | protected $config; |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $destPath; |
||
| 35 | /** |
||
| 36 | * @var Filesystem |
||
| 37 | */ |
||
| 38 | protected $fileSystem; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor. |
||
| 42 | * |
||
| 43 | * @param Config $config |
||
| 44 | */ |
||
| 45 | public function __construct(Config $config) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | public function getName() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | public function getFilters() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function getFunctions() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Filter by section. |
||
| 101 | * |
||
| 102 | * @param PagesCollection $pages |
||
| 103 | * @param string $section |
||
| 104 | * |
||
| 105 | * @return CollectionInterface |
||
| 106 | */ |
||
| 107 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Filter by variable. |
||
| 114 | * |
||
| 115 | * @param PagesCollection $pages |
||
| 116 | * @param string $variable |
||
| 117 | * @param string $value |
||
| 118 | * |
||
| 119 | * @throws Exception |
||
| 120 | * |
||
| 121 | * @return CollectionInterface |
||
| 122 | */ |
||
| 123 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Sort by title. |
||
| 141 | * |
||
| 142 | * @param CollectionInterface|array $array |
||
| 143 | * |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | public function sortByTitle($array) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sort by weight. |
||
| 160 | * |
||
| 161 | * @param CollectionInterface|array $array |
||
| 162 | * |
||
| 163 | * @return mixed |
||
| 164 | */ |
||
| 165 | public function sortByWeight($array) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Sort by date. |
||
| 193 | * |
||
| 194 | * @param CollectionInterface|array $array |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function sortByDate($array) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Create an URL. |
||
| 226 | * |
||
| 227 | * $options[ |
||
| 228 | * 'canonical' => null, |
||
| 229 | * 'addhash' => true, |
||
| 230 | * 'format' => 'json', |
||
| 231 | * ]; |
||
| 232 | * |
||
| 233 | * @param string|Page|null $value |
||
| 234 | * @param array|null $options |
||
| 235 | * |
||
| 236 | * @return string|null |
||
| 237 | */ |
||
| 238 | public function createUrl($value = null, $options = null): ?string |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Minify a CSS or a JS file. |
||
| 296 | * |
||
| 297 | * @param string $path |
||
| 298 | * |
||
| 299 | * @throws Exception |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function minify(string $path): string |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Minify CSS. |
||
| 328 | * |
||
| 329 | * @param string $value |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function minifyCss(string $value): string |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Minify JS. |
||
| 342 | * |
||
| 343 | * @param string $value |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function minifyJs(string $value): string |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Compile style file to CSS. |
||
| 356 | * |
||
| 357 | * @param string $path |
||
| 358 | * |
||
| 359 | * @throws Exception |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function toCss(string $path): string |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Compile SCSS string to CSS. |
||
| 394 | * |
||
| 395 | * @param string $value |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | public function scssToCss(string $value): string |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Read $lenght first characters of a string and add a suffix. |
||
| 408 | * |
||
| 409 | * @param string $string |
||
| 410 | * @param int $length |
||
| 411 | * @param string $suffix |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function excerpt(string $string, int $length = 450, string $suffix = ' …'): string |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Read characters before '<!-- excerpt -->'. |
||
| 429 | * |
||
| 430 | * @param string $string |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function excerptHtml(string $string): string |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Calculate estimated time to read a text. |
||
| 452 | * |
||
| 453 | * @param string $text |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | public function readtime(string $text): string |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Hash file with sha384. |
||
| 470 | * |
||
| 471 | * @param string $path |
||
| 472 | * |
||
| 473 | * @return string|null |
||
| 474 | */ |
||
| 475 | public function hashFile(string $path): ?string |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Gets the value of an environment variable. |
||
| 484 | * |
||
| 485 | * @param string $var |
||
| 486 | * |
||
| 487 | * @return string|false |
||
| 488 | */ |
||
| 489 | public function getEnv($var): ?string |
||
| 493 | } |
||
| 494 |