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 GroupProperty extends \Sabre\VObject\Property\Text { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Add a group. |
||
| 36 | * |
||
| 37 | * NOTE: We cannot just use add() as that method name is used in |
||
| 38 | * \Sabre\VObject\Property |
||
| 39 | * |
||
| 40 | * @param string $name |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | 1 | View Code Duplication | public function addGroup($name) { |
| 55 | |||
| 56 | /** |
||
| 57 | * Remove an existing group. |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | 1 | View Code Duplication | public function removeGroup($name) { |
| 73 | |||
| 74 | /** |
||
| 75 | * Test it a group by that name exists. |
||
| 76 | * |
||
| 77 | * @param string $name |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 1 | public function hasGroup($name) { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Rename an existing group. |
||
| 89 | * |
||
| 90 | * @param string $from |
||
| 91 | * @param string $to |
||
| 92 | */ |
||
| 93 | 1 | public function renameGroup($from, $to) { |
|
| 104 | |||
| 105 | // case-insensitive in_array |
||
| 106 | 1 | protected function in_arrayi($needle, $haystack) { |
|
| 112 | |||
| 113 | // case-insensitive array_search |
||
| 114 | 1 | protected function array_searchi($needle, $haystack) { |
|
| 120 | } |
||
| 121 |
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.