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 |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Minify a CSS or a JS file. |
||
| 302 | * |
||
| 303 | * @param string $path |
||
| 304 | * |
||
| 305 | * @throws Exception |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function minify(string $path): string |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Minify CSS. |
||
| 334 | * |
||
| 335 | * @param string $value |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function minifyCss(string $value): string |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Minify JS. |
||
| 348 | * |
||
| 349 | * @param string $value |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function minifyJs(string $value): string |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Compile style file to CSS. |
||
| 362 | * |
||
| 363 | * @param string $path |
||
| 364 | * |
||
| 365 | * @throws Exception |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function toCss(string $path): string |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Compile SCSS string to CSS. |
||
| 400 | * |
||
| 401 | * @param string $value |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function scssToCss(string $value): string |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Read $lenght first characters of a string and add a suffix. |
||
| 414 | * |
||
| 415 | * @param string $string |
||
| 416 | * @param int $length |
||
| 417 | * @param string $suffix |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function excerpt(string $string, int $length = 450, string $suffix = ' …'): string |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Read characters before '<!-- excerpt -->'. |
||
| 435 | * |
||
| 436 | * @param string $string |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function excerptHtml(string $string): string |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Calculate estimated time to read a text. |
||
| 458 | * |
||
| 459 | * @param string $text |
||
| 460 | * |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | public function readtime(string $text): string |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Hash file with sha384. |
||
| 476 | * |
||
| 477 | * @param string $path |
||
| 478 | * |
||
| 479 | * @return string|null |
||
| 480 | */ |
||
| 481 | public function hashFile(string $path): ?string |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Gets the value of an environment variable. |
||
| 490 | * |
||
| 491 | * @param string $var |
||
| 492 | * |
||
| 493 | * @return string|null |
||
| 494 | */ |
||
| 495 | public function getEnv(string $var): ?string |
||
| 499 | } |
||
| 500 |