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 |
||
| 10 | class DeviceController extends Controller |
||
| 11 | { |
||
| 12 | public function __construct() { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Display a listing of all authorized devices |
||
| 18 | * |
||
| 19 | * @return \Illuminate\Http\Response |
||
| 20 | */ |
||
| 21 | View Code Duplication | public function index(Request $request) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Show the form for creating a new resource. |
||
| 39 | * |
||
| 40 | * @return \Illuminate\Http\Response |
||
| 41 | */ |
||
| 42 | public function create() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Store a newly created resource in storage. |
||
| 49 | * |
||
| 50 | * @param \Illuminate\Http\Request $request |
||
| 51 | * @return \Illuminate\Http\Response |
||
| 52 | */ |
||
| 53 | public function store(Request $request) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Display the specified resource. |
||
| 60 | * |
||
| 61 | * @param int $id |
||
| 62 | * @return \Illuminate\Http\Response |
||
| 63 | */ |
||
| 64 | View Code Duplication | public function show(Request $request, $id) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Show the form for editing the specified resource. |
||
| 77 | * |
||
| 78 | * @param int $id |
||
| 79 | * @return \Illuminate\Http\Response |
||
| 80 | */ |
||
| 81 | public function edit($id) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Update the specified resource in storage. |
||
| 88 | * |
||
| 89 | * @param \Illuminate\Http\Request $request |
||
| 90 | * @param int $id |
||
| 91 | * @return \Illuminate\Http\Response |
||
| 92 | */ |
||
| 93 | public function update(Request $request, $id) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Remove the specified resource from storage. |
||
| 100 | * |
||
| 101 | * @param int $id |
||
| 102 | * @return \Illuminate\Http\Response |
||
| 103 | */ |
||
| 104 | public function destroy($id) |
||
| 108 | |||
| 109 | } |
||
| 110 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.