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 |
||
9 | abstract class BitrixModel extends BaseBitrixModel |
||
10 | { |
||
11 | /** |
||
12 | * Bitrix entity object. |
||
13 | * |
||
14 | * @var object |
||
15 | */ |
||
16 | public static $bxObject; |
||
17 | |||
18 | /** |
||
19 | * Corresponding object class name. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected static $objectClass = ''; |
||
24 | |||
25 | /** |
||
26 | * Fetch method and parameters. |
||
27 | * |
||
28 | * @var array|string |
||
29 | */ |
||
30 | public static $fetchUsing = [ |
||
31 | 'method' => 'Fetch', |
||
32 | 'params' => [], |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | * |
||
38 | * @param $id |
||
39 | * @param $fields |
||
40 | */ |
||
41 | public function __construct($id = null, $fields = null) |
||
49 | |||
50 | /** |
||
51 | * Activate model. |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | public function activate() |
||
61 | |||
62 | /** |
||
63 | * Deactivate model. |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function deactivate() |
||
73 | |||
74 | /** |
||
75 | * Internal part of create to avoid problems with static and inheritance |
||
76 | * |
||
77 | * @param $fields |
||
78 | * |
||
79 | * @throws ExceptionFromBitrix |
||
80 | * |
||
81 | * @return static|bool |
||
82 | */ |
||
83 | protected static function internalCreate($fields) |
||
105 | |||
106 | public static function internalDirectCreate($bxObject, $fields) |
||
110 | |||
111 | /** |
||
112 | * Delete model. |
||
113 | * |
||
114 | * @return bool |
||
115 | * @throws ExceptionFromBitrix |
||
116 | */ |
||
117 | public function delete() |
||
132 | |||
133 | /** |
||
134 | * Save model to database. |
||
135 | * |
||
136 | * @param array $selectedFields save only these fields instead of all. |
||
137 | * @return bool |
||
138 | * @throws ExceptionFromBitrix |
||
139 | */ |
||
140 | public function save($selectedFields = []) |
||
164 | |||
165 | /** |
||
166 | * @param $fields |
||
167 | * @param $fieldsSelectedForSave |
||
168 | * @return bool |
||
169 | */ |
||
170 | protected function internalUpdate($fields, $fieldsSelectedForSave) |
||
174 | |||
175 | /** |
||
176 | * Scope to get only active items. |
||
177 | * |
||
178 | * @param BaseQuery $query |
||
179 | * |
||
180 | * @return BaseQuery |
||
181 | */ |
||
182 | public function scopeActive($query) |
||
188 | |||
189 | /** |
||
190 | * Determine whether the field should be stopped from passing to "update". |
||
191 | * |
||
192 | * @param string $field |
||
193 | * @param mixed $value |
||
194 | * @param array $selectedFields |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
212 | |||
213 | /** |
||
214 | * Instantiate bitrix entity object. |
||
215 | * |
||
216 | * @throws LogicException |
||
217 | * |
||
218 | * @return object |
||
219 | */ |
||
220 | public static function instantiateObject() |
||
232 | |||
233 | /** |
||
234 | * Destroy bitrix entity object. |
||
235 | * |
||
236 | * @return void |
||
237 | */ |
||
238 | public static function destroyObject() |
||
242 | |||
243 | /** |
||
244 | * Set eventErrors field on error. |
||
245 | * |
||
246 | * @param bool $result |
||
247 | * @param object $bxObject |
||
248 | */ |
||
249 | protected function setEventErrorsOnFail($result, $bxObject) |
||
255 | |||
256 | /** |
||
257 | * Throw bitrix exception on fail |
||
258 | * |
||
259 | * @param bool $result |
||
260 | * @param object $bxObject |
||
261 | * @throws ExceptionFromBitrix |
||
262 | */ |
||
263 | protected function throwExceptionOnFail($result, $bxObject) |
||
269 | } |
||
270 |
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.