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 IniFile |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $file; |
||
| 14 | /** |
||
| 15 | * @var IniParser; |
||
| 16 | */ |
||
| 17 | protected $parser; |
||
| 18 | /** |
||
| 19 | * @var IniSection[] |
||
| 20 | */ |
||
| 21 | protected $sections = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * IniFile constructor. |
||
| 25 | * |
||
| 26 | * @param string|null $file |
||
| 27 | * |
||
| 28 | * @throws FileException |
||
| 29 | */ |
||
| 30 | 20 | public function __construct($file = null) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param string $file |
||
| 44 | * |
||
| 45 | * @return IniFile |
||
| 46 | * @throws FileException |
||
| 47 | */ |
||
| 48 | public static function load($file) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param null|string $outputFile |
||
| 55 | * |
||
| 56 | * @throws FileException |
||
| 57 | */ |
||
| 58 | public function save($outputFile = null) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param IniSection $section |
||
| 83 | * |
||
| 84 | * @return $this |
||
| 85 | * @throws InvalidDataException |
||
| 86 | */ |
||
| 87 | 1 | public function addSection(IniSection $section) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $sectionName |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 12 | public function hasSection($sectionName) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $sectionName |
||
| 121 | * |
||
| 122 | * @return IniSection |
||
| 123 | * @throws InvalidDataException |
||
| 124 | */ |
||
| 125 | 12 | View Code Duplication | public function getSection($sectionName) |
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $sectionName |
||
| 137 | * |
||
| 138 | * @throws InvalidDataException |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function deleteSection($sectionName) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Get normalized item value |
||
| 152 | * |
||
| 153 | * @param string $sectionName |
||
| 154 | * @param string $itemName |
||
| 155 | * @param mixed $defaultValue |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | * @throws InvalidDataException |
||
| 159 | */ |
||
| 160 | 8 | public function get($sectionName, $itemName, $defaultValue = null) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $sectionName |
||
| 169 | * @param string $itemName |
||
| 170 | * @param string $itemValue |
||
| 171 | * |
||
| 172 | * @return $this |
||
| 173 | * @throws InvalidDataException |
||
| 174 | */ |
||
| 175 | public function set($sectionName, $itemName, $itemValue) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $sectionName |
||
| 185 | * @param string $itemName |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | * @throws InvalidDataException |
||
| 189 | */ |
||
| 190 | 4 | public function delete($sectionName, $itemName) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | 3 | public function toArray() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | 2 | public function toString() |
|
| 225 | } |
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.