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 |
||
9 | class TrainingTypeController extends Controller |
||
10 | { |
||
11 | /** |
||
12 | * Display a listing of the resource. |
||
13 | * |
||
14 | * @return \Illuminate\Http\Response |
||
15 | */ |
||
16 | public function index() |
||
23 | |||
24 | /** |
||
25 | * Show the form for creating a new resource. |
||
26 | * |
||
27 | * @return \Illuminate\Http\Response |
||
28 | */ |
||
29 | public function create() |
||
35 | |||
36 | /** |
||
37 | * Store a newly created resource in storage. |
||
38 | * |
||
39 | * @param StoreTrainingTypeRequest $request |
||
40 | * |
||
41 | * @return \Illuminate\Http\Response |
||
42 | */ |
||
43 | public function store(StoreTrainingTypeRequest $request) |
||
50 | |||
51 | /** |
||
52 | * Show the individual training type record. |
||
53 | * |
||
54 | * @param $trainingtypeId |
||
55 | * |
||
56 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
57 | */ |
||
58 | public function show($trainingtypeId) |
||
66 | |||
67 | /** |
||
68 | * Show the form for editing the specified resource. |
||
69 | * |
||
70 | * @param int $id |
||
71 | * |
||
72 | * @return \Illuminate\Http\Response |
||
73 | */ |
||
74 | public function edit(TrainingType $trainingtype) |
||
80 | |||
81 | /** |
||
82 | * Update the specified resource in storage. |
||
83 | * |
||
84 | * @param \Illuminate\Http\Request $request |
||
85 | * @param int $id |
||
86 | * |
||
87 | * @return \Illuminate\Http\Response |
||
88 | */ |
||
89 | View Code Duplication | public function update(StoreTrainingTypeRequest $request, TrainingType $trainingtype) |
|
98 | |||
99 | /** |
||
100 | * Remove the specified resource from storage. |
||
101 | * |
||
102 | * @param int $id |
||
103 | * |
||
104 | * @return \Illuminate\Http\Response |
||
105 | */ |
||
106 | public function destroy($id) |
||
113 | } |
||
114 |
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.