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:
1 | <?php |
||
21 | class LotsController extends Controller |
||
22 | { |
||
23 | /** |
||
24 | * @var LotRepository |
||
25 | */ |
||
26 | protected $lots; |
||
27 | |||
28 | /** |
||
29 | * @var Guard |
||
30 | */ |
||
31 | protected $auth; |
||
32 | |||
33 | /** |
||
34 | * @var ProductsRepository |
||
35 | */ |
||
36 | protected $products; |
||
37 | |||
38 | /** |
||
39 | * @var ImprovedSpecRepository |
||
40 | */ |
||
41 | protected $improvedSpecs; |
||
42 | |||
43 | protected $specPrice; |
||
44 | |||
45 | |||
46 | protected $sub_category; |
||
47 | |||
48 | protected $method; |
||
49 | |||
50 | protected $lot_method; |
||
51 | |||
52 | protected $currencies; |
||
53 | |||
54 | /** |
||
55 | * LotsController constructor. |
||
56 | * @param LotRepository $lotRepository |
||
57 | * @param ProductsRepository $productsRepository |
||
58 | * @param ImprovedSpecRepository $improvedSpecRepository |
||
59 | * @param Guard $auth |
||
60 | */ |
||
61 | View Code Duplication | public function __construct( |
|
82 | |||
83 | /** |
||
84 | * Create lot or modify drafted for vendor. |
||
85 | * |
||
86 | * @param Vendor $vendor |
||
87 | * @return \Illuminate\View\View |
||
88 | */ |
||
89 | View Code Duplication | public function create(Vendor $vendor) |
|
96 | |||
97 | /** |
||
98 | * @param Lot $lot |
||
99 | * @return \Illuminate\View\View |
||
100 | */ |
||
101 | View Code Duplication | public function edit(Lot $lot) |
|
108 | |||
109 | /** |
||
110 | * Remove lot. |
||
111 | * |
||
112 | * @param Lot $lot |
||
113 | * @return mixed |
||
114 | */ |
||
115 | public function delete(Lot $lot) |
||
123 | |||
124 | /** |
||
125 | * Select category. |
||
126 | * |
||
127 | * @param Request $request |
||
128 | * @param Lot $lot |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | /* public function selectCategory(Request $request, Lot $lot) |
||
133 | { |
||
134 | if($this->lots->checkIfPossibleToChangeCategory($lot)) { |
||
135 | $this->lots->changeCategory($lot, $request->get('category_id')); |
||
136 | return 'true'; |
||
137 | } |
||
138 | |||
139 | return 'false'; |
||
140 | }*/ |
||
141 | |||
142 | public function selectCategory(Request $request, Lot $lot) |
||
152 | |||
153 | /** |
||
154 | * Load product form for lot create/edit. |
||
155 | * |
||
156 | * @param Request $request |
||
157 | * @param Lot $lot |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function loadProductBlock(Request $request, Lot $lot) |
||
170 | |||
171 | /** |
||
172 | * Load specification |
||
173 | * |
||
174 | * @param Request $request |
||
175 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
176 | */ |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Load improved spec. |
||
181 | * |
||
182 | * @param Request $request |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public function loadImprovedSpec(Request $request) |
||
191 | |||
192 | /** |
||
193 | * @param SaveLotRequest $request |
||
194 | * @param Lot $lot |
||
195 | * @return mixed |
||
196 | */ |
||
197 | public function saveLot(SaveLotRequest $request, Lot $lot) |
||
203 | |||
204 | View Code Duplication | public function updateLot(SaveLotRequest $request, Lot $lot) |
|
215 | |||
216 | View Code Duplication | public function publishedLot(SaveLotRequest $request, Lot $lot) |
|
228 | |||
229 | /** |
||
230 | * |
||
231 | */ |
||
232 | public function index() |
||
236 | |||
237 | /** |
||
238 | * Show user's all lots; |
||
239 | * |
||
240 | * @return \Illuminate\View\View |
||
241 | */ |
||
242 | public function myLots() |
||
247 | |||
248 | /** |
||
249 | * @param Lot $lot |
||
250 | * @return mixed |
||
251 | */ |
||
252 | public function show(Lot $lot) |
||
256 | |||
257 | |||
258 | /** |
||
259 | * Get user from Guard\Auth |
||
260 | * |
||
261 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
262 | */ |
||
263 | public function getUser() |
||
267 | } |
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.