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 | * @throws Exception |
||
| 126 | * |
||
| 127 | * @return CollectionInterface |
||
| 128 | */ |
||
| 129 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Sort by title. |
||
| 147 | * |
||
| 148 | * @param CollectionInterface|array $array |
||
| 149 | * |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | public function sortByTitle($array) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Sort by weight. |
||
| 166 | * |
||
| 167 | * @param CollectionInterface|array $array |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | public function sortByWeight($array) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Sort by date. |
||
| 199 | * |
||
| 200 | * @param CollectionInterface|array $array |
||
| 201 | * |
||
| 202 | * @return mixed |
||
| 203 | */ |
||
| 204 | public function sortByDate($array) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Create an URL. |
||
| 232 | * |
||
| 233 | * $options[ |
||
| 234 | * 'canonical' => null, |
||
| 235 | * 'addhash' => true, |
||
| 236 | * 'format' => 'json', |
||
| 237 | * ]; |
||
| 238 | * |
||
| 239 | * @param string|Page|null $value |
||
| 240 | * @param array|null $options |
||
| 241 | * |
||
| 242 | * @return string|null |
||
| 243 | */ |
||
| 244 | public function createUrl($value = null, $options = null): ?string |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Minify a CSS or a JS file. |
||
| 309 | * |
||
| 310 | * @param string $path |
||
| 311 | * |
||
| 312 | * @throws Exception |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function minify(string $path): string |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Minify CSS. |
||
| 341 | * |
||
| 342 | * @param string $value |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function minifyCss(string $value): string |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Minify JS. |
||
| 355 | * |
||
| 356 | * @param string $value |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function minifyJs(string $value): string |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Compile style file to CSS. |
||
| 369 | * |
||
| 370 | * @param string $path |
||
| 371 | * |
||
| 372 | * @throws Exception |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function toCss(string $path): string |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Compile SCSS string to CSS. |
||
| 407 | * |
||
| 408 | * @param string $value |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | public function scssToCss(string $value): string |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Read $lenght first characters of a string and add a suffix. |
||
| 421 | * |
||
| 422 | * @param string $string |
||
| 423 | * @param int $length |
||
| 424 | * @param string $suffix |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function excerpt(string $string, int $length = 450, string $suffix = ' …'): string |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Read characters before '<!-- excerpt -->'. |
||
| 442 | * |
||
| 443 | * @param string $string |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function excerptHtml(string $string): string |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Calculate estimated time to read a text. |
||
| 465 | * |
||
| 466 | * @param string $text |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function readtime(string $text): string |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Hash file with sha384. |
||
| 483 | * |
||
| 484 | * @param string $path |
||
| 485 | * |
||
| 486 | * @return string|null |
||
| 487 | */ |
||
| 488 | public function hashFile(string $path): ?string |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Gets the value of an environment variable. |
||
| 497 | * |
||
| 498 | * @param string $var |
||
| 499 | * |
||
| 500 | * @return string|false |
||
| 501 | */ |
||
| 502 | public function getEnv($var): ?string |
||
| 506 | } |
||
| 507 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.