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 |
||
21 | class APIFullUsersController extends Controller |
||
22 | { |
||
23 | /** |
||
24 | * Display a listing of the resource. |
||
25 | * |
||
26 | * @return \Illuminate\Http\Response |
||
27 | */ |
||
28 | public function index() |
||
33 | |||
34 | /** |
||
35 | * Store a newly created resource in storage. |
||
36 | * |
||
37 | * @param CreateUserRequest $request |
||
38 | * @return \Illuminate\Http\JsonResponse |
||
39 | */ |
||
40 | public function store(CreateUserRequest $request) |
||
52 | |||
53 | /** |
||
54 | * Massive destroy. |
||
55 | * |
||
56 | * @param MassiveDestroyRequest $request |
||
57 | * @return \Illuminate\Http\JsonResponse |
||
58 | */ |
||
59 | public function massiveDestroy(MassiveDestroyRequest $request) |
||
64 | |||
65 | /** |
||
66 | * Execute destroy. |
||
67 | * |
||
68 | * @param $ids |
||
69 | * @return \Illuminate\Http\JsonResponse |
||
70 | */ |
||
71 | View Code Duplication | private function executeDestroy($ids){ |
|
81 | |||
82 | /** |
||
83 | * Remove the specified resource from storage. |
||
84 | * |
||
85 | * @param int|array $id |
||
86 | * @return \Illuminate\Http\JsonResponse |
||
87 | */ |
||
88 | public function destroy($id) |
||
94 | |||
95 | /** |
||
96 | * Display the specified resource. |
||
97 | * |
||
98 | * @param int $id |
||
99 | * @return \Illuminate\Http\Response |
||
100 | */ |
||
101 | public function show($id) |
||
106 | |||
107 | /** |
||
108 | * Update the specified resource in storage. |
||
109 | * |
||
110 | * @param UpdateUserRequest $request |
||
111 | * @param $id |
||
112 | * @return \Illuminate\Http\JsonResponse |
||
113 | */ |
||
114 | public function update(UpdateUserRequest $request, $id) |
||
120 | |||
121 | /** |
||
122 | * Register user by email |
||
123 | * |
||
124 | * @param Request $request |
||
125 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
126 | */ |
||
127 | public function registerByEmail(Request $request) |
||
132 | |||
133 | } |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.