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 |
||
| 11 | class TaskController extends ApiController |
||
| 12 | { |
||
| 13 | protected $taskTransformer; |
||
| 14 | /** |
||
| 15 | * TaskController constructor. |
||
| 16 | * @param $taskTransformer |
||
| 17 | */ |
||
| 18 | public function __construct(TaskTransformer $taskTransformer) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Display a listing of the resource. |
||
| 26 | * |
||
| 27 | * @return \Illuminate\Http\Response |
||
| 28 | */ |
||
| 29 | public function index() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Show the form for creating a new resource. |
||
| 40 | * |
||
| 41 | * @return \Illuminate\Http\Response |
||
| 42 | */ |
||
| 43 | public function create() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Store a newly created resource in storage. |
||
| 50 | * @return \Illuminate\Http\Response |
||
| 51 | */ |
||
| 52 | 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) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Show the form for editing the specified resource. |
||
| 86 | * |
||
| 87 | * @param int $id |
||
| 88 | * @return \Illuminate\Http\Response |
||
| 89 | */ |
||
| 90 | public function edit($id) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Update the specified resource in storage. |
||
| 97 | * |
||
| 98 | * @param \Illuminate\Http\Request $request |
||
| 99 | * @param int $id |
||
| 100 | * @return \Illuminate\Http\Response |
||
| 101 | */ |
||
| 102 | public function update(Request $request, $id) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Remove the specified resource from storage. |
||
| 119 | * |
||
| 120 | * @param int $id |
||
| 121 | * @return \Illuminate\Http\Response |
||
| 122 | */ |
||
| 123 | public function destroy($id) |
||
| 127 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.