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 |
||
12 | class HighloadBlock |
||
13 | { |
||
14 | use FieldConstructor; |
||
15 | |||
16 | public $lang; |
||
17 | |||
18 | /** |
||
19 | * Добавить HL |
||
20 | * @throws \Exception |
||
21 | */ |
||
22 | public function add() |
||
42 | |||
43 | /** |
||
44 | * Обновить HL |
||
45 | * @param $table_name |
||
46 | * @throws \Exception |
||
47 | */ |
||
48 | View Code Duplication | public function update($table_name) |
|
59 | |||
60 | /** |
||
61 | * Удалить HL |
||
62 | * @param $table_name |
||
63 | * @throws \Exception |
||
64 | */ |
||
65 | View Code Duplication | public static function delete($table_name) |
|
76 | |||
77 | /** |
||
78 | * Установить настройки для добавления HL по умолчанию |
||
79 | * @param string $name Название highload-блока |
||
80 | * @param string $table_name Название таблицы с элементами highload-блока. |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function constructDefault($name, $table_name) |
||
87 | |||
88 | /** |
||
89 | * Название highload-блока. |
||
90 | * @param string $name |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setName($name) |
||
99 | |||
100 | /** |
||
101 | * Название таблицы с элементами highload-блока. |
||
102 | * @param string $table_name |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function setTableName($table_name) |
||
111 | |||
112 | /** |
||
113 | * Установить локализацию |
||
114 | * @param $lang |
||
115 | * @param $text |
||
116 | * @return HighloadBlock |
||
117 | */ |
||
118 | public function setLang($lang, $text) |
||
124 | } |
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.