Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 24 | class Extension extends SlugifyExtension |
||
| 25 | { |
||
| 26 | /* @var string */ |
||
| 27 | protected $destPath; |
||
| 28 | /** |
||
| 29 | * @var Filesystem |
||
| 30 | */ |
||
| 31 | protected $fileSystem; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Constructor. |
||
| 35 | * |
||
| 36 | * @param string $destPath |
||
| 37 | */ |
||
| 38 | public function __construct($destPath) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function getName() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | public function getFilters() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function getFunctions() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Filter by section. |
||
| 92 | * |
||
| 93 | * @param \Cecil\Page\Collection $pages |
||
| 94 | * @param string $section |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public function filterBySection($pages, $section) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Filter by variable. |
||
| 105 | * |
||
| 106 | * @param \Cecil\Page\Collection $pages |
||
| 107 | * @param string $variable |
||
| 108 | * @param string $value |
||
| 109 | * |
||
| 110 | * @throws Exception |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function filterBy($pages, $variable, $value) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Sort by title. |
||
| 132 | * |
||
| 133 | * @param $array|CollectionInterface |
||
| 134 | * |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | public function sortByTitle($array) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Sort by weight. |
||
| 151 | * |
||
| 152 | * @param $array|CollectionInterface |
||
| 153 | * |
||
| 154 | * @return mixed |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function sortByWeight($array) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Sort by date. |
||
| 184 | * |
||
| 185 | * @param $array|CollectionInterface |
||
| 186 | * |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function sortByDate($array) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Create an URL. |
||
| 217 | * |
||
| 218 | * $options[ |
||
| 219 | * 'canonical' => null, |
||
| 220 | * 'addhash' => true, |
||
| 221 | * ]; |
||
| 222 | * |
||
| 223 | * @param \Twig_Environment $env |
||
| 224 | * @param string|\Cecil\Page\Page|null $value |
||
| 225 | * @param array|null $options |
||
| 226 | * |
||
| 227 | * @return string|null |
||
|
|
|||
| 228 | */ |
||
| 229 | public function createUrl(\Twig_Environment $env, $value = null, $options = null) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Minify a CSS or a JS file. |
||
| 283 | * |
||
| 284 | * @param string $path |
||
| 285 | * |
||
| 286 | * @throws Exception |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function minify($path) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Minify CSS. |
||
| 315 | * |
||
| 316 | * @param $value |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function minifyCss($value) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Minify JS. |
||
| 329 | * |
||
| 330 | * @param $value |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function minifyJs($value) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Compile style file to CSS. |
||
| 343 | * |
||
| 344 | * @param string $path |
||
| 345 | * |
||
| 346 | * @throws Exception |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public function toCss($path) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Compile SCSS string to CSS. |
||
| 381 | * |
||
| 382 | * @param $value |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function scssToCss($value) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Read $lenght first characters of a string and add a suffix. |
||
| 395 | * |
||
| 396 | * @param $string |
||
| 397 | * @param int $length |
||
| 398 | * @param string $suffix |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function excerpt($string, $length = 450, $suffix = ' …') |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Read characters before '<!-- excerpt -->'. |
||
| 416 | * |
||
| 417 | * @param $string |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function excerptHtml($string) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Calculate estimated time to read a text. |
||
| 439 | * |
||
| 440 | * @param $text |
||
| 441 | * |
||
| 442 | * @return float|string |
||
| 443 | */ |
||
| 444 | public function readtime($text) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Hash file with sha384. |
||
| 457 | * |
||
| 458 | * @param string $path |
||
| 459 | * |
||
| 460 | * @return string|null |
||
| 461 | */ |
||
| 462 | public function hashFile($path) |
||
| 468 | } |
||
| 469 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.