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 |
||
| 8 | class Config |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var mixed |
||
| 12 | */ |
||
| 13 | private $config = []; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $services = [ |
||
| 19 | 'toggl', |
||
| 20 | 'harvest', |
||
| 21 | ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param $filename |
||
| 25 | */ |
||
| 26 | public function __construct($filename = null) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return mixed |
||
| 35 | */ |
||
| 36 | View Code Duplication | public function activatedServices() |
|
| 48 | |||
| 49 | public function generateDefaultConfigurationFile() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $section |
||
| 76 | * @param null $param |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | public function get($section = null, $param = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return boolean |
||
| 92 | */ |
||
| 93 | public function isConfigured() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function runningServices() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param $section |
||
| 116 | * @param $param |
||
| 117 | * @param $value |
||
| 118 | */ |
||
| 119 | public function update($section, $param, $value) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | private function load($filename) |
||
| 134 | |||
| 135 | private function save() |
||
| 146 | } |
||
| 147 |
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.