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)  | 
            ||
| 208 | |||
| 209 | /**  | 
            ||
| 210 | * Adds <currencies> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies)  | 
            ||
| 211 | *  | 
            ||
| 212 | * @param array $currencies  | 
            ||
| 213 | */  | 
            ||
| 214 | private function addCurrencies(array $currencies)  | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories)  | 
            ||
| 230 | *  | 
            ||
| 231 | * @param array $categories  | 
            ||
| 232 | */  | 
            ||
| 233 | private function addCategories(array $categories)  | 
            ||
| 246 | |||
| 247 | /**  | 
            ||
| 248 | * Adds <delivery-option> element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml)  | 
            ||
| 249 | *  | 
            ||
| 250 | * @param array $deliveries  | 
            ||
| 251 | */  | 
            ||
| 252 | private function addDeliveries(array $deliveries)  | 
            ||
| 265 | |||
| 266 | /**  | 
            ||
| 267 | * Adds <offers> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers)  | 
            ||
| 268 | *  | 
            ||
| 269 | * @param array $offers  | 
            ||
| 270 | */  | 
            ||
| 271 | private function addOffers(array $offers)  | 
            ||
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * @param OfferInterface $offer  | 
            ||
| 287 | */  | 
            ||
| 288 | private function addOfferDeliveryOptions(OfferInterface $offer)  | 
            ||
| 295 | |||
| 296 | /**  | 
            ||
| 297 | * @param OfferInterface $offer  | 
            ||
| 298 | */  | 
            ||
| 299 | private function addOfferParams(OfferInterface $offer)  | 
            ||
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * @param OfferInterface $offer  | 
            ||
| 319 | */  | 
            ||
| 320 | private function addOfferCondition(OfferInterface $offer)  | 
            ||
| 330 | |||
| 331 | /**  | 
            ||
| 332 | * @param string $name  | 
            ||
| 333 | * @param mixed $value  | 
            ||
| 334 | *  | 
            ||
| 335 | * @return bool  | 
            ||
| 336 | */  | 
            ||
| 337 | private function addOfferElement($name, $value)  | 
            ||
| 358 | }  | 
            ||
| 359 | 
If you suppress an error, we recommend checking for the error condition explicitly: