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 |
||
| 26 | class Extension extends SlugifyExtension |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var Builder |
||
| 30 | */ |
||
| 31 | protected $builder; |
||
| 32 | /** |
||
| 33 | * @var Config |
||
| 34 | */ |
||
| 35 | protected $config; |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $outputPath; |
||
| 40 | /** |
||
| 41 | * @var Filesystem |
||
| 42 | */ |
||
| 43 | protected $fileSystem; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor. |
||
| 47 | * |
||
| 48 | * @param Builder $builder |
||
| 49 | */ |
||
| 50 | public function __construct(Builder $builder) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * {@inheritdoc} |
||
| 64 | */ |
||
| 65 | public function getName() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function getFilters() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function getFunctions() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Filter by section. |
||
| 107 | * |
||
| 108 | * @param PagesCollection $pages |
||
| 109 | * @param string $section |
||
| 110 | * |
||
| 111 | * @return CollectionInterface |
||
| 112 | */ |
||
| 113 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Filter by variable. |
||
| 120 | * |
||
| 121 | * @param PagesCollection $pages |
||
| 122 | * @param string $variable |
||
| 123 | * @param string $value |
||
| 124 | * |
||
| 125 | * @return CollectionInterface |
||
| 126 | */ |
||
| 127 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Sort by title. |
||
| 145 | * |
||
| 146 | * @param CollectionInterface|array $collection |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function sortByTitle($collection): array |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sort by weight. |
||
| 164 | * |
||
| 165 | * @param CollectionInterface|array $collection |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public function sortByWeight($collection): array |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sort by date. |
||
| 197 | * |
||
| 198 | * @param CollectionInterface|array $collection |
||
| 199 | * |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function sortByDate($collection): array |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Create an URL. |
||
| 230 | * |
||
| 231 | * $options[ |
||
| 232 | * 'canonical' => null, |
||
| 233 | * 'addhash' => true, |
||
| 234 | * 'format' => 'json', |
||
| 235 | * ]; |
||
| 236 | * |
||
| 237 | * @param Page|string|null $value |
||
| 238 | * @param array|null $options |
||
| 239 | * |
||
| 240 | * @return string|null |
||
| 241 | */ |
||
| 242 | public function createUrl($value = null, $options = null): ?string |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Minify a CSS or a JS file. |
||
| 294 | * |
||
| 295 | * @param string $path |
||
| 296 | * |
||
| 297 | * @throws Exception |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function minify(string $path): string |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Minify CSS. |
||
| 326 | * |
||
| 327 | * @param string $value |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function minifyCss(string $value): string |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Minify JS. |
||
| 340 | * |
||
| 341 | * @param string $value |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function minifyJs(string $value): string |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Compile style file to CSS. |
||
| 354 | * |
||
| 355 | * @param string $path |
||
| 356 | * |
||
| 357 | * @throws Exception |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function toCss(string $path): string |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Compile SCSS string to CSS. |
||
| 392 | * |
||
| 393 | * @param string $value |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function scssToCss(string $value): string |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Read $lenght first characters of a string and add a suffix. |
||
| 406 | * |
||
| 407 | * @param string $string |
||
| 408 | * @param int $length |
||
| 409 | * @param string $suffix |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function excerpt(string $string, int $length = 450, string $suffix = ' …'): string |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Read characters before '<!-- excerpt -->'. |
||
| 427 | * |
||
| 428 | * @param string $string |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function excerptHtml(string $string): string |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Calculate estimated time to read a text. |
||
| 450 | * |
||
| 451 | * @param string $text |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function readtime(string $text): string |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Hash file with sha384. |
||
| 468 | * |
||
| 469 | * @param string $path |
||
| 470 | * |
||
| 471 | * @return string|null |
||
| 472 | */ |
||
| 473 | public function hashFile(string $path): ?string |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Gets the value of an environment variable. |
||
| 482 | * |
||
| 483 | * @param string $var |
||
| 484 | * |
||
| 485 | * @return string|null |
||
| 486 | */ |
||
| 487 | public function getEnv(string $var): ?string |
||
| 491 | } |
||
| 492 |