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) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param ShopInfo $shopInfo |
||
| 64 | * @param array $currencies |
||
| 65 | * @param array $categories |
||
| 66 | * @param array $offers |
||
| 67 | * @param array $deliveries |
||
| 68 | * |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | public function generate(ShopInfo $shopInfo, array $currencies, array $categories, array $offers, array $deliveries = []) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Add document header |
||
| 100 | */ |
||
| 101 | protected function addHeader() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Add document footer |
||
| 113 | */ |
||
| 114 | protected function addFooter() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Adds shop element data. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#shop) |
||
| 123 | * |
||
| 124 | * @param ShopInfo $shopInfo |
||
| 125 | */ |
||
| 126 | protected function addShopInfo(ShopInfo $shopInfo) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param Currency $currency |
||
| 137 | */ |
||
| 138 | protected function addCurrency(Currency $currency) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param Category $category |
||
| 148 | */ |
||
| 149 | View Code Duplication | protected function addCategory(Category $category) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param Delivery $delivery |
||
| 164 | */ |
||
| 165 | View Code Duplication | protected function addDelivery(Delivery $delivery) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param OfferInterface $offer |
||
| 178 | */ |
||
| 179 | protected function addOffer(OfferInterface $offer) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Adds <currencies> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies) |
||
| 210 | * |
||
| 211 | * @param array $currencies |
||
| 212 | */ |
||
| 213 | private function addCurrencies(array $currencies) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories) |
||
| 229 | * |
||
| 230 | * @param array $categories |
||
| 231 | */ |
||
| 232 | private function addCategories(array $categories) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Adds <delivery-option> element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml) |
||
| 248 | * |
||
| 249 | * @param array $deliveries |
||
| 250 | */ |
||
| 251 | private function addDeliveries(array $deliveries) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Adds <offers> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers) |
||
| 267 | * |
||
| 268 | * @param array $offers |
||
| 269 | */ |
||
| 270 | private function addOffers(array $offers) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param OfferInterface $offer |
||
| 286 | */ |
||
| 287 | private function addOfferParams(OfferInterface $offer) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param OfferInterface $offer |
||
| 307 | */ |
||
| 308 | private function addOfferCondition(OfferInterface $offer) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $name |
||
| 321 | * @param mixed $value |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | private function addOfferElement($name, $value) |
||
| 346 | } |
||
| 347 |
If you suppress an error, we recommend checking for the error condition explicitly: