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) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Sort by title. |
||
| 136 | * |
||
| 137 | * @param $array|CollectionInterface |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function sortByTitle($array) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Sort by weight. |
||
| 155 | * |
||
| 156 | * @param $array|CollectionInterface |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function sortByWeight($array) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Sort by date. |
||
| 188 | * |
||
| 189 | * @param $array|CollectionInterface |
||
| 190 | * |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | View Code Duplication | public function sortByDate($array) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Create an URL. |
||
| 221 | * |
||
| 222 | * $options[ |
||
| 223 | * 'canonical' => null, |
||
| 224 | * 'addhash' => true, |
||
| 225 | * ]; |
||
| 226 | * |
||
| 227 | * @param \Twig_Environment $env |
||
| 228 | * @param string|\Cecil\Page\Page|null $value |
||
| 229 | * @param array|null $options |
||
| 230 | * |
||
| 231 | * @return string|null |
||
|
|
|||
| 232 | */ |
||
| 233 | public function createUrl(\Twig_Environment $env, $value = null, $options = null) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Minify a CSS or a JS file. |
||
| 287 | * |
||
| 288 | * @param string $path |
||
| 289 | * |
||
| 290 | * @throws Exception |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function minify($path) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Minify CSS. |
||
| 319 | * |
||
| 320 | * @param $value |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function minifyCss($value) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Minify JS. |
||
| 333 | * |
||
| 334 | * @param $value |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function minifyJs($value) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Compile style file to CSS. |
||
| 347 | * |
||
| 348 | * @param string $path |
||
| 349 | * |
||
| 350 | * @throws Exception |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function toCss($path) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Compile SCSS string to CSS. |
||
| 385 | * |
||
| 386 | * @param $value |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function scssToCss($value) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Read $lenght first characters of a string and add a suffix. |
||
| 399 | * |
||
| 400 | * @param $string |
||
| 401 | * @param int $length |
||
| 402 | * @param string $suffix |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function excerpt($string, $length = 450, $suffix = ' …') |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Read characters before '<!-- excerpt -->'. |
||
| 420 | * |
||
| 421 | * @param $string |
||
| 422 | * |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function excerptHtml($string) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Calculate estimated time to read a text. |
||
| 443 | * |
||
| 444 | * @param $text |
||
| 445 | * |
||
| 446 | * @return float|string |
||
| 447 | */ |
||
| 448 | public function readtime($text) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Hash file with sha384. |
||
| 461 | * |
||
| 462 | * @param string $path |
||
| 463 | * |
||
| 464 | * @return string|null |
||
| 465 | */ |
||
| 466 | public function hashFile($path) |
||
| 472 | } |
||
| 473 |
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.