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 |
||
| 12 | class TaskController extends Controller |
||
| 13 | { |
||
| 14 | protected $TaskTransformer; |
||
| 15 | protected $tagTransformer; |
||
| 16 | |||
| 17 | public function __construct(TaskTransformer $TaskTransformer) |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Display a listing of the resource. |
||
| 24 | * |
||
| 25 | * @return \Illuminate\Http\Response |
||
| 26 | */ |
||
| 27 | public function index() |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Show the form for creating a new resource. |
||
| 47 | * |
||
| 48 | * @return \Illuminate\Http\Response |
||
| 49 | */ |
||
| 50 | public function create() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Store a newly created resource in storage. |
||
| 57 | * |
||
| 58 | * @param \Illuminate\Http\Request $request |
||
| 59 | * @return \Illuminate\Http\Response |
||
| 60 | */ |
||
| 61 | public function store(Request $request) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Display the specified resource. |
||
| 69 | * |
||
| 70 | * @param int $id |
||
| 71 | * @return \Illuminate\Http\Response |
||
| 72 | */ |
||
| 73 | public function show($id, $task) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Show the form for editing the specified resource. |
||
| 93 | * |
||
| 94 | * @param int $id |
||
| 95 | * @return \Illuminate\Http\Response |
||
| 96 | */ |
||
| 97 | public function edit($id) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Update the specified resource in storage. |
||
| 104 | * |
||
| 105 | * @param \Illuminate\Http\Request $request |
||
| 106 | * @param int $id |
||
| 107 | * @return \Illuminate\Http\Response |
||
| 108 | */ |
||
| 109 | 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) |
||
| 128 | |||
| 129 | public function transformCollection($task) |
||
| 143 | |||
| 144 | } |
||
| 145 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.