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 DepartmentController extends Controller  | 
            ||
| 18 | { | 
            ||
| 19 | /**  | 
            ||
| 20 | * DepartmentController constructor.  | 
            ||
| 21 | */  | 
            ||
| 22 | public function __construct()  | 
            ||
| 23 |     { | 
            ||
| 24 |         $this->middleware('auth'); | 
            ||
| 25 |         $this->middleware('lang'); | 
            ||
| 26 | }  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * Display a listing of the resource.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @return \Illuminate\Http\Response  | 
            ||
| 32 | */  | 
            ||
| 33 | public function index()  | 
            ||
| 34 |     { | 
            ||
| 35 |         $data['query'] = Departments::with('managers')->paginate(15); | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 36 |         return view('departments.index', $data); | 
            ||
| 37 | }  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * Show the form for creating a new resource.  | 
            ||
| 41 | *  | 
            ||
| 42 | * @return \Illuminate\Http\Response  | 
            ||
| 43 | */  | 
            ||
| 44 | public function create()  | 
            ||
| 45 |     { | 
            ||
| 46 |         return view('departments.insert'); | 
            ||
| 47 | }  | 
            ||
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * Search for a specific department  | 
            ||
| 51 | *  | 
            ||
| 52 | * @param Request $request  | 
            ||
| 53 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View  | 
            ||
| 54 | */  | 
            ||
| 55 | public function search(Request $request)  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * Store a newly created resource in storage.  | 
            ||
| 64 | *  | 
            ||
| 65 | * @param Requests\DepartmentsValidator $input  | 
            ||
| 66 | * @return \Illuminate\Http\Response  | 
            ||
| 67 | */  | 
            ||
| 68 | public function store(Requests\DepartmentsValidator $input)  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * Display the specified resource.  | 
            ||
| 76 | *  | 
            ||
| 77 | * @param int $id the department id in the database.  | 
            ||
| 78 | * @return \Illuminate\Http\Response  | 
            ||
| 79 | */  | 
            ||
| 80 | public function show($id)  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Show the form for editing the specified resource.  | 
            ||
| 88 | *  | 
            ||
| 89 | * @param int $id the department id in the database.  | 
            ||
| 90 | * @return \Illuminate\Http\Response  | 
            ||
| 91 | */  | 
            ||
| 92 | public function edit($id)  | 
            ||
| 93 |     { | 
            ||
| 94 | $data['query'] = Departments::find($id);  | 
            ||
| 95 |         return view('departments.update', $data); | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * Update the specified resource in storage.  | 
            ||
| 100 | *  | 
            ||
| 101 | * @param Requests\DepartmentsValidator $input  | 
            ||
| 102 | * @param int $id the department id in the database.  | 
            ||
| 103 | * @return \Illuminate\Http\Response  | 
            ||
| 104 | */  | 
            ||
| 105 | public function update(Requests\DepartmentsValidator $input, $id)  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Remove the specified resource from storage.  | 
            ||
| 113 | *  | 
            ||
| 114 | * @param int $id the department id in the database.  | 
            ||
| 115 | * @return \Illuminate\Http\Response  | 
            ||
| 116 | */  | 
            ||
| 117 | public function destroy($id)  | 
            ||
| 118 |     { | 
            ||
| 119 | Departments::destroy($id);  | 
            ||
| 120 | return redirect()->back(302);  | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * Get all the departments  | 
            ||
| 125 | *  | 
            ||
| 126 | * @return string  | 
            ||
| 127 | */  | 
            ||
| 128 | View Code Duplication | public function get_departments()  | 
            |
| 142 | }  | 
            ||
| 143 | 
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.