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 |
||
| 14 | class PostsController extends Controller |
||
| 15 | { |
||
| 16 | private $pagination = 15; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Display a listing of the resource. |
||
| 20 | * |
||
| 21 | * @param \Illuminate\Http\Request $request |
||
| 22 | * @return \Illuminate\Http\Response |
||
| 23 | */ |
||
| 24 | public function index(Request $request) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Show the form for creating a new resource. |
||
| 31 | * |
||
| 32 | * @return \Illuminate\Http\Response |
||
| 33 | */ |
||
| 34 | public function create() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Store a newly created resource in storage. |
||
| 41 | * |
||
| 42 | * @param \App\Http\Requests\PostFormRequest $request |
||
| 43 | * |
||
| 44 | * @return \Illuminate\Http\RedirectResponse |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function store(PostFormRequest $request) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Show the form for editing the specified resource. |
||
| 68 | * |
||
| 69 | * @param \App\Models\Post $post |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | public function edit(Post $post) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Update the specified resource in storage. |
||
| 80 | * |
||
| 81 | * @param \App\Http\Requests\PostFormRequest $request |
||
| 82 | * @param Post $post |
||
| 83 | * |
||
| 84 | * @return \Illuminate\Http\RedirectResponse |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function update(PostFormRequest $request, Post $post) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Remove the specified resource from storage. |
||
| 107 | * |
||
| 108 | * @param \Illuminate\Http\Request $request |
||
| 109 | * @param Post $post |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | * @throws \Exception |
||
| 113 | */ |
||
| 114 | public function destroy(Request $request, Post $post) |
||
| 131 | |||
| 132 | View Code Duplication | public function resource(Request $request) |
|
| 142 | } |
||
| 143 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.