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 |
||
41 | class ElementModel extends BitrixModel |
||
42 | { |
||
43 | /** |
||
44 | * Corresponding IBLOCK_ID |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | const IBLOCK_ID = null; |
||
49 | |||
50 | /** |
||
51 | * Bitrix entity object. |
||
52 | * |
||
53 | * @var object |
||
54 | */ |
||
55 | public static $bxObject; |
||
56 | |||
57 | /** |
||
58 | * Corresponding object class name. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected static $objectClass = 'CIBlockElement'; |
||
63 | |||
64 | /** |
||
65 | * Have sections been already fetched from DB? |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | protected $sectionsAreFetched = false; |
||
70 | |||
71 | /** |
||
72 | * Getter for corresponding iblock id. |
||
73 | * |
||
74 | * @throws Exception |
||
75 | * |
||
76 | * @return int |
||
77 | */ |
||
78 | public static function iblockId() |
||
87 | |||
88 | /** |
||
89 | * Create new item in database. |
||
90 | * |
||
91 | * @param $fields |
||
92 | * |
||
93 | * @throws Exception |
||
94 | * |
||
95 | * @return static|bool |
||
96 | */ |
||
97 | public static function create($fields) |
||
105 | |||
106 | /** |
||
107 | * Corresponding section model full qualified class name. |
||
108 | * MUST be overridden if you are going to use section model for this iblock. |
||
109 | * |
||
110 | * @throws Exception |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public static function sectionModel() |
||
118 | |||
119 | /** |
||
120 | * Instantiate a query object for the model. |
||
121 | * |
||
122 | * @return ElementQuery |
||
123 | */ |
||
124 | public static function query() |
||
128 | |||
129 | /** |
||
130 | * Scope to sort by date. |
||
131 | * |
||
132 | * @param ElementQuery $query |
||
133 | * @param string $sort |
||
134 | * |
||
135 | * @return ElementQuery |
||
136 | */ |
||
137 | public function scopeSortByDate($query, $sort = 'DESC') |
||
141 | |||
142 | /** |
||
143 | * Scope to get only items from a given section. |
||
144 | * |
||
145 | * @param ElementQuery $query |
||
146 | * @param mixed $id |
||
147 | * |
||
148 | * @return ElementQuery |
||
149 | */ |
||
150 | public function scopeFromSectionWithId($query, $id) |
||
156 | |||
157 | /** |
||
158 | * Scope to get only items from a given section. |
||
159 | * |
||
160 | * @param ElementQuery $query |
||
161 | * @param string $code |
||
162 | * |
||
163 | * @return ElementQuery |
||
164 | */ |
||
165 | public function scopeFromSectionWithCode($query, $code) |
||
171 | |||
172 | /** |
||
173 | * Fill extra fields when $this->field is called. |
||
174 | * |
||
175 | * @return null |
||
176 | */ |
||
177 | protected function afterFill() |
||
181 | |||
182 | /** |
||
183 | * Load all model attributes from cache or database. |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function load() |
||
193 | |||
194 | /** |
||
195 | * Get element's sections from cache or database. |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | public function getSections() |
||
207 | |||
208 | /** |
||
209 | * Refresh model from database and place data to $this->fields. |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function refresh() |
||
217 | |||
218 | /** |
||
219 | * Refresh element's fields and save them to a class field. |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | View Code Duplication | public function refreshFields() |
|
241 | |||
242 | /** |
||
243 | * Refresh element's sections and save them to a class field. |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public function refreshSections() |
||
258 | |||
259 | /** |
||
260 | * @deprecated in favour of `->section()` |
||
261 | * Get element direct section as ID or array of fields. |
||
262 | * |
||
263 | * @param bool $load |
||
264 | * |
||
265 | * @return false|int|array |
||
266 | */ |
||
267 | public function getSection($load = false) |
||
282 | |||
283 | /** |
||
284 | * Get element direct section as model object. |
||
285 | * |
||
286 | * @param bool $load |
||
287 | * |
||
288 | * @return false|SectionModel |
||
289 | */ |
||
290 | public function section($load = false) |
||
301 | |||
302 | /** |
||
303 | * Proxy for GetPanelButtons |
||
304 | * |
||
305 | * @param array $options |
||
306 | * @return array |
||
307 | */ |
||
308 | public function getPanelButtons($options = []) |
||
317 | |||
318 | /** |
||
319 | * Save props to database. |
||
320 | * If selected is not empty then only props from it are saved. |
||
321 | * |
||
322 | * @param array $selected |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | public function saveProps($selected = []) |
||
342 | |||
343 | /** |
||
344 | * Normalize properties's format converting it to 'PROPERTY_"CODE"_VALUE'. |
||
345 | * |
||
346 | * @return null |
||
347 | */ |
||
348 | protected function normalizePropertyFormat() |
||
362 | |||
363 | /** |
||
364 | * Construct 'PROPERTY_VALUES' => [...] from flat fields array. |
||
365 | * This is used in save. |
||
366 | * If $selectedFields are specified only those are saved. |
||
367 | * |
||
368 | * @param $selectedFields |
||
369 | * |
||
370 | * @return array |
||
371 | */ |
||
372 | protected function constructPropertyValuesForSave($selectedFields = []) |
||
388 | } |
||
389 |
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.