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 |
||
| 13 | class TaskController extends ApiController |
||
| 14 | { |
||
| 15 | protected $taskTranformer; |
||
| 16 | |||
| 17 | function __construct(TaskTransformer $taskTransformer) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Display a listing of the resource. |
||
| 27 | * |
||
| 28 | * @return \Illuminate\Http\Response |
||
| 29 | */ |
||
| 30 | public function index() |
||
| 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() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Display the specified resource. |
||
| 67 | * |
||
| 68 | * @param int $id |
||
| 69 | * @return \Illuminate\Http\Response |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function show($id) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Show the form for editing the specified resource. |
||
| 85 | * |
||
| 86 | * @param int $id |
||
| 87 | * @return \Illuminate\Http\Response |
||
| 88 | */ |
||
| 89 | public function edit($id) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Update the specified resource in storage. |
||
| 96 | * |
||
| 97 | * @param \Illuminate\Http\Request $request |
||
| 98 | * @param int $id |
||
| 99 | * @return \Illuminate\Http\Response |
||
| 100 | */ |
||
| 101 | public function update(Request $request, $id) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Remove the specified resource from storage. |
||
| 118 | * |
||
| 119 | * @param int $id |
||
| 120 | * @return \Illuminate\Http\Response |
||
| 121 | */ |
||
| 122 | public function destroy($id) |
||
| 126 | } |
||
| 127 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.