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 |
||
| 12 | class WorkflowTransitionController extends Controller |
||
| 13 | { |
||
| 14 | |||
| 15 | public function index() |
||
| 23 | |||
| 24 | public function create() |
||
| 30 | |||
| 31 | public function store(Request $request) |
||
| 32 | { |
||
| 33 | $name = str_replace('_','-',\Transliteration::clean_filename(strtolower($request->label))); |
||
| 34 | $label = $request->label; |
||
| 35 | $from = $request->from; |
||
| 36 | $to = $request->to; |
||
| 37 | $message = $request->message; |
||
| 38 | |||
| 39 | try |
||
| 40 | { |
||
| 41 | WorkflowTransition::create([ |
||
| 42 | 'name' => $name, |
||
| 43 | 'label' => $label, |
||
| 44 | 'from' => $from, |
||
| 45 | 'to' => $to, |
||
| 46 | 'message' => $message |
||
| 47 | ]); |
||
| 48 | } |
||
| 49 | catch(\Illuminate\Database\QueryException $e){ |
||
| 50 | // do what you want here with $e->getMessage(); |
||
| 51 | return redirect()->route('transition')->with('message', 'Error'); |
||
| 52 | } |
||
| 53 | |||
| 54 | return redirect()->route('transition')->with('message', 'Add data success'); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function show($id) |
||
| 61 | |||
| 62 | View Code Duplication | public function edit($id) |
|
| 69 | |||
| 70 | View Code Duplication | public function update(Request $request, $id) |
|
| 95 | |||
| 96 | public function Active($id){ |
||
| 101 | |||
| 102 | public function DeActive($id){ |
||
| 107 | |||
| 108 | public function destroy($id) |
||
| 112 | } |
||
| 113 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.