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 |
||
| 24 | class Generator |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $tmpFile; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \XMLWriter |
||
| 33 | */ |
||
| 34 | private $writer; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Settings |
||
| 38 | */ |
||
| 39 | private $settings; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Generator constructor. |
||
| 43 | * |
||
| 44 | * @param Settings $settings |
||
| 45 | */ |
||
| 46 | public function __construct($settings = null) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param ShopInfo $shopInfo |
||
| 62 | * @param array $currencies |
||
| 63 | * @param array $categories |
||
| 64 | * @param array $offers |
||
| 65 | * @param array $deliveries |
||
| 66 | * |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | public function generate(ShopInfo $shopInfo, array $currencies, array $categories, array $offers, array $deliveries = []) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Add document header |
||
| 98 | */ |
||
| 99 | protected function addHeader() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Add document footer |
||
| 111 | */ |
||
| 112 | protected function addFooter() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Adds shop element data. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#shop) |
||
| 121 | * |
||
| 122 | * @param ShopInfo $shopInfo |
||
| 123 | */ |
||
| 124 | protected function addShopInfo(ShopInfo $shopInfo) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param Currency $currency |
||
| 135 | */ |
||
| 136 | protected function addCurrency(Currency $currency) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param Category $category |
||
| 146 | */ |
||
| 147 | View Code Duplication | protected function addCategory(Category $category) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param Delivery $delivery |
||
| 162 | */ |
||
| 163 | View Code Duplication | protected function addDelivery(Delivery $delivery) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param OfferInterface $offer |
||
| 176 | */ |
||
| 177 | protected function addOffer(OfferInterface $offer) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Adds <currencies> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies) |
||
| 203 | * |
||
| 204 | * @param array $currencies |
||
| 205 | */ |
||
| 206 | private function addCurrencies(array $currencies) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories) |
||
| 222 | * |
||
| 223 | * @param array $categories |
||
| 224 | */ |
||
| 225 | private function addCategories(array $categories) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Adds <delivery-option> element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml) |
||
| 241 | * |
||
| 242 | * @param array $deliveries |
||
| 243 | */ |
||
| 244 | private function addDeliveries(array $deliveries) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds <offers> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers) |
||
| 260 | * |
||
| 261 | * @param array $offers |
||
| 262 | */ |
||
| 263 | private function addOffers(array $offers) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param OfferInterface $offer |
||
| 279 | */ |
||
| 280 | private function addOfferParams(OfferInterface $offer) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $name |
||
| 300 | * @param mixed $value |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | private function addOfferElement($name, $value) |
||
| 317 | } |
||
| 318 |
If you suppress an error, we recommend checking for the error condition explicitly: