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 MenuController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Index interface. |
||
| 18 | * |
||
| 19 | * @return Content |
||
| 20 | */ |
||
| 21 | public function index() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Edit interface. |
||
| 33 | * |
||
| 34 | * @param $id |
||
| 35 | * |
||
| 36 | * @return Content |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function edit($id) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Create interface. |
||
| 51 | * |
||
| 52 | * @return Content |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function create() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param $id |
||
| 67 | * |
||
| 68 | * @return $this|\Illuminate\Http\RedirectResponse |
||
| 69 | */ |
||
| 70 | public function update($id) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param $id |
||
| 77 | * |
||
| 78 | * @return \Illuminate\Http\JsonResponse |
||
| 79 | */ |
||
| 80 | public function destroy($id) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 89 | */ |
||
| 90 | public function store() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Make a tree builder. |
||
| 97 | * |
||
| 98 | * @return Tree |
||
| 99 | */ |
||
| 100 | public function tree() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Make a form builder. |
||
| 107 | * |
||
| 108 | * @return Form |
||
| 109 | */ |
||
| 110 | public function form() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return Callout |
||
| 130 | */ |
||
| 131 | protected function callout() |
||
| 137 | } |
||
| 138 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.