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 |
||
13 | class TrainingTypeController extends Controller |
||
14 | { |
||
15 | /** |
||
16 | * Display a listing of the resource. |
||
17 | * |
||
18 | * @return \Illuminate\Http\Response |
||
19 | */ |
||
20 | public function index() |
||
26 | |||
27 | /** |
||
28 | * Show the form for creating a new resource. |
||
29 | * |
||
30 | * @return \Illuminate\Http\Response |
||
31 | */ |
||
32 | public function create() |
||
37 | |||
38 | /** |
||
39 | * Store a newly created resource in storage. |
||
40 | * |
||
41 | * @param StoreTrainingTypeRequest $request |
||
42 | * @return \Illuminate\Http\Response |
||
43 | */ |
||
44 | public function store(StoreTrainingTypeRequest $request) |
||
50 | |||
51 | /** |
||
52 | * Show the individual training type record. |
||
53 | * |
||
54 | * @param $trainingtypeId |
||
55 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
56 | */ |
||
57 | public function show($trainingtypeId) |
||
64 | |||
65 | /** |
||
66 | * Show the form for editing the specified resource. |
||
67 | * |
||
68 | * @param int $id |
||
69 | * @return \Illuminate\Http\Response |
||
70 | */ |
||
71 | public function edit(TrainingType $trainingtype) |
||
76 | |||
77 | /** |
||
78 | * Update the specified resource in storage. |
||
79 | * |
||
80 | * @param \Illuminate\Http\Request $request |
||
81 | * @param int $id |
||
82 | * @return \Illuminate\Http\Response |
||
83 | */ |
||
84 | View Code Duplication | public function update(StoreTrainingTypeRequest $request, TrainingType $trainingtype) |
|
92 | |||
93 | /** |
||
94 | * Remove the specified resource from storage. |
||
95 | * |
||
96 | * @param int $id |
||
97 | * @return \Illuminate\Http\Response |
||
98 | */ |
||
99 | public function destroy($id) |
||
106 | } |
||
107 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.