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 |
||
11 | class LaralangController extends Controller |
||
12 | { |
||
13 | public function showLogin() |
||
14 | { |
||
15 | if (session('laralang.password') && Crypt::decrypt(session('laralang.password')) == config('laralang.default.password')) { |
||
16 | return redirect(Route('laralang::translations')); |
||
17 | } |
||
18 | |||
19 | return view('laralang::login'); |
||
20 | } |
||
21 | |||
22 | public function login(Request $request) |
||
23 | { |
||
24 | session(['laralang.password' => Crypt::encrypt($request->input('password'))]); |
||
25 | View Code Duplication | if (Crypt::decrypt(session('laralang.password')) != config('laralang.default.password')) { |
|
|
|||
26 | return redirect(Route('laralang::login')) |
||
27 | ->with('status', 'Invalid password'); |
||
28 | } |
||
29 | |||
30 | return redirect()->route('laralang::index'); |
||
31 | } |
||
32 | |||
33 | public function logout(Request $request) |
||
39 | |||
40 | public function index() |
||
41 | { |
||
42 | return view('laralang::index'); |
||
43 | } |
||
44 | |||
45 | public function showTranslations() |
||
46 | { |
||
47 | return view('laralang::translations'); |
||
48 | } |
||
49 | |||
50 | public function showTranslationsFilter() |
||
51 | { |
||
52 | return view('laralang::filter', ['languagesFrom' => Laralang::fromLanguages(), 'languagesTo' => Laralang::toLanguages()]); |
||
53 | } |
||
54 | |||
55 | public function showTranslate() |
||
56 | { |
||
57 | return view('laralang::translate'); |
||
58 | } |
||
59 | |||
60 | public function translate(Request $request) |
||
61 | { |
||
62 | Laralang::generateTranslations($request->input('is_package'), $request->input('package'), $request->input('path'), $request->input('to_langs')); |
||
63 | |||
64 | return redirect()->route('laralang::translate'); |
||
65 | } |
||
66 | |||
67 | public function translationsFilter(Request $request) |
||
71 | |||
72 | public function showTranslationsFiltered($from_lang, $to_lang) |
||
76 | |||
77 | public function api() |
||
81 | |||
82 | public function apiTranslate(Request $request) |
||
88 | |||
89 | public function apiFilterFromTo($from_lang, $to_lang) |
||
108 | |||
109 | public function deleteTranslation(Request $request) |
||
114 | |||
115 | public function deleteAllTranslations() |
||
120 | |||
121 | public function editTranslation(Request $request) |
||
131 | } |
||
132 |
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.