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:
| 1 | <?php |
||
| 32 | class MiscService { |
||
| 33 | |||
| 34 | /** @var ILogger */ |
||
| 35 | private $logger; |
||
| 36 | |||
| 37 | public function __construct(ILogger $logger) { |
||
| 40 | |||
| 41 | public function log($message, $level = 2) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param $arr |
||
| 52 | * @param $k |
||
| 53 | * |
||
| 54 | * @param string $default |
||
| 55 | * |
||
| 56 | * @return array|string|integer |
||
| 57 | */ |
||
| 58 | public static function get($arr, $k, $default = '') { |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $path |
||
| 69 | * @param bool $trim |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | View Code Duplication | public static function endSlash($path, $trim = false) { |
|
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $path |
||
| 88 | * @param bool $trim |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | View Code Duplication | public static function noEndSlash($path, $trim = false) { |
|
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $path |
||
| 107 | * @param bool $trim |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | View Code Duplication | public static function noBeginSlash($path, $trim = false) { |
|
| 122 | |||
| 123 | } |
||
| 124 | |||
| 125 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.