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 |
||
26 | class Generator |
||
27 | { |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $tmpFile; |
||
32 | |||
33 | /** |
||
34 | * @var \XMLWriter |
||
35 | */ |
||
36 | private $writer; |
||
37 | |||
38 | /** |
||
39 | * @var Settings |
||
40 | */ |
||
41 | private $settings; |
||
42 | |||
43 | /** |
||
44 | * Generator constructor. |
||
45 | * |
||
46 | * @param Settings $settings |
||
47 | */ |
||
48 | public function __construct($settings = null) |
||
69 | |||
70 | /** |
||
71 | * @param ShopInfo $shopInfo |
||
72 | * @param array $currencies |
||
73 | * @param array $categories |
||
74 | * @param array $offers |
||
75 | * @param array $deliveries |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function generate(ShopInfo $shopInfo, array $currencies, array $categories, array $offers, array $deliveries = []) |
||
109 | |||
110 | /** |
||
111 | * Add document header |
||
112 | */ |
||
113 | protected function addHeader() |
||
122 | |||
123 | /** |
||
124 | * Add document footer |
||
125 | */ |
||
126 | protected function addFooter() |
||
132 | |||
133 | /** |
||
134 | * Adds shop element data. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#shop) |
||
135 | * |
||
136 | * @param ShopInfo $shopInfo |
||
137 | */ |
||
138 | protected function addShopInfo(ShopInfo $shopInfo) |
||
146 | |||
147 | /** |
||
148 | * @param Currency $currency |
||
149 | */ |
||
150 | protected function addCurrency(Currency $currency) |
||
157 | |||
158 | /** |
||
159 | * @param Category $category |
||
160 | */ |
||
161 | View Code Duplication | protected function addCategory(Category $category) |
|
173 | |||
174 | /** |
||
175 | * @param Delivery $delivery |
||
176 | */ |
||
177 | View Code Duplication | protected function addDelivery(Delivery $delivery) |
|
187 | |||
188 | /** |
||
189 | * @param OfferInterface $offer |
||
190 | */ |
||
191 | protected function addOffer(OfferInterface $offer) |
||
220 | |||
221 | /** |
||
222 | * Adds <currencies> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies) |
||
223 | * |
||
224 | * @param array $currencies |
||
225 | */ |
||
226 | private function addCurrencies(array $currencies) |
||
239 | |||
240 | /** |
||
241 | * Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories) |
||
242 | * |
||
243 | * @param array $categories |
||
244 | */ |
||
245 | private function addCategories(array $categories) |
||
258 | |||
259 | /** |
||
260 | * Adds <delivery-option> element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml) |
||
261 | * |
||
262 | * @param array $deliveries |
||
263 | */ |
||
264 | private function addDeliveries(array $deliveries) |
||
277 | |||
278 | /** |
||
279 | * Adds <offers> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers) |
||
280 | * |
||
281 | * @param array $offers |
||
282 | */ |
||
283 | private function addOffers(array $offers) |
||
296 | |||
297 | /** |
||
298 | * @param OfferInterface $offer |
||
299 | */ |
||
300 | private function addOfferDeliveryOptions(OfferInterface $offer) |
||
307 | |||
308 | /** |
||
309 | * @param OfferInterface $offer |
||
310 | */ |
||
311 | private function addOfferParams(OfferInterface $offer) |
||
328 | |||
329 | /** |
||
330 | * @param OfferInterface $offer |
||
331 | */ |
||
332 | private function addOfferCondition(OfferInterface $offer) |
||
342 | |||
343 | /** |
||
344 | * @param string $name |
||
345 | * @param mixed $value |
||
346 | * |
||
347 | * @return bool |
||
348 | */ |
||
349 | private function addOfferElement($name, $value) |
||
370 | } |
||
371 |
If you suppress an error, we recommend checking for the error condition explicitly: