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 |
||
44 | class SectionModel extends BitrixModel |
||
45 | { |
||
46 | /** |
||
47 | * Corresponding IBLOCK_ID |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | const IBLOCK_ID = null; |
||
52 | |||
53 | /** |
||
54 | * Bitrix entity object. |
||
55 | * |
||
56 | * @var object |
||
57 | */ |
||
58 | public static $bxObject; |
||
59 | |||
60 | /** |
||
61 | * Corresponding object class name. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected static $objectClass = 'CIBlockSection'; |
||
66 | |||
67 | /** |
||
68 | * Recalculate LEFT_MARGIN and RIGHT_MARGIN during add/update ($bResort for CIBlockSection::Add/Update). |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected static $resort = true; |
||
73 | |||
74 | /** |
||
75 | * Update search after each create or update. |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected static $updateSearch = true; |
||
80 | |||
81 | /** |
||
82 | * Resize pictures during add/update ($bResizePictures for CIBlockSection::Add/Update). |
||
83 | * |
||
84 | * @var bool |
||
85 | */ |
||
86 | protected static $resizePictures = false; |
||
87 | |||
88 | /** |
||
89 | * Getter for corresponding iblock id. |
||
90 | * |
||
91 | * @throws LogicException |
||
92 | * |
||
93 | * @return int |
||
94 | */ |
||
95 | public static function iblockId() |
||
104 | |||
105 | /** |
||
106 | * Instantiate a query object for the model. |
||
107 | * |
||
108 | * @return SectionQuery |
||
109 | */ |
||
110 | public static function query() |
||
114 | |||
115 | /** |
||
116 | * Create new item in database. |
||
117 | * |
||
118 | * @param $fields |
||
119 | * |
||
120 | * @throws ExceptionFromBitrix |
||
121 | * |
||
122 | * @return static|bool |
||
123 | */ |
||
124 | View Code Duplication | public static function create($fields) |
|
|
|||
125 | { |
||
126 | if (!isset($fields['IBLOCK_ID'])) { |
||
127 | $fields['IBLOCK_ID'] = static::iblockId(); |
||
128 | } |
||
129 | |||
130 | return static::internalCreate($fields); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get IDs of direct children of the section. |
||
135 | * Additional filter can be specified. |
||
136 | * |
||
137 | * @param array $filter |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getDirectChildren(array $filter = []) |
||
153 | |||
154 | /** |
||
155 | * Get IDs of all children of the section (direct or not). |
||
156 | * Additional filter can be specified. |
||
157 | * |
||
158 | * @param array $filter |
||
159 | * @param array|string $sort |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC']) |
||
184 | |||
185 | /** |
||
186 | * Proxy for GetPanelButtons |
||
187 | * |
||
188 | * @param array $options |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getPanelButtons($options = []) |
||
200 | |||
201 | public static function internalDirectCreate($bxObject, $fields) |
||
205 | |||
206 | /** |
||
207 | * @param $fields |
||
208 | * @param $fieldsSelectedForSave |
||
209 | * @return bool |
||
210 | */ |
||
211 | protected function internalUpdate($fields, $fieldsSelectedForSave) |
||
215 | |||
216 | /** |
||
217 | * @param $value |
||
218 | */ |
||
219 | public static function setResort($value) |
||
223 | |||
224 | /** |
||
225 | * @param $value |
||
226 | */ |
||
227 | public static function setUpdateSearch($value) |
||
231 | |||
232 | /** |
||
233 | * @param $value |
||
234 | */ |
||
235 | public static function setResizePictures($value) |
||
239 | |||
240 | /** |
||
241 | * @param $query |
||
242 | * @param SectionModel $section |
||
243 | * @return SectionQuery |
||
244 | */ |
||
245 | public function scopeChildrenOf(SectionQuery $query, SectionModel $section) |
||
253 | |||
254 | /** |
||
255 | * @param $query |
||
256 | * @param SectionModel|int $section |
||
257 | * @return SectionQuery |
||
258 | */ |
||
259 | public function scopeDirectChildrenOf(SectionQuery $query, $section) |
||
265 | } |
||
266 |
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.