Complex classes like Product 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 Product, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Product extends Entity |
||
9 | { |
||
10 | use StandardEntity; |
||
11 | |||
12 | /** @var Discussion */ |
||
13 | protected $discussion = null; |
||
14 | |||
15 | 62 | public function __construct(array $data) |
|
28 | |||
29 | /** |
||
30 | * Should always return "product" |
||
31 | * @return string |
||
32 | */ |
||
33 | 2 | public function getType() |
|
37 | |||
38 | /** |
||
39 | * Text description, if available, of the product |
||
40 | * @return string|null |
||
41 | */ |
||
42 | 2 | public function getText() |
|
46 | |||
47 | /** |
||
48 | * Regular or original price of the product, if available. |
||
49 | * If not available, offerPrice is returned instead. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | 2 | public function getRegularPrice() |
|
58 | |||
59 | /** |
||
60 | * regularPrice separated into its constituent parts: amount, symbol, and full text. |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | 2 | public function getRegularPriceDetails() |
|
69 | |||
70 | /** |
||
71 | * Returns the shipping amount, if available. If not available, returns null. |
||
72 | * @return string|null |
||
73 | */ |
||
74 | 2 | public function getShippingAmount() |
|
78 | |||
79 | /** |
||
80 | * Discount or amount saved off the regular price. |
||
81 | * Recommended to use getSaveAmountDetails instead |
||
82 | * @see getSaveAmountDetails() |
||
83 | * @return string|null |
||
84 | */ |
||
85 | 2 | public function getSaveAmount() |
|
89 | |||
90 | /** |
||
91 | * saveAmount separated into its constituent parts: amount, symbol, full text, |
||
92 | * and whether or not it is a percentage value. |
||
93 | * @return array |
||
94 | */ |
||
95 | 2 | public function getSaveAmountDetails() |
|
99 | |||
100 | /** |
||
101 | * Diffbot-determined unique product ID. If upc, isbn, mpn or sku are identified on the page, |
||
102 | * productId will select from these values in the above order. |
||
103 | * @return string|null |
||
104 | */ |
||
105 | 2 | public function getProductId() |
|
109 | |||
110 | /** |
||
111 | * Universal Product Code (UPC/EAN), if available. |
||
112 | * @return string|null |
||
113 | */ |
||
114 | 2 | public function getUpc() |
|
118 | |||
119 | /** |
||
120 | * Manufacturer's Product Number. |
||
121 | * @return string|null |
||
122 | */ |
||
123 | 2 | public function getMpn() |
|
127 | |||
128 | /** |
||
129 | * International Standard Book Number (ISBN), if available. |
||
130 | * @return string|null |
||
131 | */ |
||
132 | 2 | public function getIsbn() |
|
136 | |||
137 | /** |
||
138 | * If a specifications table or similar data is available on the product page, |
||
139 | * individual specifications will be returned in the specs object as name/value pairs. |
||
140 | * Names will be normalized to lowercase with spaces replaced by underscores, e.g. display_resolution. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 2 | public function getSpecs() |
|
148 | |||
149 | /** |
||
150 | * Returns an array of images found in the page's content. |
||
151 | * |
||
152 | * Note that this (tries) to ignore content-unrelated images like ads arounds the page, etc. |
||
153 | * The format of the array will be: |
||
154 | * |
||
155 | * [ |
||
156 | * { |
||
157 | * "height": 808, |
||
158 | * "diffbotUri": "image|3|-543943368", |
||
159 | * "naturalHeight": 808, |
||
160 | * "width": 717, |
||
161 | * "primary": true, |
||
162 | * "naturalWidth": 717, |
||
163 | * "url": "https://example.com/image1.png" |
||
164 | * }, |
||
165 | * { |
||
166 | * "height": 506, |
||
167 | * "diffbotUri": "image|3|-844014913", |
||
168 | * "naturalHeight": 506, |
||
169 | * "width": 715, |
||
170 | * "naturalWidth": 715, |
||
171 | * "url": "https://example.com/image1.jpeg" |
||
172 | * } |
||
173 | * ] |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | 2 | public function getImages() |
|
181 | |||
182 | /** |
||
183 | * Country of origin as identified by UPC/ISBN. Null if not present. |
||
184 | * @return string|null |
||
185 | */ |
||
186 | 2 | public function getPrefixCode() |
|
190 | |||
191 | /** |
||
192 | * If available, two-character ISO country code where the product was produced. Null if not present. |
||
193 | * @return string|null |
||
194 | */ |
||
195 | 2 | public function getProductOrigin() |
|
199 | |||
200 | /** |
||
201 | * If the product is available in a range of prices, the minimum and maximum values will be returned. |
||
202 | * The lowest price will also be returned as the offerPrice. |
||
203 | * @return array|null |
||
204 | */ |
||
205 | 2 | public function getPriceRange() |
|
209 | |||
210 | /** |
||
211 | * If the product is available with quantity-based discounts, all identifiable price points will be returned. |
||
212 | * The lowest price will also be returned as the offerPrice. |
||
213 | * |
||
214 | * @return array|null |
||
215 | */ |
||
216 | 2 | public function getQuantityPrices() |
|
220 | |||
221 | /** |
||
222 | * Checks if the product has been determined available. Null if it wasn't determined. |
||
223 | * @return bool|null |
||
224 | */ |
||
225 | 2 | public function isAvailable() |
|
229 | |||
230 | /** |
||
231 | * Offer or actual/final price of the product. |
||
232 | * @return string | null |
||
233 | */ |
||
234 | 2 | public function getOfferPrice() |
|
238 | |||
239 | /** |
||
240 | * Size(s) available, if identified on the page. |
||
241 | * @return null|array |
||
242 | */ |
||
243 | 2 | public function getSize() |
|
247 | |||
248 | /** |
||
249 | * Returns array of product color options. |
||
250 | * @return null|array |
||
251 | */ |
||
252 | 2 | public function getColors() |
|
256 | |||
257 | /** |
||
258 | * Returns the brand, as determined by Diffbot |
||
259 | * @return string |
||
260 | */ |
||
261 | 2 | public function getBrand() |
|
265 | |||
266 | /** |
||
267 | * Returns Stock Keeping Unit -- store/vendor inventory number or identifier. |
||
268 | * @return string |
||
269 | */ |
||
270 | 2 | public function getSku() |
|
274 | |||
275 | /** |
||
276 | * offerPrice separated into its constituent parts: amount, symbol, and full text. |
||
277 | * @return array |
||
278 | */ |
||
279 | 3 | public function getOfferPriceDetails() |
|
283 | |||
284 | /** |
||
285 | * Returns the Discussion entity - comments of the product |
||
286 | * @return Discussion |
||
287 | */ |
||
288 | 2 | public function getDiscussion() |
|
292 | |||
293 | } |
||
294 |