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 namespace Bantenprov\VueWorkflow\Http\Controllers; |
||
20 | class WorkflowController extends Controller |
||
21 | { |
||
22 | |||
23 | use WorkflowTrait; |
||
24 | |||
25 | protected $workflowModel; |
||
26 | protected $workflowTypeModel; |
||
27 | |||
28 | /** |
||
29 | * WorkflowController constructor. |
||
30 | * @param Request $request |
||
31 | * @param Workflow $workflow |
||
32 | * @param WorkflowType $workflowType |
||
33 | */ |
||
34 | public function __construct(Request $request, Workflow $workflow, WorkflowType $workflowType){ |
||
35 | $this->workflowModel = $workflow; |
||
36 | $this->workflowTypeModel = $workflowType; |
||
37 | |||
38 | //dd($request->path()); |
||
|
|||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param Request $req |
||
43 | * @return \Illuminate\Http\JsonResponse |
||
44 | */ |
||
45 | public function index(Request $req) |
||
46 | { |
||
47 | |||
48 | // $transition = Transition::where('name','propose-to-propose')->where('workflow_id','19')->first(); |
||
49 | // dd($transition->id); |
||
50 | //dd($this->getRequest($req->input('sort'))); |
||
51 | |||
52 | //dd(class_basename($this->workflowTypeModel)); |
||
53 | //dd(class_basename(get_class($this))); |
||
54 | //dd(get_declared_classes()); |
||
55 | // foreach (get_declared_classes() as $value) { |
||
56 | // //Illuminate\Database\Eloquent\Model |
||
57 | // if(get_parent_class($value) == "Illuminate\Database\Eloquent\Model"){ |
||
58 | // echo $value."<br>"; |
||
59 | // } |
||
60 | // //echo $value."<br>"; |
||
61 | |||
62 | // } |
||
63 | // dd(); |
||
64 | $response = array(); |
||
65 | |||
66 | $param = explode('|',$req->get('sort')); |
||
67 | |||
68 | if($req->get('filter') != ''){ |
||
69 | $search = "%{$req->get('filter')}%"; |
||
70 | $response = $this->workflowModel->where('name','like',$search)->orderBy($param[0], $param[1])->paginate(10); |
||
71 | }else{ |
||
72 | if($req->get('sort') == ''){ |
||
73 | $response = $this->workflowModel->paginate(10); |
||
74 | }else{ |
||
75 | $response = $this->workflowModel->orderBy($param[0], $param[1])->paginate(10); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | return response()->json($response); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return \Illuminate\Http\JsonResponse |
||
84 | */ |
||
85 | public function create() |
||
86 | { |
||
87 | $content_type = config('vue-workflow.content_type'); |
||
88 | |||
89 | return response()->json($content_type); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param Request $req |
||
94 | * @return \Illuminate\Http\JsonResponse |
||
95 | */ |
||
96 | public function store(Request $req) |
||
121 | |||
122 | /** |
||
123 | * @param $id |
||
124 | * @return \Illuminate\Http\JsonResponse |
||
125 | */ |
||
126 | public function show($id) |
||
144 | |||
145 | /** |
||
146 | * @param $id |
||
147 | * @return \Illuminate\Http\JsonResponse |
||
148 | */ |
||
149 | View Code Duplication | public function edit($id) |
|
150 | { |
||
151 | |||
152 | $response['status'] = true; |
||
153 | $response['workflow'] = $this->workflowModel->findOrFail($id); |
||
154 | |||
155 | return response()->json($response); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param $id |
||
160 | * @param Request $request |
||
161 | * @return \Illuminate\Http\JsonResponse |
||
162 | */ |
||
163 | public function update($id, Request $request) |
||
188 | |||
189 | |||
190 | /** |
||
191 | * @param Request $req |
||
192 | * @param $id |
||
193 | * @return \Illuminate\Http\JsonResponse |
||
194 | */ |
||
195 | public function storeWorkflow(Request $req, $id) |
||
205 | |||
206 | /** |
||
207 | * @param $id |
||
208 | * @return \Illuminate\Http\JsonResponse |
||
209 | * @throws \Exception |
||
210 | */ |
||
211 | public function destroy($id) |
||
217 | } |
||
218 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.