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() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Filter by section. |
||
| 93 | * |
||
| 94 | * @param \Cecil\Page\Collection $pages |
||
| 95 | * @param string $section |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function filterBySection($pages, $section) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Filter by variable. |
||
| 106 | * |
||
| 107 | * @param \Cecil\Page\Collection $pages |
||
| 108 | * @param string $variable |
||
| 109 | * @param string $value |
||
| 110 | * |
||
| 111 | * @throws Exception |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function filterBy($pages, $variable, $value) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Sort by title. |
||
| 133 | * |
||
| 134 | * @param $array|CollectionInterface |
||
| 135 | * |
||
| 136 | * @return mixed |
||
| 137 | */ |
||
| 138 | public function sortByTitle($array) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Sort by weight. |
||
| 152 | * |
||
| 153 | * @param $array|CollectionInterface |
||
| 154 | * |
||
| 155 | * @return mixed |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function sortByWeight($array) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Sort by date. |
||
| 185 | * |
||
| 186 | * @param $array|CollectionInterface |
||
| 187 | * |
||
| 188 | * @return mixed |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function sortByDate($array) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Create an URL. |
||
| 218 | * |
||
| 219 | * $options[ |
||
| 220 | * 'canonical' => null, |
||
| 221 | * 'addhash' => true, |
||
| 222 | * ]; |
||
| 223 | * |
||
| 224 | * @param \Twig_Environment $env |
||
| 225 | * @param string|\Cecil\Page\Page|null $value |
||
| 226 | * @param array|null $options |
||
| 227 | * |
||
| 228 | * @return string|null |
||
|
|
|||
| 229 | */ |
||
| 230 | public function createUrl(\Twig_Environment $env, $value = null, $options = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Minify a CSS or a JS file. |
||
| 284 | * |
||
| 285 | * @param string $path |
||
| 286 | * |
||
| 287 | * @throws Exception |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function minify($path) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Minify CSS. |
||
| 316 | * |
||
| 317 | * @param $value |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function minifyCss($value) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Minify JS. |
||
| 330 | * |
||
| 331 | * @param $value |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function minifyJs($value) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Compile style file to CSS. |
||
| 344 | * |
||
| 345 | * @param string $path |
||
| 346 | * |
||
| 347 | * @throws Exception |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function toCss($path) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Compile SCSS string to CSS. |
||
| 382 | * |
||
| 383 | * @param $value |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function scssToCss($value) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Read $lenght first characters of a string and add a suffix. |
||
| 396 | * |
||
| 397 | * @param $string |
||
| 398 | * @param int $length |
||
| 399 | * @param string $suffix |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function excerpt($string, $length = 450, $suffix = ' …') |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Read characters before '<!-- excerpt -->'. |
||
| 417 | * |
||
| 418 | * @param $string |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function excerptHtml($string) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Calculate estimated time to read a text. |
||
| 440 | * |
||
| 441 | * @param $text |
||
| 442 | * |
||
| 443 | * @return float|string |
||
| 444 | */ |
||
| 445 | public function readtime($text) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Hash file with sha384. |
||
| 458 | * |
||
| 459 | * @param string $path |
||
| 460 | * |
||
| 461 | * @return string|null |
||
| 462 | */ |
||
| 463 | public function hashFile($path) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Gets the value of an environment variable. |
||
| 472 | * |
||
| 473 | * @param string $var |
||
| 474 | * |
||
| 475 | * @return string|false |
||
| 476 | */ |
||
| 477 | public function getEnv($var) |
||
| 481 | } |
||
| 482 |
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.