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 TaskController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * TaskController constructor. |
||
| 18 | */ |
||
| 19 | public function __construct(TaskTransformer $taskTransformer) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Display a listing of the resource. |
||
| 28 | * |
||
| 29 | * @return \Illuminate\Http\Response |
||
| 30 | */ |
||
| 31 | public function index() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Show the form for creating a new resource. |
||
| 50 | * |
||
| 51 | * @return \Illuminate\Http\Response |
||
| 52 | */ |
||
| 53 | public function create() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Store a newly created resource in storage. |
||
| 60 | * |
||
| 61 | * @param \Illuminate\Http\Request $request |
||
| 62 | * @return \Illuminate\Http\Response |
||
| 63 | */ |
||
| 64 | public function store() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Display the specified resource. |
||
| 79 | * |
||
| 80 | * @param int $id |
||
| 81 | * @return \Illuminate\Http\Response |
||
| 82 | */ |
||
| 83 | public function show($id) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Show the form for editing the specified resource. |
||
| 105 | * |
||
| 106 | * @param int $id |
||
| 107 | * @return \Illuminate\Http\Response |
||
| 108 | */ |
||
| 109 | public function edit($id) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Update the specified resource in storage. |
||
| 116 | * |
||
| 117 | * @param \Illuminate\Http\Request $request |
||
| 118 | * @param int $id |
||
| 119 | * @return \Illuminate\Http\Response |
||
| 120 | */ |
||
| 121 | public function update(Request $request, $id) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Remove the specified resource from storage. |
||
| 129 | * |
||
| 130 | * @param int $id |
||
| 131 | * @return \Illuminate\Http\Response |
||
| 132 | */ |
||
| 133 | public function destroy($id) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param Request $request |
||
| 140 | * @param $task |
||
| 141 | */ |
||
| 142 | public function saveTask(Request $request, $task) |
||
| 149 | |||
| 150 | |||
| 151 | } |
||
| 152 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: