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 Cart 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 Cart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class Cart extends \Model { |
||
58 | |||
59 | public static $logging = false; |
||
60 | public static $objectName = 'Корзины'; |
||
61 | |||
62 | public static function indexes() { |
||
85 | |||
86 | public static function relations() { |
||
147 | |||
148 | public function beforeDelete() { |
||
162 | |||
163 | public static $labels = [ |
||
164 | 'user_id' => 'Пользователь', |
||
165 | 'cart_status_id' => 'Статус', |
||
166 | 'delivery_id' => 'Доставка', |
||
167 | 'comment' => 'Комментарий', |
||
168 | 'bonus_used' => 'Выгодные рубли', |
||
169 | 'complete_data' => 'Время заказа', |
||
170 | 'info' => 'Информация', |
||
171 | 'items' => 'Товары', |
||
172 | 'paytype_id' => 'Способ оплаты', |
||
173 | 'payed' => 'Оплачен', |
||
174 | 'exported' => 'Выгружено', |
||
175 | 'warehouse_block' => 'Блокировка товаров', |
||
176 | 'extra' => 'Доп.', |
||
177 | 'card_item_id' => 'Дисконтная карта', |
||
178 | 'info' => 'Информация', |
||
179 | 'contacts' => 'Информация', |
||
180 | 'pay' => 'Счета', |
||
181 | 'sums' => 'Суммы', |
||
182 | 'deliveryInfo' => 'Для доставки', |
||
183 | 'discount' => 'Скидки', |
||
184 | ]; |
||
185 | public static $cols = [ |
||
186 | //Основные параметры |
||
187 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
188 | 'cart_status_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'status'], |
||
189 | 'delivery_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'delivery'], |
||
190 | 'paytype_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'payType'], |
||
191 | 'card_item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'card'], |
||
192 | 'warehouse_block' => ['type' => 'bool'], |
||
193 | 'payed' => ['type' => 'bool'], |
||
194 | 'comment' => ['type' => 'textarea'], |
||
195 | //Системные |
||
196 | 'exported' => ['type' => 'bool'], |
||
197 | 'complete_data' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
198 | 'date_status' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
199 | 'date_last_activ' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null, 'logging' => false], |
||
200 | 'date_create' => ['type' => 'dateTime'], |
||
201 | //Виджеты |
||
202 | 'sums' => [ |
||
203 | 'type' => 'void', |
||
204 | 'view' => [ |
||
205 | 'type' => 'widget', |
||
206 | 'widget' => 'Ecommerce\adminSums', |
||
207 | ], |
||
208 | ], |
||
209 | 'contacts' => [ |
||
210 | 'type' => 'void', |
||
211 | 'view' => [ |
||
212 | 'type' => 'widget', |
||
213 | 'widget' => 'Ecommerce\admin/contacts', |
||
214 | ], |
||
215 | ], |
||
216 | //Менеджеры |
||
217 | 'extra' => ['type' => 'dataManager', 'relation' => 'extras'], |
||
218 | 'pay' => ['type' => 'dataManager', 'relation' => 'pays'], |
||
219 | 'items' => ['type' => 'dataManager', 'relation' => 'cartItems'], |
||
220 | 'info' => ['type' => 'dataManager', 'relation' => 'infos'], |
||
221 | 'deliveryInfo' => ['type' => 'dataManager', 'relation' => 'deliveryInfos'], |
||
222 | 'discount' => ['type' => 'dataManager', 'relation' => 'discounts'], |
||
223 | ]; |
||
224 | public static $dataManagers = [ |
||
225 | 'manager' => [ |
||
226 | 'cols' => [ |
||
227 | 'contacts', |
||
228 | 'items', |
||
229 | 'extra', |
||
230 | 'discount', |
||
231 | 'sums', |
||
232 | 'cart_status_id', |
||
233 | 'delivery_id', |
||
234 | 'deliveryInfo', |
||
235 | 'payed', |
||
236 | 'pay', |
||
237 | 'complete_data', |
||
238 | ], |
||
239 | 'sortable' => [ |
||
240 | 'cart_status_id', |
||
241 | 'delivery_id', |
||
242 | 'payed', |
||
243 | 'complete_data', |
||
244 | ], |
||
245 | 'filters' => [ |
||
246 | 'cart_status_id', |
||
247 | 'delivery_id', |
||
248 | 'payed', |
||
249 | 'complete_data', |
||
250 | ], |
||
251 | 'preSort' => [ |
||
252 | 'complete_data' => 'desc' |
||
253 | ], |
||
254 | 'actions' => [ |
||
255 | 'Ecommerce\CloseCartBtn', 'Open', 'Edit', 'Delete' |
||
256 | ] |
||
257 | ] |
||
258 | ]; |
||
259 | |||
260 | public static function itemName($item) { |
||
263 | |||
264 | public static $forms = [ |
||
265 | 'manager' => [ |
||
266 | 'inputs' => [ |
||
267 | 'userSearch' => [ |
||
268 | 'type' => 'search', |
||
269 | 'source' => 'relation', |
||
270 | 'relation' => 'user', |
||
271 | 'label' => 'Покупатель', |
||
272 | 'cols' => [ |
||
273 | 'info:first_name', |
||
274 | 'info:last_name', |
||
275 | 'info:middle_name', |
||
276 | 'mail' |
||
277 | ], |
||
278 | 'col' => 'user_id', |
||
279 | 'required' => true, |
||
280 | 'showCol' => [ |
||
281 | 'type' => 'staticMethod', |
||
282 | 'class' => 'Ecommerce\Cart', |
||
283 | 'method' => 'itemName', |
||
284 | ], |
||
285 | ], |
||
286 | 'cardSearch' => [ |
||
287 | 'type' => 'search', |
||
288 | 'source' => 'relation', |
||
289 | 'relation' => 'card', |
||
290 | 'label' => 'Дисконтная карта', |
||
291 | 'cols' => [ |
||
292 | 'code', |
||
293 | 'user:info:first_name', |
||
294 | 'user:info:last_name', |
||
295 | 'user:info:middle_name', |
||
296 | 'user:mail' |
||
297 | ], |
||
298 | 'col' => 'card_item_id', |
||
299 | ], |
||
300 | ], |
||
301 | 'map' => [ |
||
302 | ['userSearch', 'cart_status_id'], |
||
303 | ['paytype_id', 'delivery_id'], |
||
304 | ['cardSearch', 'comment'], |
||
305 | ['warehouse_block', 'complete_data'], |
||
306 | ['payed'], |
||
307 | ['items'], |
||
308 | ['extra'], |
||
309 | ['pay'], |
||
310 | ['info'], |
||
311 | ['deliveryInfo'] |
||
312 | ] |
||
313 | ], |
||
314 | ]; |
||
315 | |||
316 | /** |
||
317 | * @return true|array |
||
318 | */ |
||
319 | public function availablePricesTypes() { |
||
335 | |||
336 | public function buildOrderInfo() { |
||
375 | |||
376 | public function checkStage() { |
||
405 | |||
406 | public function needDelivery() { |
||
414 | |||
415 | public function deliverySum() { |
||
443 | |||
444 | public function hasDiscount() { |
||
447 | |||
448 | public function discountSum() { |
||
455 | |||
456 | public function finalSum() { |
||
462 | |||
463 | public function itemsSum() { |
||
474 | |||
475 | public function addItem($offer_price_id, $count = 1, $final_price = 0) { |
||
506 | |||
507 | public function removeItem($offer_price_id, $count = 1, $final_price = 0) { |
||
534 | |||
535 | public function calc($save = true) { |
||
540 | |||
541 | public function availablePayTypes() { |
||
555 | } |
||
556 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.