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 Generator 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 Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Generator |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $tmpFile; |
||
31 | |||
32 | /** |
||
33 | * @var \XMLWriter |
||
34 | */ |
||
35 | private $writer; |
||
36 | |||
37 | /** |
||
38 | * @var Settings |
||
39 | */ |
||
40 | private $settings; |
||
41 | |||
42 | /** |
||
43 | * Generator constructor. |
||
44 | * |
||
45 | * @param Settings $settings |
||
46 | */ |
||
47 | public function __construct($settings = null) |
||
60 | |||
61 | /** |
||
62 | * @param ShopInfo $shopInfo |
||
63 | * @param array $currencies |
||
64 | * @param array $categories |
||
65 | * @param array $offers |
||
66 | * @param array $deliveries |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function generate(ShopInfo $shopInfo, array $currencies, array $categories, array $offers, array $deliveries = []) |
||
96 | |||
97 | /** |
||
98 | * Add document header |
||
99 | */ |
||
100 | protected function addHeader() |
||
109 | |||
110 | /** |
||
111 | * Add document footer |
||
112 | */ |
||
113 | protected function addFooter() |
||
119 | |||
120 | /** |
||
121 | * Adds shop element data. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#shop) |
||
122 | * |
||
123 | * @param ShopInfo $shopInfo |
||
124 | */ |
||
125 | protected function addShopInfo(ShopInfo $shopInfo) |
||
133 | |||
134 | /** |
||
135 | * @param Currency $currency |
||
136 | */ |
||
137 | protected function addCurrency(Currency $currency) |
||
144 | |||
145 | /** |
||
146 | * @param Category $category |
||
147 | */ |
||
148 | View Code Duplication | protected function addCategory(Category $category) |
|
160 | |||
161 | /** |
||
162 | * @param Delivery $delivery |
||
163 | */ |
||
164 | View Code Duplication | protected function addDelivery(Delivery $delivery) |
|
174 | |||
175 | /** |
||
176 | * @param OfferInterface $offer |
||
177 | */ |
||
178 | protected function addOffer(OfferInterface $offer) |
||
205 | |||
206 | /** |
||
207 | * Adds <currencies> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies) |
||
208 | * |
||
209 | * @param array $currencies |
||
210 | */ |
||
211 | private function addCurrencies(array $currencies) |
||
224 | |||
225 | /** |
||
226 | * Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories) |
||
227 | * |
||
228 | * @param array $categories |
||
229 | */ |
||
230 | private function addCategories(array $categories) |
||
243 | |||
244 | /** |
||
245 | * Adds <delivery-option> element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml) |
||
246 | * |
||
247 | * @param array $deliveries |
||
248 | */ |
||
249 | private function addDeliveries(array $deliveries) |
||
262 | |||
263 | /** |
||
264 | * Adds <offers> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers) |
||
265 | * |
||
266 | * @param array $offers |
||
267 | */ |
||
268 | private function addOffers(array $offers) |
||
281 | |||
282 | /** |
||
283 | * @param OfferInterface $offer |
||
284 | */ |
||
285 | private function addOfferParams(OfferInterface $offer) |
||
302 | |||
303 | /** |
||
304 | * @param string $name |
||
305 | * @param mixed $value |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | private function addOfferElement($name, $value) |
||
322 | } |
||
323 |
If you suppress an error, we recommend checking for the error condition explicitly: