Complex classes like Prestashop 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 Prestashop, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Prestashop implements MessageComponentInterface |
||
10 | { |
||
11 | protected $clients; |
||
12 | protected $dbConn; |
||
13 | |||
14 | public function __construct() |
||
27 | |||
28 | public function onOpen(ConnectionInterface $conn) |
||
35 | |||
36 | public function onMessage(ConnectionInterface $from, $msg) |
||
58 | |||
59 | /** |
||
60 | * @param string $data |
||
61 | */ |
||
62 | private function getCombinationData($data) { |
||
78 | |||
79 | /** |
||
80 | * @param string $data |
||
81 | */ |
||
82 | private function getProductData($data) { |
||
83 | $category = (int) substr($data, 0, strpos($data, ',')); |
||
84 | if ($category != 0) { |
||
85 | $products = $this->getProducts($category); |
||
86 | } else { |
||
87 | $product = substr($data, strpos($data, ',') + 1); |
||
88 | $products = $this->getProducts($product); |
||
89 | } |
||
90 | Analog::log("Product variables: $data"); |
||
91 | return $products; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string $data |
||
96 | */ |
||
97 | private function getCartData($data) { |
||
98 | $cart = substr($data, 0, strpos($data, ',')); |
||
99 | $cust = substr($data, strpos($data, ',') + 1); |
||
100 | |||
101 | Analog::log("Cart & customer variables: $data"); |
||
102 | $otherCarts = $this->processFindCarts($cart, $cust); |
||
103 | return $otherCarts; |
||
104 | } |
||
105 | |||
106 | |||
107 | /** |
||
108 | * @param string $cart |
||
109 | * @param string $cust |
||
110 | */ |
||
111 | private function processFindCarts($cart, $cust) |
||
112 | { |
||
113 | $sql = 'SELECT DISTINCT pc.id_cart as id, DATE_FORMAT(pc.date_upd,"%a %D %b %Y, %l:%i %p") as timer from '._DB_PREFIX_.'cart as pc |
||
114 | LEFT JOIN '._DB_PREFIX_.'cart_product as pcp on pcp.id_cart = pc.id_cart |
||
115 | WHERE pc.id_cart NOT IN (SELECT po.id_cart FROM '._DB_PREFIX_.'orders as po) |
||
116 | AND pcp.id_product IS NOT NULL |
||
117 | AND pc.id_customer = '.(int) $cust.' |
||
118 | AND pc.id_cart != '.(int) $cart.' |
||
119 | ORDER BY pc.date_upd DESC |
||
120 | LIMIT 10'; |
||
121 | if ($results = $this->dbConn->fetchRowMany($sql)) { |
||
122 | foreach ($results as &$row) { |
||
123 | $row['token'] = md5(_COOKIE_KEY_.'recover_cart_'.$row['id']); |
||
124 | } |
||
125 | |||
126 | return $results; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param string $product |
||
133 | */ |
||
134 | private function getAttribute($product, $choices) |
||
135 | { |
||
136 | $sql = 'SELECT pac.id_product_attribute from '._DB_PREFIX_.'product_attribute_combination as pac |
||
137 | LEFT JOIN '._DB_PREFIX_.'product_attribute as ppa on ppa.id_product_attribute = pac.id_product_attribute |
||
138 | LEFT JOIN '._DB_PREFIX_.'attribute a ON (a.id_attribute = pac.id_attribute) |
||
139 | WHERE ppa.id_product = '.$product; |
||
140 | foreach ($choices as $value) { |
||
141 | $sql .= ' AND pac.id_product_attribute IN |
||
142 | ( SELECT pac.id_product_attribute from '._DB_PREFIX_.'product_attribute_combination as pac |
||
143 | WHERE pac.id_attribute = '.(int) $value.') '; |
||
144 | } |
||
145 | $sql .= ' GROUP BY pac.id_product_attribute'; |
||
146 | $id_product_attribute = $this->dbConn->fetchColumn($sql); |
||
147 | |||
148 | return $id_product_attribute; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param null|string $id_product_attribute |
||
153 | */ |
||
154 | private function buildAttributes($combinations, $id_product_attribute, $row) |
||
155 | { |
||
156 | $combinationSet = array(); |
||
157 | $specific_price = null; |
||
158 | |||
159 | $combinations['name'] = $row['product_name']; |
||
160 | $typcheck = array("id_product", "price", "base_price", "price", "ecotax", "weight", "quantity", "unit_impact", "minimal_quantity"); |
||
161 | |||
162 | foreach ($typcheck as $key=>$value) |
||
163 | { |
||
164 | ((strpos($value, 'price') !== false) || (strpos($value, 'weight') !== false) || (strpos($value, 'ecotax') !== false) || (strpos($value, 'impact') !== false)) ? $combinations[$value] = (float) $row[$value] : $combinations[$value] = (int) $row[$value]; |
||
165 | |||
166 | } |
||
167 | $combinations['attributes_values'][$row['id_attribute_group']] = $row['attribute_name']; |
||
168 | $combinations['attributes'][] = (int) $row['id_attribute']; |
||
169 | |||
170 | list ($combinationSet[(int) $row['id_product_attribute']], $combinations['specific_price']) = $this->getCombinationSpecificPrice($combinationSet, $row, $id_product_attribute); |
||
171 | |||
172 | $combinations['reference'] = $row['reference']; |
||
173 | $combinations['available_date'] = $this->getAvailableDate($row); |
||
174 | $combinations['image'] = $this->getComboImage($id_product_attribute); |
||
175 | $combinations['final_price'] = $this->getFinalPrice($row, $specific_price); |
||
176 | |||
177 | return $combinations; |
||
178 | } |
||
179 | |||
180 | private function getFinalPrice($row, $specific_price) |
||
181 | { |
||
182 | $specific_price['price'] != 0 ? $final_price = (((float) $row['base_price'] + (float) $row['price']) * (((int) 100 - $specific_price['reduction_percent']) / 100)) : $final_price = (float) $row['base_price'] + (float) $row['price']; |
||
183 | return $final_price; |
||
184 | } |
||
185 | |||
186 | |||
187 | private function getAvailableDate($row) { |
||
188 | ($row['available_date'] != '0000-00-00') ? $dater = $row['available_date'] : $dater = ''; |
||
189 | return $dater; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param null|string $id_product_attribute |
||
194 | */ |
||
195 | private function getCombinationSpecificPrice($combinationSet, $row, $id_product_attribute) { |
||
196 | // Call getSpecificPrice in order to set $combination_specific_price |
||
197 | if (!isset($combinationSet[(int) $row['id_product_attribute']])) { |
||
198 | $specific_price = $this->getSpecificPrice($id_product_attribute, $row['id_product']); |
||
199 | $combinationSet[(int) $row['id_product_attribute']] = true; |
||
200 | return array($combinationSet, $specific_price); |
||
201 | } else { |
||
202 | return array(false, null); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | |||
207 | |||
208 | /** |
||
209 | * @param null|string $id_product_attribute |
||
210 | */ |
||
211 | private function getCombination($id_product_attribute) |
||
212 | { |
||
213 | $sql = 'SELECT ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, agl.`public_name` AS public_group_name, |
||
214 | a.`id_attribute`, al.`name` AS attribute_name, a.`color` AS attribute_color, pas.`id_product_attribute`, |
||
215 | IFNULL(stock.quantity, 0) as quantity, pc.price as base_price, pc.id_product, pas.`price`, pas.`ecotax`, pas.`weight`, |
||
216 | pas.`default_on`, pa.`reference`, pas.`unit_price_impact`, pl.name as product_name, |
||
217 | pas.`minimal_quantity`, pas.`available_date`, ag.`group_type` |
||
218 | FROM `'._DB_PREFIX_.'product_attribute` pa |
||
219 | INNER JOIN '._DB_PREFIX_.'product_attribute_shop pas |
||
220 | ON (pas.id_product_attribute = pa.id_product_attribute AND pas.id_shop = 1) |
||
221 | LEFT JOIN '._DB_PREFIX_.'stock_available stock |
||
222 | ON (stock.id_product = pa.id_product AND stock.id_product_attribute = IFNULL(`pa`.id_product_attribute, 0) AND stock.id_shop = 1 ) |
||
223 | LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`) |
||
224 | LEFT JOIN `'._DB_PREFIX_.'product` pc ON (pa.`id_product` = pc.`id_product`) |
||
225 | LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pc.`id_product` = pl.`id_product`) |
||
226 | LEFT JOIN `'._DB_PREFIX_.'attribute` a ON (a.`id_attribute` = pac.`id_attribute`) |
||
227 | LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON (ag.`id_attribute_group` = a.`id_attribute_group`) |
||
228 | LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute`) |
||
229 | LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group`) |
||
230 | INNER JOIN '._DB_PREFIX_.'attribute_shop attribute_shop |
||
231 | ON (attribute_shop.id_attribute = a.id_attribute AND attribute_shop.id_shop = 1) |
||
232 | WHERE pas.`id_product_attribute`= '.(int)$id_product_attribute.' |
||
233 | GROUP BY id_attribute_group, id_product_attribute |
||
234 | ORDER BY ag.`position` ASC, a.`position` ASC, agl.`name` ASC'; |
||
235 | |||
236 | $combo_groups = $this->dbConn->fetchRowMany($sql); |
||
237 | |||
238 | return $combo_groups; |
||
239 | } |
||
240 | |||
241 | public function getAttributeBase($attribute) { |
||
242 | $sql = 'SELECT al.* |
||
243 | FROM '._DB_PREFIX_.'product_attribute_combination pac |
||
244 | JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang=1) |
||
245 | WHERE pac.id_product_attribute='.(int) $attribute; |
||
246 | $result = $this->dbConn->fetchRowMany($sql); |
||
247 | |||
248 | return $result; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param null|string $id_product_attribute |
||
253 | * @param string $id_product |
||
254 | */ |
||
255 | private function getSpecificPrice($id_product_attribute, $id_product) |
||
256 | { |
||
257 | $specific_price = array(); |
||
258 | if ($this->getNumberSpecificPrice($id_product_attribute, $id_product) > 0) { |
||
259 | $result = $this->getSpecificPriceData($id_product_attribute, $id_product, date('Y-m-d H:i:s')); |
||
260 | $specific_price['price'] = $result['price']; |
||
261 | $specific_price['id_product_attribute'] = $result['id_product_attribute']; |
||
262 | $specific_price['reduction_percent'] = (int) 100 * $result['reduction']; |
||
263 | $specific_price['reduction_price'] = 0; |
||
264 | $specific_price['reduction_type'] = $result['reduction_type']; |
||
265 | |||
266 | return $specific_price; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | |||
271 | /** |
||
272 | * @param string $now |
||
273 | * @param null|string $id_product_attribute |
||
274 | * @param string $id_product |
||
275 | */ |
||
276 | private function getSpecificPriceData($id_product_attribute, $id_product, $now) |
||
277 | { |
||
278 | $sql = 'SELECT * FROM '._DB_PREFIX_.'specific_price |
||
279 | WWHERE id_product = '.(int) $id_product.' |
||
280 | AND id_product_attribute IN (0, '.(int) $id_product_attribute.') |
||
281 | AND ( |
||
282 | (from = \'0000-00-00 00:00:00\' OR \''.$now.'\' >= from) |
||
283 | AND |
||
284 | (to = \'0000-00-00 00:00:00\' OR \''.$now.'\' <= to) |
||
285 | ) '; |
||
286 | |||
287 | $sql .= ' ORDER BY id_product_attribute DESC, from_quantity DESC, id_specific_price_rule ASC'; |
||
288 | |||
289 | $result = $this->dbConn->fetchRow($sql); |
||
290 | |||
291 | return $result; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param null|string $id_product_attribute |
||
296 | * @param string $id_product |
||
297 | */ |
||
298 | private function getNumberSpecificPrice($id_product_attribute, $id_product) |
||
309 | |||
310 | /** |
||
311 | * @param null|string $id_product_attribute |
||
312 | */ |
||
313 | private function getComboImage($id_product_attribute) |
||
314 | { |
||
315 | $sql = 'SELECT pai.id_imageas image |
||
316 | FROM '._DB_PREFIX_.'product_attribute_image pai |
||
317 | LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = pai.id_image) |
||
318 | LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_image = pai.id_image) |
||
319 | WHERE pai.id_product_attribute = '.(int) $id_product_attribute.' ORDER by i.position'; |
||
320 | $image = $this->dbConn->fetchColumn($sql); |
||
321 | ($image !== false) ? $imager = (int) $image : $imager = -1; |
||
322 | return $imager; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param string $category |
||
327 | */ |
||
328 | private function getProducts($category) |
||
335 | |||
336 | /** |
||
337 | * @param string $category |
||
338 | */ |
||
339 | private function getProductIDs($category) |
||
340 | { |
||
341 | $sql = 'SELECT DISTINCT p.id_product |
||
342 | from '._DB_PREFIX_.'product as p |
||
343 | LEFT JOIN '._DB_PREFIX_.'image AS i ON i.id_product = p.id_product |
||
344 | LEFT JOIN '._DB_PREFIX_.'product_lang as pl ON pl.id_product = p.id_product |
||
345 | WHERE p.active = 1 |
||
346 | AND p.id_category_default = '.(int) $category.' |
||
347 | GROUP BY p.id_product'; |
||
360 | |||
361 | /** |
||
362 | * @param string $ids |
||
363 | */ |
||
364 | private function getProduct($ids) |
||
390 | |||
391 | private function getOrderPrice($product) { |
||
396 | |||
397 | private function getProductCat($category) { |
||
402 | |||
403 | |||
404 | public function onClose(ConnectionInterface $conn) |
||
410 | |||
411 | public function onError(ConnectionInterface $conn, \Exception $e) |
||
417 | } |
||
418 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: