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 |
||
16 | class DepartmentController extends Controller |
||
17 | { |
||
18 | /** |
||
19 | * DepartmentController constructor. |
||
20 | */ |
||
21 | public function __construct() |
||
22 | { |
||
23 | $this->middleware('auth'); |
||
24 | $this->middleware('lang'); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Display a listing of the resource. |
||
29 | * |
||
30 | * @return \Illuminate\Http\Response |
||
31 | */ |
||
32 | public function index() |
||
33 | { |
||
34 | $data['query'] = Departments::paginate(15); |
||
|
|||
35 | return view('departments.index', $data); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Show the form for creating a new resource. |
||
40 | * |
||
41 | * @return \Illuminate\Http\Response |
||
42 | */ |
||
43 | public function create() |
||
44 | { |
||
45 | return view('departments.insert'); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Store a newly created resource in storage. |
||
50 | * |
||
51 | * @param Requests\DepartmentsValidator $input |
||
52 | * @return \Illuminate\Http\Response |
||
53 | */ |
||
54 | public function store(Requests\DepartmentsValidator $input) |
||
59 | |||
60 | /** |
||
61 | * Display the specified resource. |
||
62 | * |
||
63 | * @param int $id the department id in the database. |
||
64 | * @return \Illuminate\Http\Response |
||
65 | */ |
||
66 | public function show($id) |
||
71 | |||
72 | /** |
||
73 | * Show the form for editing the specified resource. |
||
74 | * |
||
75 | * @param int $id the department id in the database. |
||
76 | * @return \Illuminate\Http\Response |
||
77 | */ |
||
78 | public function edit($id) |
||
79 | { |
||
80 | $data['query'] = Departments::find($id); |
||
81 | return view('departments.update', $data); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Update the specified resource in storage. |
||
86 | * |
||
87 | * @param Requests\DepartmentsValidator $input |
||
88 | * @param int $id the department id in the database. |
||
89 | * @return \Illuminate\Http\Response |
||
90 | */ |
||
91 | public function update(Requests\DepartmentsValidator $input, $id) |
||
96 | |||
97 | /** |
||
98 | * Remove the specified resource from storage. |
||
99 | * |
||
100 | * @param int $id the department id in the database. |
||
101 | * @return \Illuminate\Http\Response |
||
102 | */ |
||
103 | public function destroy($id) |
||
104 | { |
||
105 | Departments::destroy($id); |
||
106 | return redirect()->back(302); |
||
107 | } |
||
108 | |||
109 | View Code Duplication | public function get_departments() |
|
123 | } |
||
124 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.