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 CalculatePriceDeliveryCdek 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 CalculatePriceDeliveryCdek, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CalculatePriceDeliveryCdek { |
||
15 | |||
16 | //версия модуля |
||
17 | private $version = "1.0"; |
||
18 | //url для получения данных по отправке |
||
19 | private $jsonUrl = 'http://api.cdek.ru/calculator/calculate_price_by_json.php'; |
||
20 | |||
21 | //авторизация ИМ |
||
22 | private $authLogin; |
||
23 | private $authPassword; |
||
24 | |||
25 | //id города-отправителя |
||
26 | private $senderCityId; |
||
27 | //id города-получателя |
||
28 | private $receiverCityId; |
||
29 | //id тарифа |
||
30 | private $tariffId; |
||
31 | //id способа доставки (склад-склад, склад-дверь) |
||
32 | private $modeId; |
||
33 | //массив мест отправления |
||
34 | public $goodsList; |
||
35 | //массив id тарифов |
||
36 | public $tariffList; |
||
37 | //результат расчёта стоимости отправления ИМ |
||
38 | private $result; |
||
39 | //результат в случае ошибочного расчёта |
||
40 | private $error; |
||
41 | //планируемая дата заказа |
||
42 | public $dateExecute; |
||
43 | |||
44 | /** |
||
45 | * конструктор |
||
46 | */ |
||
47 | public function __construct() { |
||
50 | |||
51 | /** |
||
52 | * Установка планируемой даты отправки |
||
53 | * |
||
54 | * @param string $date дата планируемой отправки, например '2012-06-25' |
||
55 | */ |
||
56 | public function setDateExecute($date) { |
||
59 | |||
60 | /** |
||
61 | * Авторизация ИМ |
||
62 | * |
||
63 | * @param string $authLogin логин |
||
64 | * @param string $authPassword пароль |
||
65 | */ |
||
66 | public function setAuth($authLogin, $authPassword) { |
||
70 | |||
71 | /** |
||
72 | * Защифрованный пароль для передачи на сервер |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | private function _getSecureAuthPassword() { |
||
79 | |||
80 | /** |
||
81 | * Город-отправитель |
||
82 | * |
||
83 | * @param int $id города |
||
84 | */ |
||
85 | public function setSenderCityId($id) { |
||
92 | |||
93 | /** |
||
94 | * Город-получатель |
||
95 | * |
||
96 | * @param int $id города |
||
97 | */ |
||
98 | public function setReceiverCityId($id) { |
||
105 | |||
106 | /** |
||
107 | * Устанавливаем тариф |
||
108 | * |
||
109 | * @param int $id тарифа |
||
110 | */ |
||
111 | public function setTariffId($id) { |
||
118 | |||
119 | /** |
||
120 | * Устанавливаем режим доставки (дверь-дверь=1, дверь-склад=2, склад-дверь=3, склад-склад=4) |
||
121 | * |
||
122 | * @param int $id режим доставки |
||
123 | */ |
||
124 | public function setModeDeliveryId($id) { |
||
131 | |||
132 | /** |
||
133 | * Добавление места в отправлении |
||
134 | * |
||
135 | * @param int $weight вес, килограммы |
||
136 | * @param int $length длина, сантиметры |
||
137 | * @param int $width ширина, сантиметры |
||
138 | * @param int $height высота, сантиметры |
||
139 | */ |
||
140 | public function addGoodsItemBySize($weight, $length, $width, $height) { |
||
161 | |||
162 | /** |
||
163 | * Добавление места в отправлении по объёму (куб.метры) |
||
164 | * |
||
165 | * @param int $weight вес, килограммы |
||
166 | * @param int $volume объёмный вес, метры кубические (А * В * С) |
||
167 | */ |
||
168 | public function addGoodsItemByVolume($weight, $volume) { |
||
180 | |||
181 | /** |
||
182 | * Получение массива мест отправления |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | public function getGoodslist() { |
||
192 | |||
193 | /** |
||
194 | * добавление тарифа в список тарифов с приоритетами |
||
195 | * |
||
196 | * @param int $id тариф |
||
197 | * @param int $priority default false приоритет |
||
198 | */ |
||
199 | public function addTariffPriority($id, $priority = 0) { |
||
208 | |||
209 | /** |
||
210 | * Получение массива заданных тарифов |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | private function _getTariffList() { |
||
220 | |||
221 | /** |
||
222 | * Выполнение POST-запроса на сервер для получения данных |
||
223 | * по запрашиваемым параметрам. |
||
224 | * |
||
225 | * |
||
226 | */ |
||
227 | private function _getRemoteData($data) { |
||
244 | |||
245 | /** |
||
246 | * Расчет стоимости доставки |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function calculate() { |
||
314 | |||
315 | /** |
||
316 | * Получить результаты подсчета |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | public function getResult() { |
||
323 | |||
324 | /** |
||
325 | * получить код и текст ошибки |
||
326 | * |
||
327 | * @return object |
||
328 | */ |
||
329 | public function getError() { |
||
332 | |||
333 | } |
||
334 | |||
335 | ?> |
||
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.