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 |
||
17 | class MainController extends Controller |
||
18 | { |
||
19 | /** |
||
20 | * Fo Page. |
||
21 | * |
||
22 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
23 | */ |
||
24 | public function index() |
||
28 | |||
29 | /** |
||
30 | * Ajax Movies. |
||
31 | * |
||
32 | * @param Request $request |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public function ajaxmovies(Request $request) |
||
37 | { |
||
38 | $validator = Validator::make( |
||
39 | $request->all(), //request all : tous les elements de requetses |
||
40 | [ |
||
41 | 'title' => 'required|min:10', |
||
42 | ], [ |
||
43 | 'title.required' => 'Votre titre est obligatoire', |
||
44 | 'title.min' => 'Votre titre est trop court', |
||
45 | ]); |
||
46 | |||
47 | if ($validator->fails()) { // si mon validateur échoue |
||
48 | return $validator->errors()->all(); |
||
49 | } else { |
||
50 | Movies::create([ |
||
51 | 'title' => $request->title, |
||
52 | 'description' => $request->description, |
||
53 | 'categories_id' => $request->categories_id, |
||
54 | ]); |
||
55 | |||
56 | return $request->title; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Done Payment. |
||
62 | * |
||
63 | * @param Request $request |
||
64 | * |
||
65 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
66 | */ |
||
67 | View Code Duplication | public function done(Request $request) |
|
84 | |||
85 | /** |
||
86 | * Page Dashboard. |
||
87 | */ |
||
88 | public function dashboard() |
||
138 | } |
||
139 |
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.