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 ProductsController 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 ProductsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ProductsController extends Controller |
||
29 | { |
||
30 | /** |
||
31 | * @var ProductsRepository |
||
32 | */ |
||
33 | protected $products; |
||
34 | |||
35 | /** |
||
36 | * @var CategoryableRepository |
||
37 | */ |
||
38 | protected $categoryable; |
||
39 | |||
40 | /** |
||
41 | * @var Store |
||
42 | */ |
||
43 | protected $session; |
||
44 | |||
45 | /** |
||
46 | * @var InvolvedRepository |
||
47 | */ |
||
48 | protected $involved; |
||
49 | |||
50 | /** |
||
51 | * @var modelColorsRepository |
||
52 | */ |
||
53 | protected $modelColors; |
||
54 | |||
55 | /** |
||
56 | * @var LotRepository |
||
57 | */ |
||
58 | protected $lots; |
||
59 | |||
60 | /** |
||
61 | * @var ImprovedSpec |
||
62 | */ |
||
63 | protected $improvedSpecs; |
||
64 | protected $specPrice; |
||
65 | protected $currencies; |
||
66 | |||
67 | /** |
||
68 | * ProductsController constructor. |
||
69 | * @param Store $session |
||
70 | * @param ProductsRepository $productsRepository |
||
71 | * @param CategoryableRepository $categoryableRepository |
||
72 | * @param ModelColorsRepository $modelColorsRepository |
||
73 | * @param InvolvedRepository $involvedRepository |
||
74 | * @param LotRepository $lotRepository |
||
75 | * @param ImprovedSpecRepository $improvedSpecRepository |
||
76 | */ |
||
77 | View Code Duplication | public function __construct( |
|
100 | |||
101 | /** |
||
102 | * @param SaveProductRequest $request |
||
103 | * @param Product $product |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public function save(SaveProductRequest $request, Lot $lot, Product $product) |
||
130 | |||
131 | /** |
||
132 | * Show inner product. |
||
133 | * |
||
134 | * @param $product |
||
135 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
136 | */ |
||
137 | public function show($product) |
||
158 | |||
159 | /* public function convertAmount(){ |
||
160 | $xml = XmlParser::load('http://www.bnm.org/ro/official_exchange_rates?get_xml=1&date='.date("d.m.Y")); |
||
161 | |||
162 | $parsed = $xml->parse([ |
||
163 | 'cursToDay' => ['uses' => 'Valute[CharCode,Value]'], |
||
164 | ]); |
||
165 | |||
166 | $currency = array('EUR','USD'); |
||
167 | $json = array(); |
||
168 | foreach ($parsed as $key => $item) { |
||
169 | foreach ($item as $key => $val) { |
||
170 | if (in_array($val['CharCode'], $currency)) { |
||
171 | $json[$val['CharCode']] = $val['Value']; |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | $put = Storage::put('json_currency.json', json_encode($json)); |
||
176 | }*/ |
||
177 | |||
178 | public function getSalledPercent($id) |
||
186 | |||
187 | |||
188 | public function getSpecifications() { |
||
195 | |||
196 | public function getSpecificationsColor() { |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Delete products.. |
||
207 | * |
||
208 | * @param Request $request |
||
209 | * @param Lot $lot |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | public function remove(Request $request, Lot $lot) |
||
231 | |||
232 | /** |
||
233 | * Save specifications. |
||
234 | * |
||
235 | * @param $specifications |
||
236 | * @param $product |
||
237 | */ |
||
238 | private function saveSpecifications($specifications, Product $product) |
||
246 | |||
247 | |||
248 | private function saveSpecificationsPrice($request, Product $product) |
||
287 | |||
288 | private function saveImprovedSpecifications($specs, $product) |
||
296 | |||
297 | /** |
||
298 | * Remove spec. |
||
299 | * |
||
300 | * @param Request $request |
||
301 | * |
||
302 | * @return void |
||
303 | */ |
||
304 | public function removeSpec(Request $request) |
||
316 | |||
317 | public function removeSpecPrice(Request $request) |
||
322 | /** |
||
323 | * Remove improved spec. |
||
324 | * |
||
325 | * @param Request $request |
||
326 | * @param Lot $lot |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | public function removeImproveSpec(Request $request, Lot $lot) |
||
336 | |||
337 | /** |
||
338 | * Add image to product. |
||
339 | * |
||
340 | * @param $image |
||
341 | * @param Product $product |
||
342 | * |
||
343 | * @return mixed |
||
344 | */ |
||
345 | public function addImage($image, Product $product) |
||
355 | |||
356 | /** |
||
357 | * Remove Image. |
||
358 | * |
||
359 | * @param Request $request |
||
360 | * @param $lot |
||
361 | */ |
||
362 | public function removeImage(Request $request, $lot) |
||
366 | |||
367 | /** |
||
368 | * Save image order. |
||
369 | * |
||
370 | * @param Request $request |
||
371 | * @param $product |
||
372 | */ |
||
373 | public function saveImagesOrder(Request $request, $product) |
||
396 | |||
397 | /** |
||
398 | * Get only changed positions of sorted elements. |
||
399 | * |
||
400 | * @param $newsort |
||
401 | * @param $oldsort |
||
402 | * @return array |
||
403 | */ |
||
404 | private function getChangedSortPositions($newsort, $oldsort) |
||
417 | |||
418 | /** |
||
419 | * Save sorted ranks to changed positions. |
||
420 | * |
||
421 | * @param $changed_positions |
||
422 | * @param $newsort |
||
423 | * @param $oldsort |
||
424 | */ |
||
425 | private function setNewRankToChangedPositions($changed_positions, $newsort, $oldsort) |
||
434 | |||
435 | |||
436 | |||
437 | public function loadSpecPrice(Request $request, Lot $lot) |
||
468 | |||
469 | public function removeGroupPrice(Request $request) |
||
502 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.