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:
Complex classes like IBlockProperty often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IBlockProperty, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class IBlockProperty |
||
11 | { |
||
12 | use FieldConstructor; |
||
13 | |||
14 | /** |
||
15 | * Добавить свойство инфоблока |
||
16 | * @throws \Exception |
||
17 | */ |
||
18 | public function add() |
||
32 | |||
33 | /** |
||
34 | * Обновить свойство инфоблока |
||
35 | * @param $id |
||
36 | * @throws \Exception |
||
37 | */ |
||
38 | View Code Duplication | public function update($id) |
|
47 | |||
48 | /** |
||
49 | * Удалить свойство инфоблока |
||
50 | * @param $id |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | public static function delete($id) |
||
61 | |||
62 | /** |
||
63 | * Установить настройки для добавления свойства инфоблока по умолчанию |
||
64 | * @param string $code |
||
65 | * @param string $name |
||
66 | * @param int $iblockId |
||
67 | * @return IBlockProperty |
||
68 | */ |
||
69 | public function constructDefault($code, $name, $iblockId) |
||
73 | |||
74 | /** |
||
75 | * Символьный идентификатор. |
||
76 | * @param string $code |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function setCode($code) |
||
85 | |||
86 | /** |
||
87 | * Внешний код. |
||
88 | * @param string $xml_id |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function setXmlId($xml_id) |
||
97 | |||
98 | /** |
||
99 | * Код информационного блока. |
||
100 | * @param string $iblock_id |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setIblockId($iblock_id) |
||
109 | |||
110 | /** |
||
111 | * Название. |
||
112 | * @param string $name |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function setName($name) |
||
121 | |||
122 | /** |
||
123 | * Флаг активности |
||
124 | * @param bool $active |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setActive($active = true) |
||
133 | |||
134 | /** |
||
135 | * Обязательное (Y|N). |
||
136 | * @param bool $isRequired |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function setIsRequired($isRequired = true) |
||
145 | |||
146 | /** |
||
147 | * Индекс сортировки. |
||
148 | * @param int $sort |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function setSort($sort = 500) |
||
157 | |||
158 | /** |
||
159 | * Тип свойства. Возможные значения: S - строка, N - число, F - файл, L - список, E - привязка к элементам, G - привязка к группам. |
||
160 | * @param string $propertyType |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function setPropertyType($propertyType = 'S') |
||
169 | |||
170 | /** |
||
171 | * Установить тип свойства "Список" |
||
172 | * @param array $values массив доступных значений (можно собрать с помощью класса IBlockPropertyEnum) |
||
173 | * @param string $listType Тип, может быть "L" - выпадающий список или "C" - флажки. |
||
174 | * @param int $multipleCnt Количество строк в выпадающем списке |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function setPropertyTypeList($values, $listType = null, $multipleCnt = null) |
||
192 | |||
193 | /** |
||
194 | * Установить тип свойства "Файл" |
||
195 | * @param string $fileType Список допустимых расширений (через запятую). |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setPropertyTypeFile($fileType = null) |
||
208 | |||
209 | /** |
||
210 | * Установить тип свойства "привязка к элементам" или "привязка к группам" |
||
211 | * @param string $property_type Тип свойства. Возможные значения: E - привязка к элементам, G - привязка к группам. |
||
212 | * @param string $linkIblockId код информационного блока с элементами/группами которого и будут связано значение. |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setPropertyTypeIblock($property_type, $linkIblockId) |
||
221 | |||
222 | /** |
||
223 | * Установить тип свойства "справочник" |
||
224 | * @param string $table_name таблица HL для связи |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function setPropertyTypeHl($table_name) |
||
235 | |||
236 | /** |
||
237 | * Множественность (Y|N). |
||
238 | * @param bool $multiple |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function setMultiple($multiple = false) |
||
247 | |||
248 | /** |
||
249 | * Количество строк в выпадающем списке для свойств типа "список". |
||
250 | * @param int $multipleCnt |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function setMultipleCnt($multipleCnt) |
||
259 | |||
260 | /** |
||
261 | * Значение свойства по умолчанию (кроме свойства типа список L). |
||
262 | * @param string $defaultValue |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function setDefaultValue($defaultValue) |
||
271 | |||
272 | /** |
||
273 | * Количество строк в ячейке ввода значения свойства. |
||
274 | * @param int $rowCount |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function setRowCount($rowCount) |
||
283 | |||
284 | /** |
||
285 | * Количество столбцов в ячейке ввода значения свойства. |
||
286 | * @param int $colCount |
||
287 | * @return $this |
||
288 | */ |
||
289 | public function setColCount($colCount) |
||
295 | |||
296 | /** |
||
297 | * Тип для свойства список (L). Может быть "L" - выпадающий список или "C" - флажки. |
||
298 | * @param string $listType |
||
299 | * @return $this |
||
300 | */ |
||
301 | public function setListType($listType = 'L') |
||
307 | |||
308 | /** |
||
309 | * Список допустимых расширений для свойств файл "F" (через запятую). |
||
310 | * @param string $fileType |
||
311 | * @return $this |
||
312 | */ |
||
313 | public function setFileType($fileType) |
||
319 | |||
320 | /** |
||
321 | * Индексировать значения данного свойства. |
||
322 | * @param bool $searchable |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setSearchable($searchable = false) |
||
331 | |||
332 | /** |
||
333 | * Выводить поля для фильтрации по данному свойству на странице списка элементов в административном разделе. |
||
334 | * @param bool $filtrable |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function setFiltrable($filtrable = false) |
||
343 | |||
344 | /** |
||
345 | * Для свойств типа привязки к элементам и группам задает код информационного блока с элементами/группами которого и будут связано значение. |
||
346 | * @param int $linkIblockId |
||
347 | * @return $this |
||
348 | */ |
||
349 | public function setLinkIblockId($linkIblockId) |
||
355 | |||
356 | /** |
||
357 | * Признак наличия у значения свойства дополнительного поля описания. Только для типов S - строка, N - число и F - файл (Y|N). |
||
358 | * @param bool $withDescription |
||
359 | * @return $this |
||
360 | */ |
||
361 | public function setWithDescription($withDescription) |
||
367 | |||
368 | /** |
||
369 | * Идентификатор пользовательского типа свойства. |
||
370 | * @param string $user_type |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function setUserType($user_type) |
||
379 | |||
380 | /** |
||
381 | * Идентификатор пользовательского типа свойства. |
||
382 | * @param array $user_type_settings |
||
383 | * @return $this |
||
384 | */ |
||
385 | public function setUserTypeSettings($user_type_settings) |
||
391 | |||
392 | /** |
||
393 | * Подсказка |
||
394 | * @param string $hint |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function setHint($hint) |
||
403 | } |
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.