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 |
||
248 | { |
||
249 | $baseurl = $this->config->get('site.baseurl'); |
||
250 | $hash = md5($this->config->get('site.time')); |
||
251 | $base = ''; |
||
252 | // handle options |
||
253 | $canonical = null; |
||
254 | $addhash = false; |
||
255 | $format = null; |
||
256 | // backward compatibility |
||
257 | if (is_bool($options)) { |
||
258 | $oldOptions = $options; |
||
259 | $options = []; |
||
260 | $options['canonical'] = false; |
||
261 | if ($oldOptions === true) { |
||
262 | $options['canonical'] = true; |
||
263 | } |
||
264 | } |
||
265 | extract($options ?: []); |
||
266 | |||
267 | // set baseurl |
||
268 | if ($this->config->get('site.canonicalurl') === true || $canonical === true) { |
||
269 | $base = rtrim($baseurl, '/'); |
||
270 | } |
||
271 | if ($canonical === false) { |
||
272 | $base = ''; |
||
273 | } |
||
274 | |||
275 | // Page item |
||
276 | if ($value instanceof Page) { |
||
277 | if (!$format) { |
||
278 | $format = $value->getVariable('output'); |
||
279 | if (is_array($value->getVariable('output'))) { |
||
280 | $format = $value->getVariable('output')[0]; |
||
281 | } |
||
282 | if (!$format) { |
||
283 | $format = 'html'; |
||
284 | } |
||
285 | } |
||
286 | $url = $value->getUrl($format, $this->config); |
||
287 | $url = $base.'/'.ltrim($url, '/'); |
||
288 | } else { |
||
289 | // string |
||
290 | if (preg_match('~^(?:f|ht)tps?://~i', $value)) { // external URL |
||
291 | $url = $value; |
||
292 | } else { |
||
293 | if (false !== strpos($value, '.')) { // ressource URL (with a dot for extension) |
||
294 | $url = $value; |
||
295 | if ($addhash) { |
||
296 | $url .= '?'.$hash; |
||
297 | } |
||
298 | $url = $base.'/'.ltrim($url, '/'); |
||
299 | } else { |
||
300 | $url = $base.'/'; |
||
301 | if (!empty($value) && $value != '/') { |
||
302 | $url = $base.'/'.$value; |
||
303 | // value == page ID? |
||
304 | $pageId = $this->slugifyFilter($value); |
||
305 | if ($this->builder->getPages()->has($pageId)) { |
||
306 | $page = $this->builder->getPages()->get($pageId); |
||
307 | $url = $this->createUrl($page, $options); |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | |||
314 | return $url; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Minify a CSS or a JS file. |
||
319 | * |
||
320 | * @param string $path |
||
321 | * |
||
322 | * @throws Exception |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function minify(string $path): string |
||
348 | |||
349 | /** |
||
350 | * Minify CSS. |
||
351 | * |
||
352 | * @param string $value |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function minifyCss(string $value): string |
||
362 | |||
363 | /** |
||
364 | * Minify JS. |
||
365 | * |
||
366 | * @param string $value |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public function minifyJs(string $value): string |
||
376 | |||
377 | /** |
||
378 | * Compile style file to CSS. |
||
379 | * |
||
380 | * @param string $path |
||
381 | * |
||
382 | * @throws Exception |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | public function toCss(string $path): string |
||
414 | |||
415 | /** |
||
416 | * Compile SCSS string to CSS. |
||
417 | * |
||
418 | * @param string $value |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | public function scssToCss(string $value): string |
||
428 | |||
429 | /** |
||
430 | * Read $lenght first characters of a string and add a suffix. |
||
431 | * |
||
432 | * @param string|null $string |
||
433 | * @param int $length |
||
434 | * @param string $suffix |
||
435 | * |
||
436 | * @return string|null |
||
437 | */ |
||
438 | public function excerpt(string $string = null, int $length = 450, string $suffix = ' …'): ?string |
||
449 | |||
450 | /** |
||
451 | * Read characters before '<!-- excerpt|break -->'. |
||
452 | * |
||
453 | * @param string|null $string |
||
454 | * |
||
455 | * @return string|null |
||
456 | */ |
||
457 | public function excerptHtml(string $string = null): ?string |
||
471 | |||
472 | /** |
||
473 | * Calculate estimated time to read a text. |
||
474 | * |
||
475 | * @param string|null $text |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | public function readtime(string $text = null): string |
||
489 | |||
490 | /** |
||
491 | * Hash file with sha384. |
||
492 | * Useful for SRI (Subresource Integrity). |
||
493 | * |
||
494 | * @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity |
||
495 | * |
||
496 | * @param string $path |
||
497 | * |
||
498 | * @return string|null |
||
499 | */ |
||
500 | public function hashFile(string $path): ?string |
||
508 | |||
509 | /** |
||
510 | * Gets the value of an environment variable. |
||
511 | * |
||
512 | * @param string $var |
||
513 | * |
||
514 | * @return string|null |
||
515 | */ |
||
516 | public function getEnv(string $var): ?string |
||
520 | } |
||
521 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.