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 |
||
147 | |||
148 | /** |
||
149 | * Sort by title. |
||
150 | * |
||
151 | * @param CollectionInterface|array $collection |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function sortByTitle($collection): array |
||
166 | |||
167 | /** |
||
168 | * Sort by weight. |
||
169 | * |
||
170 | * @param CollectionInterface|array $collection |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | public function sortByWeight($collection): array |
||
199 | |||
200 | /** |
||
201 | * Sort by date. |
||
202 | * |
||
203 | * @param CollectionInterface|array $collection |
||
204 | * |
||
205 | * @return mixed |
||
206 | */ |
||
207 | public function sortByDate($collection): array |
||
232 | |||
233 | /** |
||
234 | * Create an URL. |
||
235 | * |
||
236 | * $options[ |
||
237 | * 'canonical' => null, |
||
238 | * 'addhash' => true, |
||
239 | * 'format' => 'json', |
||
240 | * ]; |
||
241 | * |
||
242 | * @param Page|string|null $value |
||
243 | * @param array|null $options |
||
244 | * |
||
245 | * @return string|null |
||
246 | */ |
||
247 | public function createUrl($value = null, $options = null): ?string |
||
313 | |||
314 | /** |
||
315 | * Minify a CSS or a JS file. |
||
316 | * |
||
317 | * @param string $path |
||
318 | * |
||
319 | * @throws Exception |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | public function minify(string $path): string |
||
345 | |||
346 | /** |
||
347 | * Minify CSS. |
||
348 | * |
||
349 | * @param string $value |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | public function minifyCss(string $value): string |
||
359 | |||
360 | /** |
||
361 | * Minify JS. |
||
362 | * |
||
363 | * @param string $value |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | public function minifyJs(string $value): string |
||
373 | |||
374 | /** |
||
375 | * Compile style file to CSS. |
||
376 | * |
||
377 | * @param string $path |
||
378 | * |
||
379 | * @throws Exception |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | public function toCss(string $path): string |
||
411 | |||
412 | /** |
||
413 | * Compile SCSS string to CSS. |
||
414 | * |
||
415 | * @param string $value |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public function scssToCss(string $value): string |
||
425 | |||
426 | /** |
||
427 | * Read $lenght first characters of a string and add a suffix. |
||
428 | * |
||
429 | * @param string|null $string |
||
430 | * @param int $length |
||
431 | * @param string $suffix |
||
432 | * |
||
433 | * @return string|null |
||
434 | */ |
||
435 | public function excerpt(string $string = null, int $length = 450, string $suffix = ' …'): ?string |
||
446 | |||
447 | /** |
||
448 | * Read characters before '<!-- excerpt -->'. |
||
449 | * |
||
450 | * @param string|null $string |
||
451 | * |
||
452 | * @return string|null |
||
453 | */ |
||
454 | public function excerptHtml(string $string = null): ?string |
||
469 | |||
470 | /** |
||
471 | * Calculate estimated time to read a text. |
||
472 | * |
||
473 | * @param string|null $text |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | public function readtime(string $text = null): string |
||
487 | |||
488 | /** |
||
489 | * Hash file with sha384. |
||
490 | * |
||
491 | * @param string $path |
||
492 | * |
||
493 | * @return string|null |
||
494 | */ |
||
495 | public function hashFile(string $path): ?string |
||
501 | |||
502 | /** |
||
503 | * Gets the value of an environment variable. |
||
504 | * |
||
505 | * @param string $var |
||
506 | * |
||
507 | * @return string|null |
||
508 | */ |
||
509 | public function getEnv(string $var): ?string |
||
513 | } |
||
514 |